Game Development Community

a global statement

by rennie moffat · in Torque Game Builder · 02/22/2010 (3:55 pm) · 5 replies

function starsStar1Behavior::mountStar(%this)
{
	%this.owner.mount($star, %this.mountX, %this.mountY, %this.mountForce, true);
	$starsStar1Mounted = true;

}


I am wondering. The way I have set up this global variable.. well it is less a variable than a statment, but doing it like this, will it worK? as in...

if I call it later in a completely separate behavior.


so in another behavior

if($starsStar1Mounted == true)
yadda yadda yadda


About the author

My thanks to Garage Games and the Garage Games Community combined with owned determination I got one game up, Temple Racer and I am looking to build more interesting, fun games for the mass market of the iOS app store.


#1
02/22/2010 (4:28 pm)
Should be fine. I believe if you call the variable without the global $ see what result you get back. I know if you place starsStar1Mounted into a variable command within the editor, or a gui button it works.

In script I believe this will work, along these lines..

function runme(%run)
{
%run = $starsStar1Mounted;

}
#2
02/22/2010 (4:31 pm)
Yes, global variables are just that, global to the program. They can be called anywhere at any time. Something to keep in mind though is that global variables are global across all clients, and can cause problems if not used properly in a multiplayer scenario.
#3
02/22/2010 (5:05 pm)
thanks guys.

i am very perplexed,
i have a behavior behavior1, with an indentical object holding behavior 2, and identical object holding behavior3 etc.

each one has about 3 global statements like that, and each is attached to a single button

and i used
if($condition1 == true
{
if($condition2 == true)
{
if(condition3 == true)
{
do this
}
}
}

anyhow, its not working. slightly puzzled.





I was told I should use this to test

echo($starsStar1Mounted)

to see if it is working, but am unsure where to place the echo.

should I
a. place it in the command to mount starsStar1
b. the call to the global variable, so if...
c. or into the console itself.

and if true,
what should i see.
(still learning how to use echos properly.)
thanks

#4
02/22/2010 (7:06 pm)
if the 'global' variable is just used to go across the objects behaviors, meaning you want to use or update $Var1 in behavior1 and behavior2 of the same object, then you should put $Var1 in the owner of the behavior.

You can use this to access Var1 in all behaviors of the object. Also you will have to set the varible in the objects onLevelLoaded function. Like this:

function someObject::onLevelLoaded(%this, %scene)
{
     %this.Var1 = 0;
}

Then to use Var1, assuming someBehavior1 is a behavior of someObject, you do this in the behavior function:

function someBehavior1::doSomeThing()
{
     %x = %this.owner.Var1;
     //or....
     %this.addVar1(%this.owner.Var1);
}

Only use $ when you need multiple objects to use or update the same variable. Like keeping score or updating time, ect.. Also these could be done in a none global way using 'get' and 'set' type functions.

Hope this helps...
#5
02/22/2010 (7:30 pm)
hmm.
i am a little confused by what you are saying.


my global variables are all conditions set in onUpdate (currently)
so...
function starsStar2Behavior::onUpdate(%this)
{
	$starsStar2Pos = %this.owner.getPosition();
	
	if(t2dVectorCompare($starsStar2Pos, %this.laodPos))
	{
		$starsStar2Loaded = true;
	}
	
	if(t2dVectorCompare($starsStar2Pos, %this.waitPos))
	{
		$starsStar2Waiting = true;	
	}
	
	$starsStar2Mounted = %this.owner.getIsMounted();
}

and this is true for starsStar1, starsStar3, 4, 5 etc.


I then call these conditions in the behaviors where starsStar3, calls starsStar2. if starsStar2 is out of the loadPos ($starsStar2Loaded == false) then load starsStar3 etc.