PDA

View Full Version : Timer help?


Kruno88
13/04/2017, 11:31 PM
How do I set a simple timer that calculates the time it took you to go from place A to place B?
Like:
It took you 43 seconds to finish the race

GangstaSunny.
14/04/2017, 12:11 AM
Just start a repeating 1 second timer and set a PVar. If he enters the last checkpoint kill the timer and show the PVar.

Simple as that.

Kruno88
14/04/2017, 12:13 AM
Just start a repeating 1 second timer and set a PVar. If he enters the last checkpoint kill the timer and show the PVar.

Simple as that.

Oh,nice idea.
I'll know how to make that :D(no sarcasm)

GangstaSunny.
14/04/2017, 12:18 AM
Don't forget to DeletePVar just to be sure :D

DRIFT_HUNTER
14/04/2017, 01:32 AM
How about gettime() ? It returns timestamp in seconds. Save that, when race starts. Then when race ends get time stamp again and subtract the one you saved from it.

new curTime = gettime();
new timeTaken = gettime() - curTime;

All you have to do now is convert that timeTaken from seconds to format you need (probably Minutes:Seconds)

GangstaSunny.
14/04/2017, 08:20 AM
I was thinking about the timestamp option too and i think its a better one.

Kruno88
14/04/2017, 11:38 AM
Hmmmm,can anybody send me an example?I can't get it to work,....

LEOTorres
14/04/2017, 12:35 PM
Hmmmm,can anybody send me an example?I can't get it to work,....

You can also assign a timer to a callback which increments a variable by one every second.

new raceTimer[MAX_PLAYERS];
new raceTime[MAX_PLAYERS];

forward CountSeconds (playerid);

public CountSeconds (playerid)
{
raceTime[playerid]++;
return 1;
}

When the race starts, do the following:

raceTimer[playerid] = SetTimerEx("CountSeconds", 1000, true, "i", playerid);

When the race ends, do the following:

KillTimer (raceTimer[playerid]);
new string[256];
format (string, sizeof(string), "ID %i took %i seconds to complete the race!", playerid, raceTime[playerid]);
SendClientMessageToAll (-1, string);
raceTime[playerid] = 0;

GangstaSunny.
14/04/2017, 12:49 PM
//Start race
SetPVarInt(playerid,"RaceTime",gettime());

//End race
printf("you were %d seconds on the street.",gettime()-GetPVarInt(playerid,"RaceTime"));