Saving File
#1

Hey guys i finally got my dynamic vehicle system working, the only problem is the way the file is saved.

I want the way it saves is that it goes to file and makes lines for each car, example:

pawn Код:
Carmodel | Posx | Posy | Posz | Angle | Color1 | Color2
Carmodel | Posx | Posy | Posz | Angle | Color1 | Color2
Carmodel | Posx | Posy | Posz | Angle | Color1 | Color2
The way it saves now is
pawn Код:
Model=411
Location_X=1796.386718
Location_Y=-1920.190429
Location_Z=13.392473
Angle=90.000000
Color_1=3
Color_2=3
Here is my code for saving
pawn Код:
format(file, sizeof(file), "NLRP/Vehicles/%d.ini", i);
            dini_Create(file);
            dini_IntSet(file,"Model",CarInfo[i][cModel]);
            dini_FloatSet(file,"Location_X",CarInfo[i][cLocationx]);
            dini_FloatSet(file,"Location_Y",CarInfo[i][cLocationy]);
            dini_FloatSet(file,"Location_Z",CarInfo[i][cLocationz]);
            dini_FloatSet(file,"Angle",CarInfo[i][cAngle]);
            dini_IntSet(file,"Color_1",CarInfo[i][cColorTwo]);
            dini_IntSet(file,"Color_2",CarInfo[i][cColorOne]);
I think i need to use format but can someone help out, thanks.
Reply
#2

You could use LoadStaticVehiclesFromFile function from gl_common library included in server package. Anyway, I think this will work:

Saving:
pawn Код:
new string[128], VehicleID[16]; // outside the loop (I guess the code below is in loop because of "i" variable)
format(file, sizeof(file), "NLRP/Vehicles/%d.ini", i);

format(string, sizeof(string), "%i | %f | %f | %f | %f | %i | %i", CarInfo[i][cModel], CarInfo[i][cLocationx], CarInfo[i][cLocationy], CarInfo[i][cLocationz], CarInfo[i][cAngle], CarInfo[i][cColorTwo], CarInfo[i][cColorOne]);

dini_Create(file);

format(VehicleID, sizeof(VehicleID), "Vehicle %i", i);
dini_Set(file, VehicleID, string);
Loading (you'll need sscanf)
pawn Код:
new string[128], VehicleID[16]; // outside the loop (I guess the code below is in loop because of "i" variable)
format(file, sizeof(file), "NLRP/Vehicles/%d.ini", i);

format(VehicleID, sizeof(VehicleID), "Vehicle %i", i);
format(string, sizeof(string), "%s", dini_Get(file, VehicleID));

sscanf(string, "s[16]p<|>ifffii", CarInfo[i][cModel], CarInfo[i][cLocationx], CarInfo[i][cLocationy], CarInfo[i][cLocationz], CarInfo[i][cAngle], CarInfo[i][cColorTwo], CarInfo[i][cColorOne]);
Reply
#3

I was told to use ini, should i use cfg? I would use mysql but since this is my first time scripting my own stuff i would rather use something i know. I will attempt mysql and stuff later on. Also i should replace my loading with yours?

pawn Код:
public LoadCar()
{
    new file[512];
    for(new idx = 1; idx < sizeof(CarInfo) ; idx++)
    {
        format(file, sizeof(file),"NLRP/Vehicles/cars.ini", idx);
        if(dini_Exists(file))
        {
            CarInfo[idx][cModel] = dini_Int(file,"Model");
            CarInfo[idx][cLocationx] = dini_Float(file,"Location_X");
            CarInfo[idx][cLocationy] = dini_Float(file,"Location_Y");
            CarInfo[idx][cLocationz] = dini_Float(file,"Location_Z");
            CarInfo[idx][cAngle] = dini_Float(file,"Angle");
            CarInfo[idx][cColorOne] = dini_Int(file,"Color_1");
            CarInfo[idx][cColorTwo] = dini_Int(file,"Color_2");
            strmid(CarInfo[idx][cOwner], dini_Get(file,"Owner"), 0, strlen(dini_Get(file,"Owner")), 255);
            CarInfo[idx][cOwned] = dini_Int(file,"Owned");
            CarInfo[idx][cLock] = dini_Int(file,"Locked");
            CarInfo[idx][cPaintjob] = dini_Int(file,"Paintjob");
            CarInfo[idx][cVirWorld] = dini_Int(file,"VirtualWorld");
            CarInfo[idx][cComponent0] = dini_Int(file,"Component0");
            CarInfo[idx][cComponent1] = dini_Int(file,"Component1");
            CarInfo[idx][cComponent2] = dini_Int(file,"Component2");
            CarInfo[idx][cComponent3] = dini_Int(file,"Component3");
            CarInfo[idx][cComponent4] = dini_Int(file,"Component4");
            CarInfo[idx][cComponent5] = dini_Int(file,"Component5");
            CarInfo[idx][cComponent6] = dini_Int(file,"Component6");
            CarInfo[idx][cComponent7] = dini_Int(file,"Component7");
            CarInfo[idx][cComponent8] = dini_Int(file,"Component8");
            CarInfo[idx][cComponent9] = dini_Int(file,"Component9");
            CarInfo[idx][cComponent10] = dini_Int(file,"Component10");
            CarInfo[idx][cComponent11] = dini_Int(file,"Component11");
            CarInfo[idx][cComponent12] = dini_Int(file,"Component12");
            CarInfo[idx][cComponent13] = dini_Int(file,"Component13");
        }
    }
    print("[SCRIPT]: Loaded Cars");
    return 1;
}
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)