Game Development Community

Creating a timer - schedule trouble

by __._ · in Torque Game Engine · 11/28/2006 (3:08 am) · 14 replies

I am trying to create a timer (probably the hard way) but can't get it to work. I have the following code, mostly gathered from old forum posts:

function startTimer(%timer, %time)
{
	%timer.time = %time;
	schedule(1000, %timer, tickTimer, %timer);
}

function tickTimer(%timer)
{
	%timer.time++;
	commandToClient(%client, 'SetTimer', %timer.time);
	schedule(1000, %timer, tickTimer, %timer);
	echo("timer tick");
}

Problem is that the tickTimer function is not being executed. The startTimer function is called.

Whats wrong with my code?

#1
11/28/2006 (4:14 am)
Not sure you should be passing %timer in as the reference object? The command isis defined as :

ConsoleFunction(schedule, S32, 4, 0, "schedule(time, refobject|0, command, <arg1...argN>)")

This works:
function startTimer(%time) {
	echo("timer started");
	schedule(1000, 0, tickTimer, %time);
}
function tickTimer(%time) {
	echo("timer tick");
	%time++;
	schedule(1000, 0, tickTimer, %time);
}

with startTimer(0); to kick it off.
#2
11/28/2006 (5:02 am)
Thanks that fixed it. But maybe there is a default function to retreive playtime I'm not aware of?
#3
11/28/2006 (5:23 am)
Hey RJ, I was in need of a timer last week. So, I wrote a high resolution timer in the engine and exposed it to the script engine. So now, I can do something like this:

[b]// Create the datablock for it[/b]
datablock TimerData(StandardTimer)
{
   type = 0;
};

[b]// Make a timer[/b]
new Timer(GameTimer)
{
   dataBlock = StandardTimer;
   nameTag = "GameTimer";
};

[b]// Start timer when player enters the game[/b]
function GameConnection::onClientEnterGame(%this)
{
   // ... Below camera creation and player spawning
   GameTimer.startTimer();
}

[b]// Get the amount of time level lasted when we disconnect[/b]
function disconnect()
{
    echo(GameTimer.getElapsedSeconds());   [b]// Can call this anywhere[/b]
}

If this is something you are interested in, I can send it to you. If others are interested, I'll just make a resource out of it.
#4
11/28/2006 (5:33 am)
Very kind of you, but unfortunately (long story short) I am not allowed to rebuild Torque with new functionality due to version control.
#5
11/28/2006 (5:37 am)
Is there someone who is allowed? I can just submit the code, and you can pass it to the one who is responsible. Or are you in a no-engine-modification situation? If that's the case, I'll weep for you, because modifying the engine is where the great stuff comes from.
#6
11/28/2006 (5:49 am)
When a client enters the game the server synchronises their clock (in server/game.cs GameConnection::onClientEnterGame) via the following clientCmd:

commandToClient(%client, 'SyncClock', $Sim::Time - $Game::StartTime);

I haven't played with this but it looks like there are two server global variables there that you may be able to use.
#7
11/28/2006 (5:51 am)
Start weeping then ;-)

But yes, the decision has been made (for now) not to modify the source in any way so that when Torque has to be installed somewhere it always is the same Torque as always, and it's guaranteed to work (no screw-ups adding new functionality that has bugs in it etc.). If there is one thing students bitch about it's software that doesn't work the way it should or how it was described.

Personally I would have liked to add all the stuff I miss, like elevators, cameras and this timer, and then "freeze" the code, but it's not up to me.
#8
11/28/2006 (6:06 am)
Your syntax is incorrect and you should take a look (by searching for "schedule" in all .cs files) at the existing scripts. Do also take a look at console.log each time you have trouble, because your syntax error would had been told in there.

schedule(1000, %timer, [b]tickTimer[/b], %timer); // <- Incorrect syntax
#9
11/28/2006 (6:54 am)
It already works now, but this is the syntax I got out of other forum posts. But Paul K's code already made it clear it was wrong.
#10
11/28/2006 (6:57 am)
RJ did you take a look at those two globals i mentioned in the second post?

$Sim::Time and $Game::StartTime
#11
11/28/2006 (7:03 am)
Yes I did, $Sim::Time is the time you have been playing the game, but it starts counting when you launch Torque, not when you start a mission.
#12
11/28/2006 (7:38 am)
Quote:
It already works now, but this is the syntax I got out of other forum posts. But Paul K's code already made it clear it was wrong.

Well, just making sure that you noticed the error since it is very clear by looking trough the source how it should be, thus my clarification.

Quote:
Yes I did, $Sim::Time is the time you have been playing the game, but it starts counting when you launch Torque, not when you start a mission.

What about $Game::StartTime?
#13
07/22/2009 (7:41 am)
hi,
could you explain the schedules variables?

schedule(1000, 0, tickTimer, %time);
#14
07/22/2009 (7:52 am)
ps. I am dealing in TGB, I will post there.