Variable name from string?
#1

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

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;
}
Reply
#3

Thanks you.
Reply


Forum Jump:


Users browsing this thread: 3 Guest(s)