Game Development Community

Something wrong with this behavior...

by Orion the Hunter · in Torque Game Builder · 04/14/2013 (11:50 am) · 8 replies

Hey,

I'm working on a behavior which uses a timer sort of thing. It's supposed to make platforms appear in a sequence, and then disappear when the next comes. It isn't working and I don't even think my loop is being initiated. Here's what I have...

//-----------------------------------------------------------------------------
// Platform Timer
//-----------------------------------------------------------------------------

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

    %template.friendlyName = "Platform Timer";
    %template.behaviorType = "Platform";
    %template.description  = "Platform timer";
    
    %template.addBehaviorField( Number,    "What order this comes along",    FLOAT, 0.0 );
}

function PlatformTimerBehavior::onAddToScene( %this, %scenegraph )
{
%this.schedule(%this.Number * 1000, 0, "TimePlatform");
}

function PlatformTimerBehavior::TimePlatform( %this )
{
%this.Owner.Visible = 1;
echo("Visible!");
%this.schedule(1000, 0, "RemovePlatform");
}

function PlatformTimerBehavior::RemovePlatform( %this )
{
echo("Invisible!");
%this.Owner.Visible = 0;
%this.schedule(3000, 0, "TimePlatform");
}

Any idea what's wrong here?

#1
04/15/2013 (8:33 am)
Not sure if it's the problem, but Number should ALWAYS be greater than zero - and the time requested in schedule() should be more than 32ms just to be safe. I'm a little surprised this doesn't crash with the infamous "missing flux capacitor" assert.

Just to be sure, try putting an echo in onAddToScene() - perhaps echo the method name and the value of Number, for instance.
#2
04/15/2013 (9:07 am)
How are you adding the behavior to an object? Are you loading a scene from file or dynamically adding a BehaviorInstance? Also, how are you adding the owner to the scene?
#3
04/15/2013 (11:47 am)
I just use the editor to add it in. The owner is a sprite that is added in the editor as well. Are you saying I should name the platforms?
#4
04/15/2013 (1:16 pm)
No, you don't need to name them.

But you're saving the level file and then loading it in game? That's what I'm getting from the "I just use the editor to add it in" statement. Sound about right Mich?
#5
04/15/2013 (1:50 pm)
Yep, that's right.
#6
04/15/2013 (2:18 pm)
Well, I did what you suggested but it doesn't seem to work. The echoes in onAddToScene showed up which leads me to believe there is something wrong with
%this.schedule(%this.Number * 1000, 0, "TimePlatform");

Mr. Console.log has been awfully silent...
#7
04/15/2013 (2:41 pm)
Ah, I fixed it!

//-----------------------------------------------------------------------------
// Platform Timer
//-----------------------------------------------------------------------------

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

    %template.friendlyName = "Platform Timer";
    %template.behaviorType = "Platform";
    %template.description  = "Platform timer";
    
    %template.addBehaviorField( Number,    "What order this comes along",    STRING, 1 );
}

function PlatformTimerBehavior::onAddToScene( %this, %scenegraph )
{
%this.schedule(%this.Number * 1000, "TimePlatform");
echo("Platform has been added to the scene.");
}

function PlatformTimerBehavior::TimePlatform( %this )
{
%this.Owner.Visible = 1;
echo("Visible!");
%this.schedule(1000, "RemovePlatform");
}

function PlatformTimerBehavior::RemovePlatform( %this )
{
echo("Invisible!");
%this.Owner.Visible = 0;
%this.schedule(3000, "TimePlatform");
}

Notice how the schedule functions no longer look like schedule(1000, 0, "Function");

It looks like the middle number was the culprit so I removed it in all functions. Thanks!
#8
04/15/2013 (4:06 pm)
Then echo out the result of your schedule:
%sched = %this.schedule(%this.Number * 1000, "TimePlatform");
echo (" @@@ PlatformTimerBehavior created schedule " @ %sched);
It should contain a positive number.