SA-MP Forums Archive
Little Mathematic ^_^ - 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: Little Mathematic ^_^ (/showthread.php?tid=594029)



Little Mathematic ^_^ - Sanady - 12.11.2015

Hello everybody I have little problem.I set timer ex, for 1 second and it will repeat, but in callback I set one variable which always will be +1 when timer repeats so it`s should give numbers like 1,2,3,4,5,6,.... but problem that it give me numbers always 1,1,1,1,... why I don`t know.

pawn Код:
SetTimerEx("OnPlayerTryToCapture", 1000, 1, "i", playerid);

public OnPlayerTryToCapture(playerid)
{
    new timer;
    timer += 1;
    SendDebug(playerid, "Time: %d", timer);
    return 1;
}



Re: Little Mathematic ^_^ - Ahmad45123 - 12.11.2015

Just make the variable global.
Like:
PHP код:
SetTimerEx("OnPlayerTryToCapture"10001"i"playerid);
new 
timer;
public 
OnPlayerTryToCapture(playerid)
{
    
timer += 1;
    
SendDebug(playerid"Time: %d"timer);
    return 
1;

The reason behind this is because whenever the function finishes.. Simply, the variable is reset to zero.. So you need to make it global so its never reset unless explicitly.


Re: Little Mathematic ^_^ - Amads - 12.11.2015

You create a new 'timer' int each time OnPlayerTryToCapture is called, no wonder it's always 1

// oops, too late


Re: Little Mathematic ^_^ - Crayder - 12.11.2015

Yeah it's because you are creating the variable "timer" each time the timer is called. Make it a global variable.


EDIT: You know what? Fuck both of you other guys that answered at nearly the same exact time I did... XD


Re: Little Mathematic ^_^ - Sanady - 12.11.2015

F***, I totally forget that, thank you.