SA-MP Forums Archive
Cars not saving? - 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)
+--- Thread: Cars not saving? (/showthread.php?tid=611023)



Cars not saving? - MayaEU - 01.07.2016

Hello, can anyone tell me why the cars doesnt save after server has restarted?

pawn Код:
forward SaveCars();
public SaveCars()
{
    new idx;
    new File: file2 = fopen("cars.cfg", io_append);
    while (idx < sizeof(CarInfo))
    {
        new coordsstring[800];  //                   0  1  2  4  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
        format(coordsstring, sizeof(coordsstring), "%d,%f,%f,%f,%f,%d,%d,%s,%s,%d,%s,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%i,%i,%i,%i,%i,%i,%i,%i,%f,%i,%d,%i,%i,%i,%i,%i,%i,%i,%d,%d\n",
        CarInfo[idx][cModel], // 0
        CarInfo[idx][cLocationx], // 1
        CarInfo[idx][cLocationy], // 2
        CarInfo[idx][cLocationz],  // 3
        CarInfo[idx][cAngle],  //4
        CarInfo[idx][cColorOne], //5
        CarInfo[idx][cColorTwo], //6
        CarInfo[idx][cOwner], //7
        CarInfo[idx][cDescription], //8
        CarInfo[idx][cValue], //9
        CarInfo[idx][cPlate], //10
        CarInfo[idx][cOwnedCar], //11
        CarInfo[idx][cLock], //12
        CarInfo[idx][cMod][0], //13
        CarInfo[idx][cMod][1], //14
        CarInfo[idx][cMod][2], //15
        CarInfo[idx][cMod][3], //16
        CarInfo[idx][cMod][4], //17
        CarInfo[idx][cMod][5], //18
        CarInfo[idx][cMod][6], //19
        CarInfo[idx][cMod][7], //20
        CarInfo[idx][cMod][8], //21
        CarInfo[idx][cMod][9], //22
        CarInfo[idx][cDonate], //23
        CarInfo[idx][cInsure], //24
        CarInfo[idx][cTrunkWep][1], //25
        CarInfo[idx][cTrunkAmmo][1], //26
        CarInfo[idx][cTrunkWep][2], //27
        CarInfo[idx][cTrunkAmmo][2], //28
        CarInfo[idx][cTrunkWep][3], //29
        CarInfo[idx][cTrunkAmmo][3], //30
        CarInfo[idx][cTrunkWep][4], //31
        CarInfo[idx][cTrunkAmmo][4], //32
        CarInfo[idx][cTrunkArmour], //33
        CarInfo[idx][cTrunkCounter], //34
        CarInfo[idx][cAlarm], //35
        CarInfo[idx][cTrunkPot], //36
        CarInfo[idx][cTrunkCrack], //37
        CarInfo[idx][cTrunkMatsa], //38
        CarInfo[idx][cTrunkMatsb], //39
        CarInfo[idx][cTrunkMatsc], //40
        CarInfo[idx][cImpounded], //41
        CarInfo[idx][cImpoundedPrice], //42
        CarInfo[idx][cDestroys], //43
        CarInfo[idx][cInsurePrice]); //44
        fwrite(file2, coordsstring);
        idx++;
    }
    fclose(file2);
}



Re: Cars not saving? - MayaEU - 01.07.2016

This function works, it saves the cars, but it makes the server lagging very hard, for like 10 seconds

pawn Код:
public SaveCars()
{
    new idx;
    new File: file2;
    while (idx < sizeof(CarInfo))
    {
        new coordsstring[800];  //                   0  1  2  4  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
        format(coordsstring, sizeof(coordsstring), "%d,%f,%f,%f,%f,%d,%d,%s,%s,%d,%s,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%i,%i,%i,%i,%i,%i,%i,%i,%f,%i,%d,%i,%i,%i,%i,%i,%i,%i,%d,%d\n",
        CarInfo[idx][cModel], // 0
        CarInfo[idx][cLocationx], // 1
        CarInfo[idx][cLocationy], // 2
        CarInfo[idx][cLocationz],  // 3
        CarInfo[idx][cAngle],  //4
        CarInfo[idx][cColorOne], //5
        CarInfo[idx][cColorTwo], //6
        CarInfo[idx][cOwner], //7
        CarInfo[idx][cDescription], //8
        CarInfo[idx][cValue], //9
        CarInfo[idx][cPlate], //10
        CarInfo[idx][cOwnedCar], //11
        CarInfo[idx][cLock], //12
        CarInfo[idx][cMod][0], //13
        CarInfo[idx][cMod][1], //14
        CarInfo[idx][cMod][2], //15
        CarInfo[idx][cMod][3], //16
        CarInfo[idx][cMod][4], //17
        CarInfo[idx][cMod][5], //18
        CarInfo[idx][cMod][6], //19
        CarInfo[idx][cMod][7], //20
        CarInfo[idx][cMod][8], //21
        CarInfo[idx][cMod][9], //22
        CarInfo[idx][cDonate], //23
        CarInfo[idx][cInsure], //24
        CarInfo[idx][cTrunkWep][1], //25
        CarInfo[idx][cTrunkAmmo][1], //26
        CarInfo[idx][cTrunkWep][2], //27
        CarInfo[idx][cTrunkAmmo][2], //28
        CarInfo[idx][cTrunkWep][3], //29
        CarInfo[idx][cTrunkAmmo][3], //30
        CarInfo[idx][cTrunkWep][4], //31
        CarInfo[idx][cTrunkAmmo][4], //32
        CarInfo[idx][cTrunkArmour], //33
        CarInfo[idx][cTrunkCounter], //34
        CarInfo[idx][cAlarm], //35
        CarInfo[idx][cTrunkPot], //36
        CarInfo[idx][cTrunkCrack], //37
        CarInfo[idx][cTrunkMatsa], //38
        CarInfo[idx][cTrunkMatsb], //39
        CarInfo[idx][cTrunkMatsc], //40
        CarInfo[idx][cImpounded], //41
        CarInfo[idx][cImpoundedPrice], //42
        CarInfo[idx][cDestroys], //43
        CarInfo[idx][cInsurePrice]); //44
        if(idx == 0)
        {
            file2 = fopen("cars.cfg", io_write);
        }
        else
        {
            file2 = fopen("cars.cfg", io_append);
        }
        fwrite(file2, coordsstring);
        idx++;
        fclose(file2);
    }
}



