Game Development Community

Scope question

by Alex Head · in Torque Game Builder · 02/20/2011 (1:32 pm) · 1 replies

While going over the documentation tutorials i noticed that within a script functions are either explicitly scoped or not and I do not know why.

for example in the fish game player.cs script there is "function PlayerFish::updateMovement()" and "function FishPlayerUp()". Both functions are usable only by the PlayerFish class, so why the diffrence?

#1
02/21/2011 (5:44 am)
PlayerFish:: functions can be called like this

%obj = new t2dStaticSprite()
{
scenegraph = t2dScene;
class = "PlayerFish";
};

%obj.updateMovement();

Only playerFish can do that because they have the class name.

You can explicitly call

PlayerFish::updateMovement(%differentObject);

since the function is defined as:
function PlayerFish::updateMovement(%this)

....

If you used function FishPlayerUp(%this) you would have to call it as

%obj = new t2dStaticSprite()
{
scenegraph = t2dScene;
class = "PlayerFish";
};

fishPlayerUp(%obj);

for every single object. Not very convenient, especially with callbacks. For example you could have an onPositionTarget callback after calling moveTo on a t2d object.... Then when your obejct gets where it's moving it calls the callback. If you had a FishPlayer class static sprite then it would call:

function FishPlayer::onPositionTarget(%this)

which you can define in your code to do custom code for the FishPlayer only.

Hope this helps.