Game Development Community

Question about Global Variables

by Matt Sanders · in Torque Game Builder · 03/01/2005 (5:22 pm) · 12 replies

Do you have to use global variables to define players and objects in T2D? I just have noticed that alot of people are using stuff like $Player. I was just wondering if there is a specific reason people are doing this other then to easily access info later on.

#1
03/01/2005 (5:39 pm)
Probably because it's easy/convenient. Definitely not required, and I wouldn't do it for a project of any complexity. Depends on your needs, but I prefer to be as object-oriented as possible. I guess I'll find out how far I can take that desire with TorqueScript.
#2
03/01/2005 (5:53 pm)
I'm using it a lot for now because I haven't fully grasped the concepts of laying out and structuring my code in TorqueScript. Obviously it's never a good thing to use a lot of globals. I hope to cut back personally (gee I sound like an addict :P)
#3
03/02/2005 (3:00 am)
@Matt: As has been said above, globals, particularly when starting/prototyping, as just convienient.

More production-quality games would want to ensure that objects are stored in containers and that when the game/mission finishes, these containers and their contents are destroyed.

You can lookup SimSets/SimGroups in the TGE engine doco for an idea on how to maintain abstract lists of objects.

The good thing about T2D is that when you add any of its objects to the scene (fxSceneGraph2D), they are automatically destroyed when the scene is destroyed and so the scene can be used as a container.

WIth singular objects like a $player, it's much easier to simply use a global although when networking comes along, this practice won't scale very well.

Also, I personally don't like wide-scale use of naming objects e.g. "new fxCoolObject2D(MyNamedObject)" because of the potential of naming conflicts. This is particularly true for the naming of GUI elements which don't have the concept of instantiation when placed onto the Canvas.

- Melv.
#4
03/02/2005 (9:15 am)
Thanks again Melv you rock. I just wanted to see if I have this correct though... you only have to worry about SimSets/SimGroups in TGE when you are editing the Source (C++) Correct? I am just wondering because alot of code I write is actually in Script (for now).

Also I noticed you said that with networking it would not be a good idea to use globals. Is this also true with TGE?

I am always trying to learn the best and most efficient ways to work with Torque products.
#5
03/02/2005 (9:35 am)
In a single player game on a single machine a global can be accessed through any of the script, however when working with networked (TGE for example) you have to pass information back and forth between the server and client, if its simple calls you can do calls to server and clients... while large ammounts of info need passed in the packets as it updates...

you can create a Simset and/or Simgroup TGE... one important thing about simgroups (that Stephen Zepp has reiterated a lot) is that when you delete a Simgroup it deletes all of its contents and an object can only belong to one Simgroup, it will silently remove it from any others
#6
03/02/2005 (10:22 am)
... and also, SimGroups / SimSets are not just C++ objects, they are script objects and very useful they are.

TGE uses them for things like Mission-Groups where anything that was specifically created for a mission is added to them, that way when the mission ends, you can delete the group and all the objects follow.

- Melv.
#7
03/03/2005 (6:37 am)
Cool I am starting to understand that more now. only thing is in script do you use the keywords SimSets/SimGroups to use them. I was just wondering because All I see is SimDatablocks in the Documentation for scripting.

EDIT: I guess the concern that I have is that maby I am not using these when I should be. For example do you have to creat a SimSet/SimGroup when you create a vehicle or character to manage the vehicle or character correctly or do they already belong to one if you use the default datablocks?
#8
03/03/2005 (6:41 am)
I've also been looking for mroe information about SimSets/SimGroups, and after scouring the documentation, I can't seem to find any function information.
#9
03/03/2005 (6:43 am)
Not sure where it is in the doco but you use....

new fxStaticSprite2D(myObject);

// Create Set.
new SimSet(mySet);

// Add Object to Set.
mySet.add( myObject );

// Get Container Count.
%count = mySet.getCount();

// Get First Object.
%obj = mySet.getObject( 0 );

// Remove Object.
mySet.remove( myObject );

// Clear Objects from list.
mySet.clear();

// Check membership.
%isMember = mySet.isMember( myObject );

// List Objects.
mySet.listObjects();

Try typing the following into the console...

new SimSet().dump();


- Melv.
#10
03/03/2005 (6:49 am)
Ok now that makes me a little nervous about some of the stuff I have done in TGE I will have to try and find some more info on this somewhere (not sure where though). If you do not use these does it create data leaks? Also another NewBish question: How do you know if your program has leaks? (I ask this because I see alot of people in the forums talking about how they see huge memmory leaks in thier progs and they end up finding where it is to fix, Is there a tool to aid in this process?) I know these are probably very stupid questions but I see no documentation for them that is why I finally decided to ask to see if someone could clear this up for me :).
#11
03/03/2005 (7:22 am)
Thanks for the mini-tutorial Melv! You are truly a god among men. :)
#12
03/04/2005 (5:22 am)
Here you go...

for ( %n = 0; %n < mySet.getCount(); %n++ )
{
    %obj = mySet.getObject(%n);

    %obj.someSuperFunkyT2DFunction();
}

- Melv.