Game Development Community

How do I access variables from another file?

by Eunice Chen · in Torque Game Engine · 04/13/2006 (8:20 am) · 3 replies

I declared a counter variable in the server/game.cs file. I want my server/trigger.cs file to increment the variable when something enters the trigger, but I can't access this variable correctly.

The original variable was declared %player.applesStolen = 0; In trigger.cs, I've tried everything from %client.player.applesStolen to %player.applesStolen but none of them seem to access/output the variable correctly. What do I use to access this variable?

In server/game.cs:
function GameConnection::createPlayer(%this, %spawnPoint)
{
...
%player.applesStolen = 0;
echo("Apples Stolen: " @ %player.applesStolen); //this outputs 0 correctly
}

In server/trigger.cs:
function treeTrigger::onEnterTrigger( %this, %trigger, %obj )
{
echo("Apples Stolen: " @ %client.player.applesStolen); //this doesn't output anything..HELP Please!
%player.applesStolen++;
echo("Apples Stolen: " @ %player.applesStolen); //this outputs nothing as well :(
}

EDIT: THe player's counter needs to be incremented. But the objects entering the trigger are AI Players/bots, not the player itself. So I can't use the %client thing..?

About the author

Recent Threads


#1
04/13/2006 (8:40 am)
In server/game.cs:
function GameConnection::createPlayer(%this, %spawnPoint)
{
...
   $player::applesStolen = 0;
   echo("Apples Stolen: " @ $player::applesStolen);
}

In server/trigger.cs:
function treeTrigger::onEnterTrigger( %this, %trigger, %obj )
{
   echo("Apples Stolen: " @ $player::applesStolen);
   $player::applesStolen++;
}
#2
04/13/2006 (12:07 pm)
Read the scripting reference for a better overview on the scripting syntanx.

You use % for local variables, and $ for global variables.

Local variables exist only inside the function they were declared at. If you declare a local variable outside any function (in bare script body), it'll exist only during the execution of the script (during the exec() command), and will be wiped out when the last line in the script is executed, and it'll not be avaliable inside of functions declared in the same script.

Global variables are avaliable across all functions and all namespaces, and can be accessed and modified anywhere in the code.

And the file have little relevance to the scripts. A function declared in triggers.cs can be called in game.cs and vice versa. You could even move them all to foo.cs and everything would work the same (as long as the foo.cs script is executed, of course).

The only thing that would be file-dependant is code placed in the script body, outside of any function. In that case, the code in the body will be executed when you exec() the script as if it were part of a function (something like a root function on the script).
#3
04/13/2006 (12:40 pm)
Thanks a lot to the both of you. Syntax has always been the hardest part of coding for me. Heh.