Game Development Community

Question about Objects and passing string to function

by Jacob Vann · in Torque Game Builder · 08/28/2005 (3:19 pm) · 2 replies

Hey guys -

I've read up some good docs on Torque Objects, and so I'm trying to create my first class / object using T2D. I'm starting small, making a class for the player controls. Here's what I have:

// Object for Controls
$controls = new ScriptObject(SJControlsObject) {
	left = false;
	right = false;
	up = false;
	down = false;
	jump = false;
	shoot = false;
};
 
function SJControlsObject::isKeyDown(%key) {
	switch$ (%key) {
            ...
	}
}

function SJControlsObject::setKeyOn(%key) {
	echo ("I'm here!!" SPC %key);
	switch$ (%key) {
	case "left":
		%this.left = true;
		echo ("Setting Left on!");
           ...
	}
}

function SJControlsObject::setKeyOff(%key) {
 ...
}

function SJControlsObject::resetKeys(%key) {
 ...
}

The "..." is where I've ommited code not relevant to what I'm asking. The object creates without errors, and the functions work. Later on in the code, I have.

...
new ActionMap(playerMap);	
...
playerMap.bindCmd(keyboard, "left", "playerLeft();", "playerLeftStop();");
...
playerMap.push();
...


function playerLeft()
{

	$player.setLinearVelocityX(-300);
	$player.playAnimation(SJRunningLeft);

	echo ("Trying to set left on!");
	$controls.setKeyOn("left");
}

So what I expect is that, when the left arrow key is pressed, $controls.setKeyOn will be called, and a string with the value "left" will be passed.

What ends up happening is, when I echo out the %key variable from within the setKeyOn function, it has a value of 1043. I also have functions for playerRight, playerUp, etc. And every time it seems that the value being passed is 1043. This of course doesn't match anything in the switch block, so it fails and the key map isn't being set correctly.

I looked at the Torque core docs and found nothing special about passing a string to a function. Obviously I'm not doing something right. Anyone have any ideas? It's good to note that the function is getting called just fine, just the parameter being passed is not correct.

Oh yeah, here is what the console spits out if you're interested:

Trying to set left on!
I'm here!! 1043

Thanks!

#1
08/28/2005 (3:33 pm)
The first parameter in any namespace function in TorqueScript is always a reference to the calling object. So, in this case, %key is referring to $controls. All you need to do is change the parameter list for all methods in the SJControlsObject class to (%this, %key).
#2
08/28/2005 (6:07 pm)
That was it!

Thanks! Makes perfect sense in retrospect...