Game Development Community

Global Variable Question

by Zachary Woodard · in General Discussion · 10/26/2011 (10:35 am) · 5 replies

All,

In the tutorials, I see a lot of code similar to this:

function playerShip::onLevelLoaded(%this, %scenegraph)
{
$pShip = %this;
... Do stuff
}

function DoOtherStuff()
{
... Do stuff referencing $pShip ...
}

Is there any way to set up a system that doesn't use Global Variables? I tried passing %this to other functions, but it would go out of scope and even crash functions defined in main.cs (i.e. The ActionMap defined at the beginning of most tutorials). Can the actionmap be made local to the class that handles the player interaction? Is there a better method? If there's an existing tutorial or code, I'd love to see it. As always, any help is appreciated.

"Snackin" Zack

#1
10/26/2011 (10:38 am)
Whoops, that should have said it crashes code from game.cs, not main.cs :)

Thanks,

"Snackin"
#2
10/26/2011 (11:35 am)
Well, if you pass variables correctly, theoretically it shouldn't go out of scope. I'm not sure what problem you're running into.

There's no reason you can't "package" or just push/pop actionmaps based on class. I know that only answers this specific case, but what problems are you having with global variables and/or passing variables?
#3
10/26/2011 (11:49 am)
Lemme throw out some code that didn't work (TGB literally shut down) I'm just posting the relevant code snippets, not full functions to save some space:

Game.cs:

function startGame(%level)
{
// compile the extra scripts.
// Exec stuff here

// From Shooter tutorial
new ActionMap(moveMap);
moveMap.push();

// Some default stuff left here

sceneWindow2D.loadLevel(%level);
}

function playerShip::onLevelLoaded(%this, %scenegraph)
{
moveMap.bindCmd(keyboard, "up", "playerShip::SetThrustOn(%this);", "playerShip::SetThrustOff(%this);"); // device, key, function onDown, function onUp
// Other Movemap stuff, in similar fashion goes here
}

function playerShip::SetThrustOn(%o)
{
%o.keyThrust = true;
%o.updateMovement();
}

function playerShip::updateMovement(%o)
{
if (%o.keyThrust == true)
{
// Do Stuff here
}
// Repeat if structures for other control features.
}

So, moveMap is defined in Game.cs and belongs to whatever namespace/class (what nameSpace or class DOES it belong to?). Evertything else was in the playerShip class, and that Namespace/slassname matches the class set in the editor for the player ship sprite. A dump confirmed the functions were there. However, when I pressed the "up" key (thrust), TGB would crash and shut down. Is there an example of adding the ActionMap to the class so I can digest the code?

As always, thanks for trying to steer me in the right direction.

"Snackin"
#4
10/28/2011 (11:19 am)
You're doin it wrong! Hehe

Here's where you messed up:

playerShip::SetThrustOn(%this);

should be

%this.SetThrustOn();

The logic error you're running into is you're confusing "playerShip" (a class) with "%this", "$pShip" or whatever variable you want to use (an instance or object OF that class)

You can't call a method from a class, you must call it from an object (which is of the desired class).

Also, typically if you only have one parameter passed into a function, you should leave it named as %this for consistency's sake. Using %o can make it harder to read your code visually, and might be confusing if you send in multiple objects for a method. As the torque script primer suggest, the first parameter of any class member function is always the object itself (%this).
#5
10/29/2011 (10:06 am)
@Daniel: Thanks for the reply. you're right, that example I posted is totally not right. It became that way after multiple permutations of "just trying stuff". I was trying to fix a "cannot find function SetThrustOn" error when passing the function to the bindCmd function on the ActionMap. I got it working later.

@Daniel: I also appreciate you pointing out the line from the primer about the fist parameter of a member class function being %this. I read that somewhere but couldn't find it.

This thread has raised two more questions for me though.

1) Just to confirm, if, for some weird reason, I wrote a member class function that would handle multiple objects, it should look like this:

function nameSpace::functionName(%this, %o)

Where %this is a reference to the current nameSpace instance, and %o is the object passed in from some other function. Is this correct?

Second Question:

I see a variable called moveMap in tutorials, created in game.cs. I understand where and how it is created, I understand it is an Actionmap object. I do not see a "$" on it to denote it as global. Anyone know why or how this works? What's the scope? Does it work this same way with other objects created with the new keyword in that they don't take an operator or modifier to indicate scope?

Thanks for any and all answers. Believe it or not (I'm walking on air) I made a lot of progress this week.

Snackin