SA-MP Forums Archive
Timer runs longer than it should y_timers - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Timer runs longer than it should y_timers (/showthread.php?tid=484812)



Timer runs longer than it should y_timers - dusk - 01.01.2014

I just set a repeat timer OnPlayerDeath
pawn Код:
AfterDeathTimer[playerid] = repeat AfterDeathFunction(playerid);

The whole function:
pawn Код:
timer AfterDeathFunction[1000](playerid)
{
    new str[32];
    CountTime[playerid]--;
    format(str,sizeof(str),"Pasveiksite po %s",ConvertSecondsString(CountTime[playerid]));
    TextDrawSetString(HospitalDelay[playerid],str);
    TextDrawHideForPlayer(playerid,HospitalDelay[playerid]);
    TextDrawShowForPlayer(playerid,HospitalDelay[playerid]);
    if(CountTime[playerid] <= 0)
    {
        printf("AfterDeathTimer still running with CountTime:%d",CountTime[playerid]);
        stop AfterDeathTimer[playerid];
        TextDrawHideForPlayer(playerid,HospitalDelay[playerid]);
        SendClientMessage(playerid,GREEN,"Jūs jau visiљkai sveikas, galite iљeiti iљ ligoninės!");
        SetPlayerHealth(playerid,100.0);
        set.IsAfterDeath[playerid] = false;
    }
    return 1;
}
Seems correct,doesn't it?
pawn Код:
[22:35:12] AfterDeathTimer still running with CountTime:-2836553[22:35:12] AfterDeathTimer still running with CountTime:-2836553
You see the print. There are 2836553 lines like that... But the stop code is just under the print line... so what's wrong?


Re: Timer runs longer than it should y_timers - Patrick - 01.01.2014

This should work, you did not even stop the AfterDeathFunction that's why it keeps running you just stopped the AfterDeathTimer

pawn Код:
timer AfterDeathFunction[1000](playerid)
{
    new str[32];
    CountTime[playerid]--;
    format(str,sizeof(str),"Pasveiksite po %s",ConvertSecondsString(CountTime[playerid]));
    TextDrawSetString(HospitalDelay[playerid],str);
    TextDrawHideForPlayer(playerid,HospitalDelay[playerid]);
    TextDrawShowForPlayer(playerid,HospitalDelay[playerid]);
    if(CountTime[playerid] <= 0)
    {
        printf("AfterDeathTimer still running with CountTime:%d",CountTime[playerid]);
        stop AfterDeathTimer(playerid);
        stop AfterDeathTimer(playerid);
        TextDrawHideForPlayer(playerid,HospitalDelay[playerid]);
        SendClientMessage(playerid,GREEN,"Jus jau visiškai sveikas, galite išeiti iš ligonines!");
        SetPlayerHealth(playerid,100.0);
        set.IsAfterDeath[playerid] = false;
    }
    return 1;
}



Re: Timer runs longer than it should y_timers - dusk - 01.01.2014

But "AfterDeathTimer" is an array
pawn Код:
stop AfterDeathTimer(playerid);
To be sure I did try it and got a compiler error.


Re: Timer runs longer than it should y_timers - Patrick - 01.01.2014

I have no idea why you're using stop at AfterDeathTimer if you could do this

pawn Код:
AfterDeathTimer[playerid] = 0;
This should work

[pawn]
This should work, you did not even stop the AfterDeathFunction that's why it keeps running you just stopped the AfterDeathTimer

pawn Код:
timer AfterDeathFunction[1000](playerid)
{
    new str[32];
    CountTime[playerid]--;
    format(str,sizeof(str),"Pasveiksite po %s",ConvertSecondsString(CountTime[playerid]));
    TextDrawSetString(HospitalDelay[playerid],str);
    TextDrawHideForPlayer(playerid,HospitalDelay[playerid]);
    TextDrawShowForPlayer(playerid,HospitalDelay[playerid]);
    if(CountTime[playerid] <= 0)
    {
        printf("AfterDeathTimer still running with CountTime:%d",CountTime[playerid]);
        AfterDeathTimer[playerid] = 0, CountTime = 0;
        stop AfterDeathTimer(playerid);
        TextDrawHideForPlayer(playerid,HospitalDelay[playerid]);
        SendClientMessage(playerid,GREEN,"Jus jau visiљkai sveikas, galite iљeiti iљ ligonines!");
        SetPlayerHealth(playerid,100.0);
        set.IsAfterDeath[playerid] = false;
    }
    return 1;
}



