Function That Reads from .INI
#1

Hi!

I have looked at wiki and found the "how to read .ini files" but I didn't know how to make a function which I could use in my GM. This is an example of my .ini file:
Code:
IP=127.0.0.1
Registered=1
Level=10
Cash=180000000
Kills=150000
Deaths=0
Password=237634235
Wired=0
WiredWarnings=0
Jailed=0
VIP=0
LoggedIn=1
I want to check whether VIP = 1 or not. How can I make a function? Please would anyone mind guiding me through it? Thanks in advance
Reply
#2

pawn Code:
stock INI_Get(filename[],key[])
{
    new File:F,string[128];
    new sname[24],sval[24];
    F = fopen(filename,io_read);
    if(!F) return sname;
    while(fread(F,string))
    {
        sscanf(string,"p<=>s[24]s[24]",sname,sval);
        if(!strcmp(sname,key)) {
            sval[strlen(sval)-2] = 0;
            fclose(F);
            return sval;
        }
    }
    fclose(F);
    sname[0] = '\0';
    return sname;
}
Usage:
pawn Code:
new value = strval(INI_Get("filename.ini","VIP"));
Reply
#3

How would I set this: new value = strval(INI_Get("filename.ini","VIP"));
to: new value = strval(INI_Get("Tigerbeast11.ini","VIP"));

Because my .ini files are based on names of players...
Reply
#4

Use a string?
pawn Code:
new file[64], pName[MAX_PLAYER_NAME];
GetPlayerName(playerid,pName,sizeof(pName));
format(file,sizeof(file),"%s.ini",pName);
new value = strval(INI_Get(file,"VIP"));
Reply
#5

Pawn Compiler crashes when I insert this part:
pawn Code:
new value = strval(INI_Get("filename.ini","VIP"));
Reply
#6

pawn Code:
new nick[30];
GetPlayerName(playerid,nick,24);
format(nick,sizeof(nick),"%s.ini",nick);
new value = strval(INI_Get(nick,"VIP"));
EDIT: Too late
DOWN: Works great for me.
Reply
#7

It still crashes
Reply
#8

Where are you adding it?
Reply
#9

Quote:
Originally Posted by Delux13
View Post
Where are you adding it?
Underneath all the rest of the code:
pawn Code:
stock INI_Get(filename[],key[])
{
    new File:F,string[128];
    new sname[24],sval[24];
    F = fopen(filename,io_read);
    if(!F) return sname;
    while(fread(F,string))
    {
        sscanf(string,"p<=>s[24]s[24]",sname,sval);
        if(!strcmp(sname,key))
        {
            sval[strlen(sval)-2] = 0;
            fclose(F);
            return sval;
        }
    }
    fclose(F);
    sname[0] = '\0';
    return sname;
}

new nick[30];
GetPlayerName(playerid,nick,24);
format(nick,sizeof(nick),"%s.ini",nick);
new value = strval(INI_Get(nick,"VIP"));
Reply
#10

EDIT: I've changed it and the compiler doesn't crash but it gives me these errors:

pawn Code:
stock INI_Get(filename[],key[])
{
    new File:F,string[128];
    new sname[24],sval[24];
    F = fopen(filename,io_read);
    if(!F) return sname;
    while(fread(F,string))
    {
        sscanf(string,"p<=>s[24]s[24]",sname,sval); //866
        if(!strcmp(sname,key))
        {
            sval[strlen(sval)-2] = 0;
            fclose(F);
            return sval;
           
            new nick[30];//873
            GetPlayerName(playerid,nick,24);
            format(nick,sizeof(nick),"%s.ini",nick);
            new value = strval(INI_Get(nick,"VIP"));
        }
    }
    fclose(F);
    sname[0] = '\0';
    return sname;
}
Code:
OSK-TDM.pwn(866) : error 017: undefined symbol "sscanf"
OSK-TDM.pwn(873) : warning 225: unreachable code
OSK-TDM.pwn(874) : error 017: undefined symbol "playerid"
OSK-TDM.pwn(876) : warning 204: symbol is assigned a value that is never used: "value"
Reply
#11

You should put it where you want to use it...
Reply
#12

I still get those errors, read my previous post
Reply
#13

Quote:
Originally Posted by Tigerbeast11
View Post
I want to check whether VIP = 1 or not.
Where You want check this?
Reply
#14

pawn Code:
public OnPlayerSpawn(playerid)
{
    new nick[30];
    GetPlayerName(playerid,nick,24);
    format(nick,sizeof(nick),"%s.ini",nick);
    new value = strval(INI_Get(nick,"VIP"));
   
    if (value == 1)
    {
        SetPlayerArmour(playerid,100);
    }
    return 1;
}
Code:
VIP=1
But it doesn't work...
Reply
#15

Works for me, where is player account saved?
scriptfiles/nick.ini
or
scriptfiles/folder/nick.ini
?
or where
Reply
#16

Quote:
Originally Posted by Jefff
View Post
Works for me, where is player account saved?
scriptfiles/nick.ini
or
scriptfiles/folder/nick.ini
?
or where
Scriptfiles/XAdmin/nick.ini
Reply
#17

pawn Code:
public OnPlayerSpawn(playerid)
{
    new nick[40];
    GetPlayerName(playerid,nick,24);
    format(nick,sizeof(nick),"/XAdmin/%s.ini",nick);
    new IsVIP = strval(INI_Get(nick,"VIP"));

    if(IsVIP == 1)
    {
        SetPlayerArmour(playerid,100);
    }
    return 1;
}
Reply
#18

My server window gives me this error... It doesn't load the GM. This is the error:
Code:
Run time error 19: "File or function is not found"
Reply
#19

Quote:
Originally Posted by Tigerbeast11
View Post
My server window gives me this error... It doesn't load the GM. This is the error:
Code:
Run time error 19: "File or function is not found"
That means there is something wrong with your plugins / includes.
Reply
#20

It doesn't give me that error when I get rid of the last bit of code...
pawn Code:
public OnPlayerSpawn(playerid)
{
    new nick[30];
    GetPlayerName(playerid,nick,24);
    format(nick,sizeof(nick),"%s.ini",nick);
    new value = strval(INI_Get(nick,"VIP"));
   
    if (value == 1)
    {
        SetPlayerArmour(playerid,100);
    }
    return 1;
}
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)