Sleep
by baylor wetzel · in Torque Game Builder · 03/28/2007 (10:40 am) · 1 replies
Two questions
First, is there a sleep function in Torquescript? i tried writing my own, first using a while loop with getSimTime() and then with a semaphor i built out of a ScriptObject and schedule, but both crash TGB. Which frankly doesn't surprise me - polling that frequently inside a function often kills scripting engines. But since those don't work, is there a sleep equivalent already in TGB?
The second question is whether there's a better approach to doing this than sleep
My game: a turn based strategy where one or both sides (normally both sides) are controlled by AI. So unit A1 might go, then B1, A2, A4, A6, B3, etc. If i just let them run at full speed, you can't tell what's going on, so i want them to pause a little. A1 goes, wait one second, B1 goes, wait a second, etc.
Hmm, alternatives. i suppose i could do something like :
Actually, that would probably work, although for my specific game i'll need to figure out how to build the turn order (it's based on speed, with some units getting to go more often than others)
First, is there a sleep function in Torquescript? i tried writing my own, first using a while loop with getSimTime() and then with a semaphor i built out of a ScriptObject and schedule, but both crash TGB. Which frankly doesn't surprise me - polling that frequently inside a function often kills scripting engines. But since those don't work, is there a sleep equivalent already in TGB?
The second question is whether there's a better approach to doing this than sleep
My game: a turn based strategy where one or both sides (normally both sides) are controlled by AI. So unit A1 might go, then B1, A2, A4, A6, B3, etc. If i just let them run at full speed, you can't tell what's going on, so i want them to pause a little. A1 goes, wait one second, B1 goes, wait a second, etc.
Hmm, alternatives. i suppose i could do something like :
function nextTurn()
{
$currentTurn++;
%agent = $turnOrder[$currentTurn];
highlightUnit(%agent);
%agent.onTurn();
}
function Actor::onTurn(%this)
{
do something
schedule(1000, 0, nextTurn);
}Actually, that would probably work, although for my specific game i'll need to figure out how to build the turn order (it's based on speed, with some units getting to go more often than others)
About the author
Associate David Higgins
DPHCoders.com
So if your units all have a 'doMove' function in there class, regardless of what class they are from -- for example, I would create a Unit class, then create a Soldier, Tank, Spotter, Archer class ... and assign all my units the superClass of Unit, and then give them there respective 'class' values of Soldier, Tank, Spotter, Archer, etc ...
Then I would write a Queue class, which would most likely work on a SimSet... then I would simply schedule the 'doMove' function (which would be in the 'Unit' class, but the other classes could override it for special move functionality for that class type) ... I would then have a 'perform' function in the Queue class ... which would look something like:
function UnitQueue::perform(%this) { %obj = %this.Queue.getObject(0); %this.Queue.remove(%obj); schedule(30, %this, action, %obj); } function UnitQueue::action(%this, %obj) { %obj.doMove(); schedule(1000, %this, perform) }That was all free-hand, so is very error prone ... but should show the general concept ... this would allow for a time interval to be processed ... you could even make the "1000" in the schedule a variable ... so that the player could have a "Slow, Medium, Fast" option ... depending on whether or not they want to wait for the moves to occur, or just have everything happen to get the game time cut down ...
Hope that helps.