Re: Timer runs longer than it should y_timers - dusk - 01.01.2014

pawn Код:
AfterDeathTimer[playerid] = 0;
No, this doesn't work, because AfterDeathTimer has tag:
pawn Код:
Timer:AfterDeathTimer[MAX_PLAYERS]
And you use the variable as it were a function...This is an example from y_timers thread:
pawn Код:
main()
{
    repeat DelayedFunction(42);
    new
        Timer:x = repeat DelayedFunction(42);
    stop x;
}
And that is what I followed.


Re: Timer runs longer than it should y_timers - Patrick - 01.01.2014

So basically the timer won't stop because of repeat & stop, the output will be this, give me a second, I'll try to re-write the code I have here because I I didn't have that much information.

pawn Код:
- Repeat
- Stop
- Repeat
- Stop
- Repeat
- Stop
// and so on, so it will not stop.
EDIT - After I got enough information, I could help you now :P

pawn Код:
//variable
new Timer:AfterDeathTimer[ MAX_PLAYERS ], CountTime[ MAX_PLAYERS ];

//Running the timer.
AfterDeathTimer[ playerid ] = repeat AfterDeathFunction(playerid);
//Asigning the seconds of the timer, example.
CountTime[ playerid ] = 60; //60 seconds for example.

timer AfterDeathFunction[1000](playerid)
{
    new str[32];
   
    if(CountTime[playerid] >= 1)
    {
        CountTime[playerid]--;
        format(str,sizeof(str),"Pasveiksite po %s",ConvertSecondsString(CountTime[playerid]));
        TextDrawSetString(HospitalDelay[playerid],str);
       
        TextDrawHideForPlayer(playerid,HospitalDelay[playerid]), TextDrawShowForPlayer(playerid,HospitalDelay[playerid]);
    }
    else if(CountTime[playerid] <= 0)
    {
        printf("AfterDeathTimer still running with CountTime:%d",CountTime[playerid]);

        stop AfterDeathTimer[playerid]; //Executing the timer.
        CountTime[playerid] = 0; //Executing and returning the array back to its default number ( 0 )
       
        TextDrawHideForPlayer(playerid,HospitalDelay[playerid]);
       
        SendClientMessage(playerid,GREEN,"Jus jau visiљkai sveikas, galite iљeiti iљ ligonines!");
        SetPlayerHealth(playerid, 100.0);
       
        set.IsAfterDeath[playerid] = false;
    }
    return 1;
}
The simple guide I created to help me, and it worked properly
pawn Код:
#include <a_samp>
#include <YSI\y_timers>

new
    CountDown = 0,
    Timer:Timer[MAX_PLAYERS]
;

main()
{
    new playerid = random(501);
    Timer[playerid] = repeat AfterDeathFunction(playerid);
    CountDown = 5;
}

timer AfterDeathFunction[1000](playerid)
{
    if(CountDown >= 1)
    {
        CountDown --;
        printf("%i", CountDown);
    }
    else if(CountDown <= 0)
    {
        CountDown = 0;
        stop Timer[playerid];
        printf("%i", CountDown);
    }
}



Re: Timer runs longer than it should y_timers - dusk - 01.01.2014

Thanks for the help but the problem was elsewhere(I think). As I mentioned the timer is started OnPlayerDeath. It never occured to me: what if someone dies in a hospital? Then a new timer starts running. But the new timer ID overrides the old one. And there it is, I have no way to stop the timer that was started first.

But I'm still grateful for your time