Game Development Community

1.5 User error, bug or feature?

by Jon Mitchell · in Torque Game Builder · 07/22/2007 (5:23 pm) · 2 replies

I am having some issues calling functions that had been working in 1.1.3
For example I have a function called enablePlayer1 So in the console I type in: enablePlayer1()

here is the code
function enablePlayer1()
{
   $P1Tank.isDead = false;
   $P1Tank.setEnabled(true);
   $P1TankTurret.setEnabled(true);
}


The console reports this error:
==>enablePlayer1();
game/gameScripts/common.cs (143): Unable to find object: '' attempting to call function 'setEnabled'
game/gameScripts/common.cs (144): Unable to find object: '' attempting to call function 'setEnabled'


While I was testing some other things I discovered other functions that called $P1Tank were not working, one function while typing it in I forgot to put the $ in, and the function worked.
So I went back to that function and removed the $'s so I had this:
function enablePlayer1()
{
   P1Tank.isDead = false;
   P1Tank.setEnabled(true);
   P1TankTurret.setEnabled(true);
}

And that function now works. Thinking I might have messed something else up, I checked code for setting the name for $P1Tank which is correct.

function Player1Tank::onLevelLoaded(%this, %scenegraph)
{
	$P1Tank = %this; //set the player 1 vehicle name
}

I am lost on what is going on, is a product of the change to 1.5?

#1
07/23/2007 (7:25 am)
Neither--you simply need to do it consistently.

A $ or % indicates a variable,with a $ being global. If you also happened to have "named" your object (as many of the tutorials tell you to), then TorqueScript allows you the alternate technique of using the object directly by name.

The subtlety that may be confusing you is that (based on your description) you are using two very similar tokens to both represent the same object:

$P1Tank -- a global variable that contains the object ID of your tank (once you fixed the bug in onLevelLoaded)
P1Tank -- a named object that is also a reference to your tank
#2
07/24/2007 (6:51 am)
I gotcha, in the opening where I name the player's tank it is kind of redundant to keep calling it by the global once the name is set unless I am setting a variable like a health value on it.

Thank you for getting me straightened out Stephen! :-)