Re: Cars not saving? - Runn3R - 01.07.2016

Use y_ini instead.


Re: Cars not saving? - MayaEU - 01.07.2016

But, there must be a way to fix this


Re: Cars not saving? - Konstantinos - 01.07.2016

Do I have to mention that you re-open the file 2000 times?

You better have an iterator (foreach/y_iterate) and add the "index" to it so you can loop through existed vehicles only. You might as well use GetVehiclePoolSize()-1 but be careful with that.

Last, check if the file was opened successfully before trying to write to it to prevent server crashes.


Re: Cars not saving? - MayaEU - 01.07.2016

Yes, it keep opening the folder and thats why its lagging, but how to make it open just once?


Re: Cars not saving? - Konstantinos - 01.07.2016

Quote:
Originally Posted by MayaEU
Посмотреть сообщение
Yes, it keep opening the folder and thats why its lagging, but how to make it open just once?
pawn Код:
new File: file2 = fopen("cars.cfg", io_write);
if (!file2) return; // couldn't open file, stop right here

// loop...

fclose(file2);



Re: Cars not saving? - MayaEU - 01.07.2016

Sorry, i dont understand


Re: Cars not saving? - Konstantinos - 01.07.2016

Replace the comment "loop..." with your exact code (while loop) but without those:
pawn Код:
if(idx == 0)
{
    file2 = fopen("cars.cfg", io_write);
}
else
{
    file2 = fopen("cars.cfg", io_append);
}
pawn Код:
fclose(file2);
as they are not used outside of the loop.


Re: Cars not saving? - MayaEU - 01.07.2016

Like this?
pawn Код:
public SaveCars()
{
    new idx;
    new File: file2;
    while (idx < sizeof(CarInfo))
    {
        new coordsstring[800];  //                   0  1  2  4  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
        format(coordsstring, sizeof(coordsstring), "%d,%f,%f,%f,%f,%d,%d,%s,%s,%d,%s,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%i,%i,%i,%i,%i,%i,%i,%i,%f,%i,%d,%i,%i,%i,%i,%i,%i,%i,%d,%d\n",
        CarInfo[idx][cModel], // 0
        CarInfo[idx][cLocationx], // 1
        CarInfo[idx][cLocationy], // 2
        CarInfo[idx][cLocationz],  // 3
        CarInfo[idx][cAngle],  //4
        CarInfo[idx][cColorOne], //5
        CarInfo[idx][cColorTwo], //6
        CarInfo[idx][cOwner], //7
        CarInfo[idx][cDescription], //8
        CarInfo[idx][cValue], //9
        CarInfo[idx][cPlate], //10
        CarInfo[idx][cOwnedCar], //11
        CarInfo[idx][cLock], //12
        CarInfo[idx][cMod][0], //13
        CarInfo[idx][cMod][1], //14
        CarInfo[idx][cMod][2], //15
        CarInfo[idx][cMod][3], //16
        CarInfo[idx][cMod][4], //17
        CarInfo[idx][cMod][5], //18
        CarInfo[idx][cMod][6], //19
        CarInfo[idx][cMod][7], //20
        CarInfo[idx][cMod][8], //21
        CarInfo[idx][cMod][9], //22
        CarInfo[idx][cDonate], //23
        CarInfo[idx][cInsure], //24
        CarInfo[idx][cTrunkWep][1], //25
        CarInfo[idx][cTrunkAmmo][1], //26
        CarInfo[idx][cTrunkWep][2], //27
        CarInfo[idx][cTrunkAmmo][2], //28
        CarInfo[idx][cTrunkWep][3], //29
        CarInfo[idx][cTrunkAmmo][3], //30
        CarInfo[idx][cTrunkWep][4], //31
        CarInfo[idx][cTrunkAmmo][4], //32
        CarInfo[idx][cTrunkArmour], //33
        CarInfo[idx][cTrunkCounter], //34
        CarInfo[idx][cAlarm], //35
        CarInfo[idx][cTrunkPot], //36
        CarInfo[idx][cTrunkCrack], //37
        CarInfo[idx][cTrunkMatsa], //38
        CarInfo[idx][cTrunkMatsb], //39
        CarInfo[idx][cTrunkMatsc], //40
        CarInfo[idx][cImpounded], //41
        CarInfo[idx][cImpoundedPrice], //42
        CarInfo[idx][cDestroys], //43
        CarInfo[idx][cInsurePrice]); //44
        new File: file2 = fopen("cars.cfg", io_write);
        if (!file2) return; // couldn't open file, stop right here
        fwrite(file2, coordsstring);
        idx++;
        fclose(file2);
    }
}