Game Development Community

respawnTime at random

by Vlad I · in Torque Game Builder · 10/16/2011 (12:03 am) · 15 replies

I have an animation of a spider attached to a path which I want to kill with mouse click.
The spider crawls along the path till it hides under a dish plate.
I want this animation to be repeated at random respawn time til it gets whacked.
I'm using the Pathing in TGB editor and using Restart in Follow Mode.

How do I get this animation to be respawned at random time ?

#1
10/16/2011 (8:05 am)
Okay - try this:

$respawnTime = getRandom(minAmount, maxAmount);

In this example, $respawnTime represents whatever variable you are using to store the respawn interval, and minAmount and maxAmount stand for whatever you wish to be the maximum and minimum amount the getRandom returns.
#2
10/16/2011 (10:11 am)
I have my animation linked mounted on Path with Follow Mode Restart,in my Dynamic Fields I have minRespawnTime 10000, maxRespawnTime 50000, minSpeed 1 , maxSpeed 100 and
I'm trying this in my Spider.cs :


function Spider::spawn(%this, %scenegraph)

{

%this.speed = getRandom(%this.minSpeed, %this.maxSpeed);
%respawnTime = getRandom(%this.minRespawnTime, %this.maxRespawnTime);


}
It doesnt show any error and doesnt change the speed or respawn time, the spider simply follows the path and gets restarted on node 0 and goes into loop.
#3
10/16/2011 (1:15 pm)
Have you made sure that your spider is in the spider class? Also, you will need to make sure the function is called when you wish it to be - say, when it reaches a certain point on the path, or reaches the end.
#4
10/16/2011 (10:26 pm)
" Have you made sure that your spider is in the spider class? "
Yes it is.

" Also, you will need to make sure the function is called when you wish it to be - say, when it reaches a certain point on the path, or reaches the end. "

How do I do it? I need to go into that TGB Pathing behavior which I dont know where, but I found another pathing behaviour which looks exactly the same :

//-----------------------------------------------------------------------------
// AttachPath behaviour
// for Torque Game Builder
// by Michael Hartlef
// Version 1.0, January 14, 2008
//-----------------------------------------------------------------------------

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

%template.friendlyName = "Attach Path";
%template.behaviorType = "AI";
%template.description = "Attach an object to a path";

%template.addBehaviorField(path, "The path", object, "", t2dPath);
%template.addBehaviorField(speed, "The speed at which the object will move along the path", float, "1.0");
%template.addBehaviorField(direction, "1 to specify forward movement, -1 to specify reverse movement", integer, "1");
%template.addBehaviorField(startnode, "The node that the object starts at (0=default)", integer, "0");
%template.addBehaviorField(endnode, "The node that the object ends at (0=default)", integer, "0");
%followM = "WRAP" TAB "REVERSE" TAB "RESTART";
%template.addBehaviorField(followmode, "The action to take upon reaching the end of the path", enum, "WRAP", %followM);
%template.addBehaviorField(loops, "The number of loops to take around the path (0=unlimited)", integer, "0");
%template.addBehaviorField(sendtostart, "True to send the object directly to the start node, false to have the object move there", bool, "0");
}


function AttachPathBehaviour::onLevelLoaded(%this, %scenegraph)
{
if (isObject(%this.path))
%this.path.attachObject(%this.owner, %this.speed, %this.direction, %this.startnode, %this.endnode, %this.followmode, %this.loops, %this.sendtostart);
}

How do I call my function, I can see %followM = "WRAP" TAB "REVERSE" TAB "RESTART"; but no KILL, so I dont know how to call it when it reaches a certain node :( What should I be looking into?

Thank you for your help btw ;)
#5
10/17/2011 (4:33 pm)
Alright - here's a solution. Your spider is moving along a path. All you need to do is setup world limits where you want your function to be called to restart the path at a certain time - and have it call that function on the world limit.

Also, when you have defined your path, you need to make sure the variable, isLooping is equal to "0".
#6
10/18/2011 (1:47 am)
I'm following your advice.
Hers is what I got in my Spider.cs :

