Is it possible? I have to break my habit or running everything in ‘update’ somehow!
There are multiple ways; the route I took when I ported some code from Panda to Unity is to create a set of “Intervals” classes that, at the basic level are state machines that do ‘something’ for a certain amount of time or a flag/trigger event is set. Inside of the main Update() function I call a “Do interval work”. Each “something” has a specific derived class from the base that does things like play a sound for x time, move gui from n1 to n2, fire a message after x seconds, stack other sub-intervals, etc.
You could take this general idea and extended it to use coroutines, or if you’re gutsy, unsupported threading.
thanks for the inspiration. I guess what I could do is make a schedule controller that just, when called, creates a new script that sleeps for X time then calls a string fed sendmessage.
I’ll have to get round to that some time. Currently making a game by myself and it’s a bit too soon to be worried about that I guess ![]()
cheers.
Here is the code fwiw. The class is attached, and an example how I use it:
public class MainCode : MonoBehaviour
{
internal MetaInterval rsgInterval;
const String RSG_COMPLETE = "rsg_animation_done";
void Update()
{
rsgInterval.WorkInterval();
}
void Start()
{
rsgInterval = new MetaInterval();
rsgInterval.AddInterval(new WaitInterval(0.50f));
rsgInterval.AddInterval(new ShowInterval(readySetGoNode));
rsgInterval.AddInterval(new WaitInterval(0.75f));
rsgInterval.AddInterval(SetupReadySetGoAnimation_add321sub(3));
rsgInterval.AddInterval(SetupReadySetGoAnimation_add321sub(2));
rsgInterval.AddInterval(SetupReadySetGoAnimation_add321sub(1));
rsgInterval.AddInterval(SetupReadySetGoAnimation_add321sub(0));
rsgInterval.AddInterval(new HideInterval(countdownNode));
rsgInterval.AddInterval(new HideInterval(readySetGoNode));
rsgInterval.SetCompletion(gameObject, "IntervalCompleted", RSG_COMPLETE);
}
void TriggerRSG()
{
rsgInterval.StartInterval();
}
internal IntervalBase SetupReadySetGoAnimation_add321sub(int number)
{
String str = number.ToString();
if (number == 0)
str = "GO!";
MetaInterval meta = new MetaInterval("sub1");
meta.AddInterval(new SetTextInterval(countdownNode, str));
meta.AddInterval(new ShowInterval(countdownNode));
NodePathInterval path1 = new NodePathInterval(countdownNode, 0.75f);
path1.SetStartEndPos(new Vector3(0.5f, 0.80f, 1.0f), new Vector3(0.5f, (number == 0) ? 0.43f : 0.5f, 1.0f));
float endV = (number == 0) ? 0.28f : 0.45f;
path1.SetStartEndScale(new Vector3(0.020f, 0.020f, 1), new Vector3(endV, endV, 1));
path1.SetStartEndFontSize(3, 512);
meta.AddInterval(path1);
meta.AddInterval(new PlaySoundInterval((number == 0) ? longHornSound : shortHornSound, 0.25f, false));
meta.AddInterval(new HideInterval(countdownNode));
return meta;
}
}
void IntervalCompleted(System.Object parms)
{
switch (parms.ToString())
{
case RSG_COMPLETE:
// mumble
break;
}
}
I’ve grossly over-simplified my example from an existing project; in the example the Work is called during Update, but in my real code, Work is called from a coroutine, and there is a lot of other intervals that are called (if they are not running, nothing happens).
What the above Ready, Set, GO! sample does is
- Wait 1/2 a second
- Show the “Get Ready!” text banner
- Wait 3/4 of a second
- Add 4 sub-intervals. These each have additional steps:
4A: Set the text of the node to 3, 2, 1, or GO!
4B: show the text node
4C: set up a movement path that runs for 3/4 of a second that moves and scales the text node
4D: plays a sound when the animation ends for 1/4 a second (sound continues after this)
4E: hide the text node
5,6,7: duplicate of 4.
8: hide the countdown node
9: hide the Get Ready Node
10: fire a “done” even to IntervalCompleted with a text string.
599954–21324–$Intervals.zip (2.78 KB)
I don’t know anything about Torque, but it sounds like you want coroutines. There is a really good set of scripts on the wiki called coroutine scheduler that would probably help.