static variable works, but not the 'new' - 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: static variable works, but not the 'new' (
/showthread.php?tid=582113)
static variable works, but not the 'new' -
Beckett - 17.07.2015
PHP код:
// This belongs under OnPlayerUpdate
static str[7] /* Works fine, but when "new str[7]" doesn't work at all and GetVehicleSpeed returns 0. */
new Float:speed = GetVehicleSpeed(vid,false);
new round = floatround(speed);
vid = GetPlayerVehicleID(playerid);
format(str,sizeof(str),"%i MPH",round);
PlayerTextDrawSetString(playerid,Speed[playerid],str);
printf("(Speed: %i)",round);
Already so basically this, when 'str' variable is static it returns the correct speed, but when 'str' isn't static the GetVehicleSpeed returns 0 and I don't think the GetVehicleSpeed function needs to be posted here as it works perfectly when the 'str' variable is static.
"Str" variable isn't static:
pawn Код:
[03:00:18] (Speed: 0)
[03:00:18] (Speed: 0)
[03:00:18] (Speed: 0)
[03:00:18] (Speed: 0)
Variable is a static:
pawn Код:
[03:01:21] (Speed: 47)
[03:01:22] (Speed: 48)
[03:01:22] (Speed: 47)
[03:01:22] (Speed: 46)
[03:01:22] (Speed: 46)
[03:01:22] (Speed: 45)
[03:01:22] (Speed: 44)
[03:01:22] (Speed: 43)
Thanks!
Re: static variable works, but not the 'new' -
Beckett - 18.07.2015
Seems like OnPlayerUpdate wasn't the right place for this, created a timer which calculates the speed every 500ms fixed it.
Solved.
Re: static variable works, but not the 'new' -
SickAttack - 18.07.2015
It's your own fault for placing it wrong (vid is set after getting the speed).
pawn Код:
new str[7], Float:speed = GetVehicleSpeed(GetPlayerVehicleID(playerid), false);
format(str, sizeof(str), "%i MPH", floatround(speed));
PlayerTextDrawSetString(playerid, Speed[playerid], str);
Static makes the value persist.
Re: static variable works, but not the 'new' -
Beckett - 18.07.2015
Quote:
Originally Posted by SickAttack
It's your own fault for placing it wrong (vid is set after getting the speed).
pawn Код:
new str[7], Float:speed = GetVehicleSpeed(GetPlayerVehicleID(playerid), false); format(str, sizeof(str), "%i MPH", floatround(speed)); PlayerTextDrawSetString(playerid, Speed[playerid], str);
Static makes the value persist.
|
Yeah I've noticed that, thanks for the reply though.