SA-MP Forums Archive
Function That Reads from .INI - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+---- Forum: Help Archive (https://sampforum.blast.hk/forumdisplay.php?fid=89)
+---- Thread: Function That Reads from .INI (/showthread.php?tid=277627)

Pages: 1 2


[STILL UNSOLVED]Function That Reads from .INI - Tigerbeast11 - 18.08.2011

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


Re: Function That Reads from .INI - Jefff - 18.08.2011

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"));



Re: Function That Reads from .INI - Tigerbeast11 - 18.08.2011

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...


Re: Function That Reads from .INI - [MWR]Blood - 18.08.2011

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"));



Re: Function That Reads from .INI - Tigerbeast11 - 18.08.2011

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



Re: Function That Reads from .INI - Jefff - 18.08.2011

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.


Re: Function That Reads from .INI - Tigerbeast11 - 18.08.2011

It still crashes


Re: Function That Reads from .INI - [MWR]Blood - 18.08.2011

Where are you adding it?


Re: Function That Reads from .INI - Tigerbeast11 - 18.08.2011

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"));



Re: Function That Reads from .INI - Tigerbeast11 - 18.08.2011

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"



Re: Function That Reads from .INI - [MWR]Blood - 18.08.2011

You should put it where you want to use it...


Re: Function That Reads from .INI - Tigerbeast11 - 18.08.2011

I still get those errors, read my previous post


Re: Function That Reads from .INI - Jefff - 18.08.2011

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


Re: Function That Reads from .INI - Tigerbeast11 - 18.08.2011

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...


Re: Function That Reads from .INI - Jefff - 18.08.2011

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


Re: Function That Reads from .INI - Tigerbeast11 - 18.08.2011

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


Re: Function That Reads from .INI - Jefff - 19.08.2011

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;
}



Re: Function That Reads from .INI - Tigerbeast11 - 19.08.2011

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"



Re: Function That Reads from .INI - Kingunit - 19.08.2011

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.


Re: Function That Reads from .INI - Tigerbeast11 - 19.08.2011

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;
}