Game Development Community

Behavior variable scope

by Bojan Vukojevic · in Torque Game Builder · 12/26/2009 (6:56 pm) · 2 replies

Hello all,

I've downloaded the TGB trial and I'm playing around to see if it is what i need for my project. So far I'm very happy with it but i recently stumbled to a problem that i cant solve on my own. I started learning the basics and by now I'm relatively familiar with all the basic concepts. Today i created a behavior and i attached that behavior to an object that has 1 dynamic field called FuelAmount. There are few functions in my behavior but only built in functions can access it:

function GetFuelBehavior::onUpdate(%this)
{
   %this.owner.schedule(5000, getFuelBehavior.AddFuel());
   echo(%this.owner.FuelAmount);      
   %this.owner.FuelAmount++;
}

function GetFuelBehavior::onPositionTarget(%this)
{
   echo(%this.owner.FuelAmount);      
}

function GetFuelBehavior::AddFuel(%this)
{
   %this.owner.FuelAmount++;
   echo(%this.owner.FuelAmount);      
}
In this example onUpdate and onPositionTarget see %this.owner.FuelAmount without any problems but AddFuel does not. Any custom function i add cant see this dynamic field. I can solve this by using global variables or changing the mechanics but i would like to know what am i doing wrong here. I did search for it on forums a lot but i was not able to find a solution. Any help is appreciated.

Thanks

#1
12/26/2009 (7:58 pm)
I think the issue is with your schedule. The name of the command must be within a string. For it to work in your behavior:

%this.owner.schedule(5000, "AddFuel");
#2
12/27/2009 (2:27 pm)
Thanks a lot Mike that solved the problem. I'm confused because when i used my syntax the function AddFuel was still called i tested it with some echo functions like echo("test test") and i got the string in console. But all is good now that i know how to properly call the function.

Thanks again :)