Issue with timer -
FunnyBear - 18.12.2016
Hey there,
I've recently been working on a robbery script to work alongside my store system. It all went well, except for one thing.
The timer would go much slower than set.
Код:
//top of my script
#define ROBBERY_COUNTDOWN (15) //The time it takes for a player to complete a robbery
new
timeLeft[ MAX_PLAYERS ] = ROBBERY_COUNTDOWN,
robTimer[ MAX_PLAYERS ];
Код:
//when a player starts a robbery
robTimer[playerid] = SetTimerEx("StoreRobbery", 1000, true, "i", playerid);
Код:
// timer itself
public StoreRobbery( playerid )
{
TimeLeft[playerid] --;
if( TimeLeft[ playerid ] > 0 )
{
//code here
}
else
{
//code here
KillTimer(robTimer[playerid]);
}
printf("%d", timeLeft[playerid]); //debug
return 1;
}
The timer is used to countdown how many seconds the player has left until they complete the robbery. It is set for a one second lapse, but instead it takes around 20 seconds to do one second.
I'm not quite sure what's wrong.
Could someone please help?
Thanks!
Re: Issue with timer -
Luicy. - 19.12.2016
Add this to onplayerconnect:
PHP код:
timeLeft[playerid] = 15;
http://pastebin.com/z1vM0yrK
Re: Issue with timer -
FunnyBear - 19.12.2016
Quote:
Originally Posted by Luicy.
|
It's still not working. I have a feeling it might have something to do with my OnPlayerKeyStateChange code, because when I tagged most of it out, it sped up the timer, but a little too fast.
http://pastebin.com/S3dKAfzC
Above is a pastebin link containing the code for the timer and OnPlayerKeyStateChange.
Thanks
Re: Issue with timer -
Lordzy - 19.12.2016
I doubt your code is being excessively called when the keys are pressed repeatedly. Debug your code in such cases and also assign a default value for all indexed timers (Timer Ids other than -1 should be flagged as running timers)
pawn Код:
if( storeData[i][virtualID] == GetPlayerVirtualWorld(playerid) )
{
if(robTimer[playerid] != -1) {
//Then your usual code.
}
//Other than that, you know the timer is running so further calls aren't necessary.
}
//Whenever you disable or kill your timer, set the robTimer array also to -1.
KillTimer(robTimer);
robTimer[playerid] = -1;
//Always assign indexed timers to -1 under OnPlayerConnect or your necessary callback.
I used to have a pre-defined KillTimer function to handle these indexed arrays within the function.
pawn Код:
stock KillTimerEx(&timerid) {
KillTimer(timerid);
timerid = -1;
}
I haven't gone thoroughly with your code, but try debugging using print or SendClientMessage functions if the problem still persists.
Re: Issue with timer -
Luicy. - 19.12.2016
Quote:
Originally Posted by FunnyBear
It's still not working. I have a feeling it might have something to do with my OnPlayerKeyStateChange code, because when I tagged most of it out, it sped up the timer, but a little too fast.
http://pastebin.com/S3dKAfzC
Above is a pastebin link containing the code for the timer and OnPlayerKeyStateChange.
Thanks
|
The pastebin I sent you is working all good for me.
Quote:
Number of vehicle models: 0
14
13
12
11
10
9
8
7
6
5
4
3
2
1
0
|
I'll check yours when I get home.
Re: Issue with timer -
FunnyBear - 19.12.2016
Quote:
Originally Posted by Lordzy
I doubt your code is being excessively called when the keys are pressed repeatedly. Debug your code in such cases and also assign a default value for all indexed timers (Timer Ids other than -1 should be flagged as running timers)
pawn Код:
if( storeData[i][virtualID] == GetPlayerVirtualWorld(playerid) ) { if(robTimer[playerid] != -1) { //Then your usual code. } //Other than that, you know the timer is running so further calls aren't necessary. }
//Whenever you disable or kill your timer, set the robTimer array also to -1. KillTimer(robTimer); robTimer[playerid] = -1;
//Always assign indexed timers to -1 under OnPlayerConnect or your necessary callback.
I used to have a pre-defined KillTimer function to handle these indexed arrays within the function.
pawn Код:
stock KillTimerEx(&timerid) {
KillTimer(timerid); timerid = -1; }
I haven't gone thoroughly with your code, but try debugging using print or SendClientMessage functions if the problem still persists.
|
I followed what you suggested, and it seems to work but only on the second time you press the key; I pressed the key once and the timer started but continued very slowly, I pressed it again, and it continued at its normal pace.
Код:
if(newkeys & KEY_WALK)
{
for(new i = 0; i != sizeof(storeData); ++i)
{
if(IsPlayerInRangeOfPoint(playerid, 1.0, storeData[i][cpPos][0], storeData[i][cpPos][1], storeData[i][cpPos][2]))
{
if( storeData[i][virtualID] == GetPlayerVirtualWorld(playerid) )
{
if( robTimer[playerid] != -1 )
{
SendClientMessage(playerid, -1, "You have begun a robbery");
robTimer[playerid] = SetTimerEx("StoreRobbery", 1000, true, "i", playerid);
}
}
}
}
}
Код:
public StoreRobbery( playerid )
{
TimeLeft[playerid] --;
if( TimeLeft[ playerid ] > 0 )
{
SendClientMessage(playerid, -1, "-1");
}
else if( TimeLeft[ playerid ] == 0 )
{
SendClientMessage(playerid, -1, "You have completed a robbery");
KillTimer(robTimer[playerid]);
robTimer[playerid] = -1;
}
printf("%d", TimeLeft[playerid]);
return 1;
}
__________________________________________________ ________
Quote:
Originally Posted by Luicy.
|
I've tried this too, and it's still the same case. It goes slow on the first time you press it, but the second time you press it, it goes at its normal pace.
Re: Issue with timer -
Lordzy - 19.12.2016
That's strange, unless you have some modifications over your timer functions. Make sure if your "StoreRobbery" callback is being called only within the timer. In case if it's being called somewhere else, it could be a reason. You can either check through your whole code or simply change it to something else.
Quote:
Originally Posted by FunnyBear
I followed what you suggested, and it seems to work but only on the second time you press the key; I pressed the key once and the timer started but continued very slowly, I pressed it again, and it continued at its normal pace.
Код:
if(newkeys & KEY_WALK)
{
for(new i = 0; i != sizeof(storeData); ++i)
{
if(IsPlayerInRangeOfPoint(playerid, 1.0, storeData[i][cpPos][0], storeData[i][cpPos][1], storeData[i][cpPos][2]))
{
if( storeData[i][virtualID] == GetPlayerVirtualWorld(playerid) )
{
if( robTimer[playerid] != -1 )
{
SendClientMessage(playerid, -1, "You have begun a robbery");
robTimer[playerid] = SetTimerEx("StoreRobbery", 1000, true, "i", playerid);
}
}
}
}
}
|
The timer will not be affected on your second button press turn because as per the code, robTimer is not equal to -1 once the key is pressed and so it cannot call the timer again. Do note that time delays are not completely efficient. The do have some variations. You can check for "timerfix" plugin which almost fixes this issue.
PS : Check if the timer is running under the first if statement of the code you've given here. You don't have to simply loop around your storeData array each time the key is pressed.
Re: Issue with timer -
SickAttack - 19.12.2016
You have "10000" in that pastebin link, "1000" is one second.
Timer IDs are never reused and start at 1, there's no need to set values to -1 to 'prevent' killing random timers or any timer at all.
Re: Issue with timer -
Lordzy - 19.12.2016
Quote:
Originally Posted by SickAttack
You have "10000" in that pastebin link, "1000" is one second.
Timer IDs are never reused and start at 1, there's no need to set values to -1 to 'prevent' killing random timers or any timer at all.
|
Good catch, I didn't check the pastebin link. Though, having a constant value can help to know whether a repeating timer is still active or not.
Re: Issue with timer -
SickAttack - 19.12.2016
Quote:
Originally Posted by Lordzy
Good catch, I didn't check the pastebin link. Though, having a constant value can help to know whether a repeating timer is still active or not.
|
True, but really only needed if you want to track active timers I suppose.
An if-then would just slow down the process before killing a timer, as doing it on a invalid timer ID does nothing anyway.