PDA

View Full Version : Getting numeric value from a variable name formatted into a string


DTV
21/03/2017, 03:39 AM
I'm not sure how else to word it, so I'll just show what I'm trying to do with an example.


//Let's say I had a var called PlayerInfo[playerid][pAdmin] and it equals 2.

function(blah[]) //blah in this case would be "pAdmin"
{
new string[128],stat[32];
format(stat,sizeof(stat),"PlayerInfo[playerid][%s]",blah); //at this point the string should read as "PlayerInfo[playerid][pAdmin]"
format(string,sizeof(string),"The value of this stat is %i.",stat); //here's where the issue is since stat is a string and not an integer
SendClientMessage(playerid,-1,string);
}


Basically in that example, what I'm trying to do is have is the stat string acted like I typed in the variable there.


format(string,sizeof(string),"The value of this stat is %i.",PlayerInfo[playerid][pAdmin]);


I'm not sure if I'm just overthinking this or not, but is there a solution to this?

AndySedeyn
21/03/2017, 03:46 AM
You're overthinking it:

#include <a_samp>

enum E_PLAYER_DATA {

pAdmin
};
new PlayerInfo[MAX_PLAYERS][E_PLAYER_DATA];

main() {

new
playerid = 10
;

printf("[BEFORE] Player[%d][pAdmin] == %d", playerid, PlayerInfo[playerid][pAdmin]);
Increase_Data(playerid, pAdmin);
printf("[AFTER] Player[%d][pAdmin] == %d", playerid, PlayerInfo[playerid][pAdmin]);
}

Increase_Data(playerid, E_PLAYER_DATA:enumerator) {

PlayerInfo[playerid][enumerator] ++;
return true;
}

DTV
21/03/2017, 03:57 AM
I don't think that's what I'm after, let me show a clearer example that I thought of.


function(blah[]) //still pAdmin
{
new string[128],stat[32];
format(stat,sizeof(stat),"PlayerInfo[playerid][%s]",blah);
format(string,sizeof(string),"The value of %s is %i.",blah,stat);
SendClientMessage(playerid,-1,string);
return 1;
}


I don't want to do anything to change the value, I just want the script to think I typed "PlayerInfo[playerid][pAdmin]" rather than "stat".

AndySedeyn
21/03/2017, 04:06 AM
You can stringify it by placing a hashtag in front of it (utilises the preprocessor):

#include <a_samp>

enum E_PLAYER_DATA {

pAdmin,
pLevel
};
new PlayerInfo[MAX_PLAYERS][E_PLAYER_DATA];

main() {

new
playerid = 10
;

Display_Data(playerid, pAdmin, #pAdmin);
Display_Data(playerid, pLevel, #pLevel);
}

Display_Data(playerid, E_PLAYER_DATA:enumerator, stringified[]) {

new string[128];
format(string,sizeof(string),"The value of %s is %i.", stringified, PlayerInfo[playerid][enumerator]);
print(string);
return true;
}


prints:

[04:57:52] The value of pAdmin is 0.
[04:57:52] The value of pLevel is 0.


Using #enumerator inside the function doesn't seem to work because it stringifies what you put after it, hence why I do it in the function call. But at that point, you could just do "pLevel" and stuff.

That's what I understand from your examples.

DTV
21/03/2017, 04:17 AM
Assuming stringify means turning something into a string, how would you de-stringify a string into plain text to the script?

AndySedeyn
21/03/2017, 04:19 AM
Assuming stringify means turning something into a string, how would you de-stringify a string into plain text to the script?

You can't, turning it into plain text would make it a variable name, function,... That's why I added the second parameter to that function.