Game Development Community

Memory leak? bindCmd in a behavior returns junk data

by daffodilistic · in Torque Game Builder · 11/16/2010 (10:34 am) · 3 replies

Hi all,

Use the following code below as a behavior:
if (!isObject(fishBehavior))
{
   %template = new BehaviorTemplate(fishBehavior);
   
   %template.friendlyName = "Fish Behavior";
   %template.behaviorType = "Player Behavior";

   %template.addBehaviorField(yVelocity, "The object's vertical speed", float, 5.0);
}

function fishBehavior::onLevelLoaded(%this)
{
   //moveMap is already defined in another place, and function call does work
   moveMap.bindCmd(keyboard, "space", "fishBehavior.jump(1);", "");
   moveMap.bindCmd(keyboard, "z", "redirect(1);", "");
}

function fishBehavior::jump(%value)
{
   //line below gives me junk data
   echo("yVelocity is" @ %this.yVelocity);
}

function redirect(%value)
{
   fishBehavior.jump(%value);
}

Create a new project and scene, and assign this behavior to a sprite. Press the space and z keys, and open the console. After pressing space, I do not get any data, but if I use redirect(), I get junk data.

#1
11/16/2010 (1:31 pm)
Try:
function fishBehavior::jump(%this, %value) {  
   //line below gives me junk data  
   echo("yVelocity is" @ %this.yVelocity);  
}
I'm really new to Torque, this is just going off of the shooter tutorial. This is untested; simply a guess.
I'd need to look into the debugging to understand why it's specifically not working. I've got some really strange fixes for my game because of %this.
#2
12/17/2010 (6:48 am)
It seems you're calling jump() on a null object.
#3
12/28/2010 (2:03 pm)
Here's the deal... TorqueScript doesn't have a concept of "this" like a real object oriented language does.

So, to accomplish what you need, for *ALL* class methods (className::methodName) the first parameter should be %this. If you omit it, like you did on line 18, %value would be assigned the data that should be in %this.

So Timi's solution should work barring any other errors.