How to load a .ini file when the server starts.
#1

Hey guys, i was wondering how i could go about loading a .ini
Heres my code.

pawn Код:
CreatePayPhone(Float:XPos, Float:YPos, Float:ZPos)
{
    PhoneCount ++;
    PayPhoneInfo[PhoneCount][PhoneX] = XPos;
    PayPhoneInfo[PhoneCount][PhoneY] = YPos;
    PayPhoneInfo[PhoneCount][PhoneZ] = ZPos;
    PayPhonePickup[PhoneCount] = CreatePickup(PayPhoneObjectID, PickupType, XPos, YPos, ZPos, -1);
    return PhoneCount;
}
COMMAND:createpayphone(playerid, params[])
{
    if(IsPlayerAdmin(playerid))
    {
        new string[100],string1[100],Float:x,Float:y,Float:z;
        GetPlayerPos(playerid, x,y,z);
        format(string1,sizeof(string1),"%i",dini_Int("PayPhones.ini","amount"));
        format(string,sizeof(string),"CreatePayPhone(%d,%d,%d);",x,y,z);
        dini_Set("PayPhones.ini",string1,string);
        format(string1,sizeof(string1),"PayPhone created, a total of %i payphones have been created.",dini_Int("PayPhones.ini","amount"));
        SendClientMessage(playerid, 0xFFFFFFF,string1);
        dini_IntSet("PayPhones.ini","amount",dini_Int("PayPhones.ini","amount")+1);
        CreatePayPhone(x,y,z);
    }
    else
    {
        SendClientMessage(playerid, 0xFFFFFF,"You're not an admin.");
    }
    return 1;
}
Everything saves into the file perfectly fine in the format below.
pawn Код:
amount=12
0=CreatePayPhone(-988334941,1144270702,1108129514);
1=CreatePayPhone(-988347416,1144314919,1108344770);
2=CreatePayPhone(-988402683,1144501376,1108086784);
3=CreatePayPhone(-988377260,1144279794,1108940800);
4=CreatePayPhone(-988379952,1144256353,1108946168);
5=CreatePayPhone(-988384768,1144162005,1108303087);
6=CreatePayPhone(-988384150,1144043437,1108086784);
7=CreatePayPhone(-988298045,1144253563,1108301880);
8=CreatePayPhone(-988317700,1144131695,1108291422);
9=CreatePayPhone(-988346780,1144012484,1108298220);
10=CreatePayPhone(-988371530,1144325988,1108828150);
11=CreatePayPhone(-988356176,1144324023,1108704160);
then under OnFliterScriptInIt i need to load the payphones but i'm not sure on how it would be done.
pawn Код:
public OnFilterScriptInit()
{
        //loading here.
    return 1;
}
cheers to anybody whos willing to help out.
Reply
#2

Код:
Check if line exists first:
dini_Isset(filename[],key[]);
Then load
dini_Float(filename[],key[]);
And i suggest you use this code to save the payphones:

Код:
COMMAND:createpayphone(playerid, params[])
{
    if(IsPlayerAdmin(playerid)) 
    {
        new string[100],string1[100],Float:x,Float:y,Float:z;
        GetPlayerPos(playerid, x,y,z);
        format(string1,sizeof(string1),"%i",dini_Float("PayPhones.ini","amount"));
        format(string,sizeof(string),"CreatePayPhone(%f,%f,%f);",x,y,z);
        dini_FloatSet("PayPhones.ini",string1,string);
        format(string1,sizeof(string1),"PayPhone created, a total of %i payphones have been created.",dini_Int("PayPhones.ini","amount"));
        SendClientMessage(playerid, 0xFFFFFFF,string1);
        dini_IntSet("PayPhones.ini","amount",dini_Int("PayPhones.ini","amount")+1);
        CreatePayPhone(x,y,z);
    }
    else
    {
        SendClientMessage(playerid, 0xFFFFFF,"You're not an admin.");
    }
    return 1;
}
Reply
#3

ah cheers, do you think you could explain more about the loading.
like what is Key[]
Reply
#4

My edited version with loading function:
pawn Код:
#define PAYPHONES "payphones.cfg"

public OnGameModeInit()
{
    LoadPayPhones(PAYPHONES);
    return 1;
}

CreatePayPhone(Float:XPos, Float:YPos, Float:ZPos)
{
    PhoneCount ++;
    PayPhoneInfo[PhoneCount][PhoneX] = XPos;
    PayPhoneInfo[PhoneCount][PhoneY] = YPos;
    PayPhoneInfo[PhoneCount][PhoneZ] = ZPos;
    PayPhonePickup[PhoneCount] = CreatePickup(PayPhoneObjectID, PickupType, XPos, YPos, ZPos, -1);
    return PhoneCount;
}

