ParseFile bug +rep
#1

I am facing a problem with INI_ParseFile, whenever I use it within a CMD.

PHP код:
CMD:giveoffmoney(playeridparams[])
{
    if(
PlayerInfo[playerid][pAdmin] < 1) return 0;
       if(
PlayerInfo[playerid][pAdmin] < 4) return SendClientMessage(playeridCOLOR_YELLOWGREEN"Error: You are not authorized to use this command.");
    new 
string[128], user[50], amount;
    if(
sscanf(params"s[50]i"useramount)) return SendClientMessage(playeridCOLOR_ORANGE"Usage: /giveoffmoney [playername] [amount]");
    
format(stringsizeof string"/Users/%s.txt"user);
    if(!
fexist(string)) return SendClientMessage(playeridCOLOR_RED"Error: The player has not been found in our database!");
    
INI_ParseFile(string"LoadPlayerData_user", .bExtra true, .extra playerid);
    new 
INI:File INI_Open(string);
    
INI_SetTag(File"PlayerData");
    
INI_WriteInt(File"Money"PlayerInfo[playerid][pMoney] + amount);
    
INI_Close(File);
    
format(stringsizeof string"You have given '%s' $%i. Player '%s' has now $%i dollars."useramountuserPlayerInfo[playerid][pMoney] + amount);
    
SendClientMessage(playerid, -1string);
    return 
1;

The problem is, the stats of the [playername] are transferring to me. It's kinda hard to explain...
----
For example: Michael is admin level 3 and Sarah is admin level 0.
Michael uses the CMD: /giveoffmoney Sarah 500
after committing this, Michael receives the STATS of Sarah, so Michael's admin level is 0 now.
+rep.
Reply
#2

Thats because you are passing playerid in INI_ParseFile, which is admin id as playerid is the id of player who typed the command not id of player for which the command was typed. You are passing the Player name who has to be given the money and admin id in ParseFile which leads to loading of stats of the player who the money has to be given(Sarah) in to the array slot which is of admin(Micheal).
Also you would need to check if player is already online or not if player is already online then you need not to load his/her stats separately.
Reply
#3

Quote:
Originally Posted by BroZeus
Посмотреть сообщение
Also you would need to check if player is already online or not if player is already online then you need not to load his/her stats separately.
Thanks for the tip, I'll fix that to check whether the player is online or not.

Quote:
Originally Posted by BroZeus
Посмотреть сообщение
Thats because you are passing playerid in INI_ParseFile, which is admin id as playerid is the id of player who typed the command not id of player for which the command was typed. You are passing the Player name who has to be given the money and admin id in ParseFile which leads to loading of stats of the player who the money has to be given(Sarah) in to the array slot which is of admin(Micheal).
But how could I possibly change/avoid this... because when I use for example 'offlineplayer' as parameter it returns into errors.
Reply
#4

Firstly declare PlayerInfo like this:
Quote:

new PlayerInfo[ MAX_PLAYERS + 1]..

Now there is a extra slot in PlayerInfo, so use this slot to load offline player info in it.
Example:
PHP код:
    INI_ParseFile(string"LoadPlayerData_user", .bExtra true, .extra MAX_PLAYERS); // use maxplayers as .extra here: 
    
new INI:File INI_Open(string); 
    
INI_SetTag(File"PlayerData"); 
    
INI_WriteInt(File"Money"PlayerInfo[MAX_PLAYERS][pMoney] + amount); //max players here too
    
INI_Close(File); 
    
format(stringsizeof string"You have given '%s' $%i. Player '%s' has now $%i dollars."useramountuserPlayerInfo[MAX_PLAYERS][pMoney] + amount);// here too 
    
SendClientMessage(playerid, -1string); 
    return 
1

See how i replaced playerid with MAX_PLAYERS in above code.
Reply
#5

Thanks it is solved. Can you give me a little explenation of the 'MAX_PLAYERS + 1' and how it does recognize a offline player ?
+repped
Reply
#6

Initializing array with size MAX_PLAYERS + 1 will obviously increase array length by 1. Now in this extra array slot we just load an offline player info rather than loading in a slot where an online player info is stored.

Lets say you have max_players defined as 10 so array size will be from 0-9 and when player id 0 logs in and he is admin so id 0 info will load in PlayerInfo[0][]. And according to your earlier code when id 0(admin) typed the give away money code the offline player info will load in PlayerInfo[0][] in which admin info is already there thus their stats were changed.

Now when you declare array with max_players + 1 it has indexes from 0 - 10 and you are using max_player as index to load offline player info so the offline player info will load in PlayerInfo[10][] in which no one info is there thus no bug.
Reply
#7

I see, that's pretty smart tho.
Thanks again man.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)