function Spider01::onWorldLimit(%this, %mode, %limit)
{
if (%limit = "Top")
{
%this.playAnimation ("SpiderAnim03");
}
}

The console doesnt show any errors, but it doesnt work.

The spider keeps crawling despite the world limits I set up for the top. It should be hitting the world limit and restart the animation and crawl from node 0.
In the editor for Pathing I put zero loops, with follow mode Restart, in World Limits limit Mode as NULL, with callback checked, and in scripting as class Spider01.
Why does it ignore my world limits ? I'm puzzled.

However if I set world limits to Kill it gets deleted but no animation play from node 0, somehow my Null callback doesnt want to work.
#7
10/18/2011 (4:38 am)
Ok some update I changed the code slightly and now I can see that the world limits callback does work :

function Spider01::onWorldLimit(%this, %mode, %limit)
{
if (%limit $= "Top")
{
%this.playAnimation ("SpiderAnim03");
}
}

So far so good. Now I need to make my spider to respawn at random times if not killed
#8
10/18/2011 (8:22 am)
I got stuck again,I cant figure out how to make it played at random times upon hitting the world limit. Console doesnt show any error I'm trying to use this for the spider :

function Spider01::onWorldLimit(%this, %mode, %limit)
{
if (%limit $= "top")
{
// %this.safedelete();
%spawnTime = getRandom(3, 10);
%this.playAnimation ("SpiderAnim03");
%this.schedule(%spawnTime, "spawn");

}
}
#9
10/18/2011 (9:07 am)
I think I see the problem. It actually IS random - it's just random in such tiny amounts you can't see it. Get rid of %this.safedelete() - as I see you have commented it out. Then raise the getRandom to 3000 and 10000. The schedule function works in MILLISECONDS.

So, try what you have now, except it's getRandom(3000, 10000);
#10
10/18/2011 (9:15 am)
No, it doesnt change anything still the same.
I even tried to put getRandom(30000, 100000);
Just to notice if there is any change
#11
10/18/2011 (12:36 pm)
Hmm...interesting. I'll do a little research later - try some different ways of doing things, then let you know what works. I just need to know this: WHEN do you want the spider to decide to respawn? When it crawls under the plate?

And one more thing - it respawned, but when you added the schedule, it didn't? Interesting...
#12
10/18/2011 (7:35 pm)
Thank you Laura for helping me on this one.
Yes, I want my spider to respawn when it crawls under the plate.
And it does so but I want it to respawn always at different times to make it less pattern.
I made a Path with 0,1,2,3,4 nodes. I mounted my animation on that Path, on node 0 (it is start node) it crawls along the path to node 4, then restarts and back at node 0 and loops , be cause I set Follow Mode to Restart in its Pathing - all this I did in the editor.

That is what I did initially, then I started scripting, made spider function and started playing with World limits and it didnt help.

So all I want is to have my spider get restarted back on node 0 at a different (random) time.
#13
10/18/2011 (7:42 pm)
Got it. I'll get to work as soon as I have access to a mouse at all times (I lost mine, and now the family has to share one)
#14
10/19/2011 (5:41 am)
Ok I got some update :

function Spider01::onLevelLoaded(%this, %scenegraph)
{

%this.startX = %this.getPositionX();
%this.startY = %this.getPositionY();
%this.spawn();
}

function Spider01::spawn(%this)
{

// Set the speed limits
// %this.minSpeed = -5;
// %this.maxSpeed = -20;
// %this.setPositionY(%this.startY);
// %this.setPositionX(%this.startX);
SpiderPath01.attachObject(SpiderA, 5, -1, 2, 3, "WRAP", 1, true);

}

function Spider01::onWorldLimit(%this, %mode, %limit)
{
if (%limit $= "top")
{
%this.spawn();
}
}

The SpiderA crawls till it hits world limit I set up and instantly appears on start node 2 as I defined but then it doesnt move to node 1 and 0 , but stays at the node 2 with animation being played. the console doesnt show any errors .What gives?

P.S. The reason I'm using this method is because I think I'd be able to add speed random and respawn random later. But first I need to make it work.
#15
10/31/2011 (1:05 am)
ghjmhjmhjm