Game Development Community

[RESOLVED] Can't return a value from a behavior method

by Chris Hoopes · in Torque Game Builder · 01/22/2013 (8:48 am) · 2 replies

I'm having a bit of trouble trying to get a value from a behavior method and I saw that there weren't any responses from this thread from a while ago. Here's an example of what I'm trying to accomplish:

I have a behavior, we'll call it ThisBehavior and it has a field SomeObject that takes in a t2dSceneObject.
if ( !isObject( ThisBehavior) )
{
   %template = new BehaviorTemplate( ThisBehavior);

   %template.addBehaviorField( SomeObject, "Some other object", OBJECT,  "", t2dSceneObject );
}

SomeObject is an object that is assigned a different behavior that contains some values such as
if ( !isObject( ThatBehavior) )
{
   %template = new BehaviorTemplate( ThatBehavior);

   %template.addBehaviorField( SomeValue, "Some value", STRING,  "Default" );
}

So what I need to do is reference ThisBehavior's SomeObject assigned values. I can't do this via standard dot notation as this
function ThisBehavior::someFunction( %this )
{
    echo( %this.SomeObject.SomeValue );
}
...won't echo any value at all. Only a blank string is written out.

I thought that I could fix this by creating methods from SomeObject's assigned behavior so I wrote this:
function ThisBehavior::someFunction( %this )
{
    echo( %this.SomeObject.getSomeValue() );
}

function ThatBehavior::getSomeValue( %this )
{
    return %this.SomeValue;
}

Now the strange thing here is that once again nothing is written to the screen but ThatBehavior::getSomeValue is being executed correctly. If within getSomeValue I echo the value to be returned through echo( %this.SomeValue ); I see the correct value is being accessed just fine.

So why isn't anything being returned? Is there something in the engine's source that's preventing behavior methods from returning any values? If so, what can I do about this?

#1
01/22/2013 (5:36 pm)
Ok, you guys are confusing the heck out of me - I think this is what we're talking about....

if (!isObject(MyBehavior))   
{   
   %template = new BehaviorTemplate(MyBehavior);   

   %template.addBehaviorField(Time, "The time (in seconds) to scale", float, 2.0);   
}   

function MyBehavior::getTime(%this)
{
	return %this.Time;
}

In order to get the field value on the instance of the behavior owned by a particular object, you have to get that instance and then either directly access it (%instance.Time) or call the method -
function MySprite::onMouseDown(%this)
{
    %instance = %this.getBehavior(MyBehavior);
    echo(" @@@ Scale time : " @ %instance.getTime());
    // also works:
    echo(" @@@ Scale time : " @ %instance.Time);
}

This will echo the value of the field in the behavior instance owned by the object that calls this onMouseDown() handler.

If you're not accessing the instance then you're asking the template to provide a value that it doesn't have.

If you're trying to return a value that belongs to the owning object, access it using the instance's .owner field.

function MyBehavior::getSize(%this)
{
	return %this.owner.size;
}

function MySprite::onMouseDown(%this)
{
    %instance = %this.getBehavior(MyBehavior);
    echo(" @@@ Size : " @ %instance.getSize());
    // also works
    echo(" @@@ Size : " @ %instance.owner.size);
}
#2
01/22/2013 (6:33 pm)
Sorry if my example was confusing but using getBehavior to get an object's instance is exactly what I needed to fix my problem. Thanks! Now I can just use that instead of having to rely on methods to return values from the assigned object.

And to apply what you said into my example to clear it up for anyone else who might stumble upon this in the future:
function ThisBehavior::someFunction( %this )
{
    %instance = %this.SomeObject.getBehavior( ThatBehavior );
    echo( %instance.SomeValue );
}