Issue with timer
#1

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!
Reply
#2

Add this to onplayerconnect:
PHP код:
timeLeft[playerid] = 15
http://pastebin.com/z1vM0yrK
Reply
#3

Quote:
Originally Posted by Luicy.
Посмотреть сообщение
Add this to onplayerconnect:
PHP код:
timeLeft[playerid] = 15
http://pastebin.com/z1vM0yrK
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
Reply
#4

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.
Reply
#5

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.
Reply
#6

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.
Посмотреть сообщение
Add this to onplayerconnect:
PHP код:
timeLeft[playerid] = 15
http://pastebin.com/z1vM0yrK
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.
Reply
#7

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.
Reply
#8

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.
Reply
#9

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.
Reply
#10

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.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)