Getting numeric value from a variable name formatted into a string
#1

I'm not sure how else to word it, so I'll just show what I'm trying to do with an example.

pawn Код:
//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.

pawn Код:
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?
Reply
#2

You're overthinking it:
PHP код:
#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"playeridPlayerInfo[playerid][pAdmin]);
    
Increase_Data(playeridpAdmin);
    
printf("[AFTER] Player[%d][pAdmin] == %d"playeridPlayerInfo[playerid][pAdmin]);
}
Increase_Data(playeridE_PLAYER_DATA:enumerator) {
    
PlayerInfo[playerid][enumerator] ++;
    return 
true;

Reply
#3

I don't think that's what I'm after, let me show a clearer example that I thought of.

pawn Код:
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".
Reply
#4

You can stringify it by placing a hashtag in front of it (utilises the preprocessor):
PHP код:
#include <a_samp>
enum E_PLAYER_DATA {
    
    
pAdmin,
    
pLevel
};
new 
PlayerInfo[MAX_PLAYERS][E_PLAYER_DATA];
main() {
    new
        
playerid 10
    
;
    
Display_Data(playeridpAdmin#pAdmin);
    
Display_Data(playeridpLevel#pLevel);
}
Display_Data(playeridE_PLAYER_DATA:enumeratorstringified[]) {
    new 
string[128];
    
format(string,sizeof(string),"The value of %s is %i."stringifiedPlayerInfo[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.
Reply
#5

Assuming stringify means turning something into a string, how would you de-stringify a string into plain text to the script?
Reply
#6

Quote:
Originally Posted by DTV
Посмотреть сообщение
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.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)