SA-MP Forums Archive
Variable name from string? - 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: Variable name from string? (/showthread.php?tid=382611)



Variable name from string? - Whizion - 04.10.2012

Let's say i have 5 variables, and i want to change values in them...

pawn Код:
PlayerInfo[playerid][pSomething1];
PlayerInfo[playerid][pSomething2];
PlayerInfo[playerid][pSomething3];
PlayerInfo[playerid][pSomething4];
PlayerInfo[playerid][pSomething5];
How would i do this better and with less lines?

pawn Код:
if(!strcmp(method, "pSomething1", true)) { code for changing pSomething1 }
if(!strcmp(method, "pSomething2", true)) { code for changing pSomething2 }
if(!strcmp(method, "pSomething3", true)) { code for changing pSomething3 }
if(!strcmp(method, "pSomething4", true)) { code for changing pSomething4 }
if(!strcmp(method, "pSomething5", true)) { code for changing pSomething5 }
I curently have much more than only 5 variables and it's a pain to do it like shown above.

So is there a better way? Something like:

pawn Код:
PlayerInfo[playerid][p%s]
So i don't have to have so many if's?

Thank you.


Re: Variable name from string? - Roel - 04.10.2012

Use a loop and an array
for exampe:
in your PlayerInfo enum you but:

pSomething[10],

Then somewhere in your code:
pawn Код:
for(new i = 0; i < 10; i++)
{
  psomeThing[i] = yourvaleu;
}



Re: Variable name from string? - Whizion - 04.10.2012

Thanks you.