SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help

Reply
 
Thread Tools Display Modes
Old 21/03/2017, 03:39 AM   #1
DTV
Huge Clucker
 
DTV's Avatar
 
Join Date: Dec 2010
Posts: 417
Reputation: 41
Default Getting numeric value from a variable name formatted into a string

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

Code:
//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.

Code:
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?
DTV is offline   Reply With Quote
Old 21/03/2017, 03:46 AM   #2
AndySedeyn
High-roller
 
AndySedeyn's Avatar
 
Join Date: Feb 2014
Location: Belgium
Posts: 1,354
Reputation: 681
Default Re: Getting numeric value from a variable name formatted into a string

You're overthinking it:
PHP Code:
#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;

AndySedeyn is offline   Reply With Quote
Old 21/03/2017, 03:57 AM   #3
DTV
Huge Clucker
 
DTV's Avatar
 
Join Date: Dec 2010
Posts: 417
Reputation: 41
Default Re: Getting numeric value from a variable name formatted into a string

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

Code:
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".
DTV is offline   Reply With Quote
Old 21/03/2017, 04:06 AM   #4
AndySedeyn
High-roller
 
AndySedeyn's Avatar
 
Join Date: Feb 2014
Location: Belgium
Posts: 1,354
Reputation: 681
Default Re: Getting numeric value from a variable name formatted into a string

You can stringify it by placing a hashtag in front of it (utilises the preprocessor):
PHP Code:
#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:
Code:
[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.
AndySedeyn is offline   Reply With Quote
Old 21/03/2017, 04:17 AM   #5
DTV
Huge Clucker
 
DTV's Avatar
 
Join Date: Dec 2010
Posts: 417
Reputation: 41
Default Re: Getting numeric value from a variable name formatted into a string

Assuming stringify means turning something into a string, how would you de-stringify a string into plain text to the script?
DTV is offline   Reply With Quote
Old 21/03/2017, 04:19 AM   #6
AndySedeyn
High-roller
 
AndySedeyn's Avatar
 
Join Date: Feb 2014
Location: Belgium
Posts: 1,354
Reputation: 681
Default Re: Getting numeric value from a variable name formatted into a string

Quote:
Originally Posted by DTV View Post
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.
AndySedeyn is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Numeric detection limit on a string. +REP Harty Scripting Help 12 25/03/2016 07:03 PM
[help] Need a define that returns a formatted string DeathTone Scripting Help 6 08/05/2012 10:50 PM
Compile error(warning 219: local variable "string" shadows a variable at a preceding level) calin1996 Scripting Help 6 25/04/2012 03:13 PM
Make string to numeric kevinsoftware Help Archive 5 05/04/2009 09:48 AM


All times are GMT. The time now is 03:56 PM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.