Game Development Community

Equivalent of Thread.sleep()

by Arden · in Torque X 2D · 10/06/2010 (6:58 pm) · 10 replies

Hiya guys,

This might seem like a simple question, but what is the TorqueX equivalent of Thread.sleep()? I created a spawner component and I want to set a timer between spawns.

Thanks for any info!

#1
10/06/2010 (7:21 pm)
Is there a reason why you cant use Thread.Sleep();?

using System.Threading;


;-) Aaron
#2
10/06/2010 (7:31 pm)
I'm trying to set a timer before cloning an object, as in:
public T2DSceneObject Spawn()
        {
            // Initial timer before Spawning

            while (count < _spawndelay)
            {
                count++;
            }
            
            T2DSceneObject newInstance = _template.Clone() as T2DSceneObject;
            Modify(newInstance);
            
            TorqueObjectDatabase.Instance.Register(newInstance);

            return newInstance;            
        }

But it's still not waiting before spawning the template. In an imperative language like basic, this would work. So I'm looking for an alternative, or a way to understand better how I can control flow it so it works.
#3
10/06/2010 (7:32 pm)
Thread.Sleep() will do the job ... <- FALSE | TRUE -> ... If you want an alternate solution, you could setup a Countdown Timer, execute the code when the time reaches 0 and then restart the timer.
#4
10/06/2010 (7:41 pm)
Try This (just off the top of my head, I havnt tested):

public T2DSceneObject Spawn()
        {
            // Initial timer before Spawning
            // 5 second delay
            Thread.Sleep(5000);

            T2DSceneObject newInstance = _template.Clone() as T2DSceneObject;
            Modify(newInstance);

            TorqueObjectDatabase.Instance.Register(newInstance);

            return newInstance;
        }

Thread.Sleep(); takes int miliseconds or TimeSpan

Let me know if that works
#5
10/07/2010 (12:50 am)
You can also use the game's scheduling method

Game.Instance.Engine.RealTimeSchedule.Schedule(milliseconds, methodToCall);

The method has to look like this,
private void _removeStun(object sender, ScheduledEventArguments scheduleEventArguments) {

}
If you're spawning in a process tick I just use the dt variable passed into ProcessTick.

spawnTime += dt
if(dt > timeInSeconds){
spawn;
}
#6
10/07/2010 (7:51 am)
Thats not something you would use sleep for.. just so you know. Those methods sound good.
#7
10/07/2010 (8:46 am)
@Will: They use sleep in the XNA Racing Game kit to delay the sound effect code, why shouldn't it be used to delay the spawning code? I'm curious, cuz I'm still learning :-)
#8
10/07/2010 (5:37 pm)
1. Well, for one, C# is an imperative language.

2. If he call's thread.sleep(), he'll sleep his entire game, because he didn't create a separate thread first for the spawner.

3. Why does this one particular spawner need a separate thread? The answer is it doesn't.

#9
10/07/2010 (6:37 pm)
Thanks Will, I was under the impression that sleep would only effect the code within the method and should have given it a test run :) I will call you Yoda, and I could be the young Jedi? :-)
#10
10/08/2010 (3:17 pm)
Thanks for the advice guys, really appreciated.