You have to decide if you want to put all vehicles into one file, which is a little easier to implement, but very (very) slow, or if you want to create an won file for every vehicle, which is at least a little tricky.
I will only explain the second variant, as i would prefer it, and as there is no big difference in implementing between the first.
At first i would recommend you using an ini include. There are lots of different ones, every single has its own advantages and disadvantages, while the way you use them is almost the same.
In your script, add a boolean array, with the size MAX_VEHICLES (2000), and a function to get the number of the first entry that is false (the first "free slot")
pawn Код:
new bool:validcar[MAX_VEHICLES];
stock GetFreeVehicleSlot()
{
for(new i = 0; i < sizeof(validcar); i ++)
{
if(!validcar[i]) return i;
}
return -1;
}
This will store if a car exists (true) or not (false), you will need this just for determining the name of the file the vehicle is saved in.
The next thing you will need is a functio to create a new vehicle, you can just call it from a command later, and it will create the car you want. It will store some of the cars attributes into another array and spawn the car. The following function is just an example, you can add every attribute you want to be customizable.
pawn Код:
enum carDataEnum { //You have to use an enum here, because you have to put different variable types into one array
model,
Float:xspawn,
Float:yspawn,
Float:zspawn,
Float:anglespawn,
col1,
col2,
respawn,
owner[20]
}
new carData[MAX_VEHICLES][carDataEnum];
stock CreateVehicleEx(modelid, Float:x, Float:y, Float:z, Float:angle, color1, color2, respawntime, ownername[20])
{
//now put all the data into the array
//To get the index, use the first free slot with the function before
new carid = GetFreeVehicleSlot();
carData[carid][model] = modelid;
carData[carid][xspawn] = x;
carData[carid][yspawn] = y;
carData[carid][zspawn] = z;
carData[carid][anglespawn] = angle;
carData[carid][col1] = color1;
carData[carid][col2] = color2;
carData[carid][respawn] = respawntime;
carData[carid][owner] = ownername;
//... you can just add any data you like
//dont forget to mark the carslot as used in the validcar array
validcar[carid] = true;
//At last create the vehicle in the game
CreateVehicle(modelid, x, y, z, angle, color1, color2, respawntime);
return carid; //Make the function return the carid/array index. This is not necessarily needed, but good style
}
Now we get to the tricky part, where you have to save the car to a file using any ini system. The basic strategy is to create a function to save a single vehicle with a specified id (index of it in the carData array) to a file with a specified name, and another function to save all vehicles, using the first function in a loop.
This is only pseudo code, you will have to change the used ini function to match to your ini include.
pawn Код:
stock SaveVehicle(vehicle, filename[36])
{
new iniid = ini_CreateIniFile(filename);
ini_SetInt(iniid, "Model", carData[vehicle][model]);
ini_SetFloat(iniid, "XSpawn", carData[vehicle][xspawn]);
//... do this for all elements in the array/enum
ini_CloseIni(iniid);
}
//this function will save all vehicles in files with a continous number as name, because it will be easier to load
//than some other name
stock SaveAllVehicles()
{
new saveindex = 0;
new fname[36];
for(new i = 0; i < MAX_VEHICLES; i ++)
{
if(validcar[i])
{
format(fname, sizeof(fname), "%d.ini", saveindex); //You can also add a subfolder here<.
//ex: format(fname, sizeof(fname), "/vehicles/%d.ini", saveindex); //will put all the files onto /scriptfiles/vehicles/ if the folder exists
SaveVehicle(i, fname);
saveindex ++; //Increase the filename number for the next vehicle
}
}
So far all the vehicles you create using CreateVehicleEx will be saved with SaveAllVehicles(). YOu could for example call this function in OnGameModeExit or in a timer.
And whats missing? Of course, something to load the vehicles from the files. It will just work the other way round as the save functions, create two functions again (pseudo ini-code):
pawn Код:
stock LoadVehicle(filename[36])
{
new iniid = ini_OpenIni(filename);
//Now just use CreateVehicleEx and load all the parameters from the ini file
//The function will do all the rest for you
CreateVehicleEx(ini_GetInt(iniid, "Model"), ini_GetFloat(iniid, "XSpawn"), ...);
ini_CloseIni(iniid);
}
stock LoadAllVehicles()
{
new fname[36];
new index = 0;
format(fname, sizeof(fname), "%d.ini", index);
while(fexist(fname)) //Here's the reason why the ini files are named continuosly
//This function keeps loading the files with next number as long as there is a next one
//and then stops loading, so you do not need a plugin to get all files in a folder or so
{
LoadVehicle(fname);
index ++;
format(fname, sizeof(fname), "%d.ini", index);
}
}
Be careful with the LoadAllVehicles function, you should only use it at the beginning of your gm (OnGameModeInit) or after deleting all existing vehicles on the server (you can for example set validcar to false for every index with a loop) or your vehicles will exist twice and will also be saved twice. And parting the original vehicles from the doubled ones can be very annoying if you have a lot of vehicles.
So all in all, these functions should be a ground structure for your vehicle system. Looks quite little, doesnt it? It has some small disadvantages, like you cant just delete a file to delete the vehicle it represents, you can only do that ingame to keep the system working correctly.
But apart from this, if you adjust them to your server, especially your ini system, you have a nice base that does the save/load job, so you can focus on scripting all the other stuff you'd like to have for your vehicle system.
I hope this helps you, I wrote all this off hand, so I cant guarantee for it to be free from bugs. But I think also if you shouldnt be scripting for a long time, you should be able to fix them, the structure itself should work. If you have any further questions, feel free to ask, ill follow this topic for the next one or two days.
BTW: Hehe, I just realized that I wrote all that in about 30 mins, and so built a whole vehicle system. I think I will also post this in the tutorial section, as some others might also fgind this helpful (even if there are some other tutorials on this topic i think)