Game Development Community

Function Call Scope Issue

by Michael Mashayekh · in Torque Game Builder · 02/18/2009 (2:11 pm) · 4 replies

From my GUI Control, I am trying to call a function that exists in my Player class. I am using the Adventure Kit, and my player is global (it can be access with $player).

From <guiControlName>.onDialogPush() I can easily access the function that exists in my $player. Simply use: $player.<functionname>();

But from the GUI Button Control command feature, this same thing does not work. It says it can't find my player object (even though its global!).

I wonder, is there a special scoping case while executing a command issued from a GUI Button Control? Does anyone have a crafty way to get around this?

#1
02/19/2009 (8:10 am)
I don't know about the scoping issue but you could just do this.
Have the button command just call a standard function and then call your $player.<functionname>(); from that function in your script.

Add

ButtonPushed(); in the command for the GUI button.

then in script

Add

function ButtonPushed()
{
echo ("Gui Button has been pushed");
$Player.doSomething();
}

#2
02/19/2009 (8:12 am)
i have tried $player.doSomething(); for the button's command but it doesnt work at all. apparently it only works when your function call is like "doThis();" or "objectName.doThis();" It doesnt seem to work with global var.
#3
02/19/2009 (9:43 am)
Yeah the wierd issue is that the compiler is telling me it can't find my global object when it is called from the ButtonPushed() command (as used from the GUI Controls).

It will work from onPushDialog, but not from onPopDialog.

The way i solved it for the time being is to just directly set the variables i want to change from the ButtonPushed() command...they're global and I can at least set them. Its ugly and slopy, but I don't understand how I could do it otherwise.
#4
02/19/2009 (10:00 am)
> It will work from onPushDialog, but not from onPopDialog.

that's pretty odd.

perhaps something has unset $player in the interim ?
try printing the value of $player in there,
and/or try out setting some other, new global variable like $gFoo and see if it's accessible or not from the routines.

or you could just go with the solution you've got, but those are the sorts of inconsistencies where i personally really like to understand what's going on.