stock LoadPayPhones(FileName[])
{
    if(!fexist(FileName)) return 0;

    new
        File:PPhone,
        Float:PLoc[3],
        pTotal,
        Line[60];

    PPhone = fopen(FileName, io_read);
    while(fread(PPhone , Line))
    {
        unformat(Line, "p<|>fff", PLoc[0], PLoc[1], PLoc[2]);
        CreatePayPhone(PLoc[0], PLoc[1], PLoc[2]);

        pTotal++;
    }
    fclose(PPhone);
    printf("%i Pay Phones loaded", pTotal);
    return pTotal;
}

COMMAND:createpayphone(playerid, params[])
{
    if(IsPlayerAdmin(playerid))
    {
        if(!fexist(PAYPHONES)) return SendClientMessage(playerid, COLOR_RED, "Cannot find the PayPhone file!");

        new
        File:PayPhone = fopen(PAYPHONES, io_append),
        Float:Pos[3],
        gStr[60];

        GetPlayerPos(playerid, Pos[0], Pos[1], Pos[2]);

        format(gStr, sizeof(gStr), "%.2f|%.2f|%.2f\r\n",
        Pos[0],
        Pos[1],
        Pos[2]);

        fwrite(PayPhone, gStr);
        fclose(PayPhone);
        SendClientMessage(playerid, COLOR_TEAL, "A new Pay Phone has been created.");
        return 1;
    }
    else return 0;
}
The new writing format will look like this:
pawn Код:
locationx|locationy|locationz
Reply
#5

cheers, don't have to use dini now, although i don't get this error here.

undefined symbol "unformat"
pawn Код:
stock LoadPayPhones(FileName[])
{
    if(!fexist(FileName)) return 0;

    new
        File:PPhone,
        Float:PLoc[3],
        pTotal,
        Line[60];

    PPhone = fopen(FileName, io_read);
    while(fread(PPhone , Line))
    {
        unformat(Line, "p<|>fff", PLoc[0], PLoc[1], PLoc[2]); //line 202
        CreatePayPhone(PLoc[0], PLoc[1], PLoc[2]);

        pTotal++;
    }
    fclose(PPhone);
    printf("%i Pay Phones loaded", pTotal);
    return pTotal;
}
Reply
#6

Just rename it 'sscanf'
Reply
#7

Hey man, says the payphones load, but they don't appear ingame. any ideas?
Reply
#8

Here:
pawn Код:
#define PAYPHONES "payphones.cfg"
#define MAX_PAYPHONES   10

#define PayPhoneObjectID    1216
#define PickupType  1

enum PayPhones
{
    Float:PhoneX,
    Float:PhoneY,
    Float:PhoneZ
}
new PayPhoneInfo[MAX_PAYPHONES][PayPhones];
new PayPhonePickup[MAX_PAYPHONES];
new PhoneCount = 0;

public OnFilterScriptInit()
{
    LoadPayPhones(PAYPHONES);
    return 1;
}

CreatePayPhone(Float:XPos, Float:YPos, Float:ZPos)
{
    PhoneCount ++;
    PayPhoneInfo[PhoneCount][PhoneX] = XPos;
    PayPhoneInfo[PhoneCount][PhoneY] = YPos;
    PayPhoneInfo[PhoneCount][PhoneZ] = ZPos;
    PayPhonePickup[PhoneCount] = CreatePickup(PayPhoneObjectID, PickupType, XPos, YPos, ZPos, -1);
    return PhoneCount;
}

stock LoadPayPhones(FileName[])
{
    if(!fexist(FileName)) return 0;

    new
        File:PPhone,
        Float:PLoc[3],
        pTotal,
        Line[60];

    PPhone = fopen(FileName, io_read);
    while(fread(PPhone , Line))
    {
        sscanf(Line, "p<|>fff", PLoc[0], PLoc[1], PLoc[2]);
        CreatePayPhone(PLoc[0], PLoc[1], PLoc[2]);

        pTotal++;
    }
    fclose(PPhone);
    printf("%i Pay Phones loaded", pTotal);
    return pTotal;
}

CMD:cpp(playerid, params[])
{
    if(IsPlayerAdmin(playerid))
    {
        if(!fexist(PAYPHONES)) return SendClientMessage(playerid, COLOR_RED, "Cannot find the PayPhone file!");

        new
        File:PayPhone = fopen(PAYPHONES, io_append),
        Float:Pos[3],
        gStr[60];

        GetPlayerPos(playerid, Pos[0], Pos[1], Pos[2]);

        format(gStr, sizeof(gStr), "%.2f|%.2f|%.2f\r\n",
        Pos[0],
        Pos[1],
        Pos[2]);
       
        CreatePayPhone(Pos[0], Pos[1], Pos[2]);

        fwrite(PayPhone, gStr);
        fclose(PayPhone);
        SendClientMessage(playerid, COLOR_TEAL, "A new Pay Phone has been created.");
        return 1;
    }
    else return 0;
}
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)