Need Help On Dynamic
#1

Hey guys, i started on my dynamic vehicle system and well im stuck now.

How do i make the CMD that adds vehicles to my file and how do i format it correctly? After that how do i read the vehicles off the file and make the cars load? Someone help please, dont just read this and leave -.-
Reply
#2

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)
Reply
#3

Thanks a bunch, and yes it would be a very good thing if you posted in tut section because we have nearly no dini dynamic tuts . I will attempt to see if this works and i will reply

For cmd, i would use
pawn Код:
CreateVehicleEx(modelid, Float:x, Float:y, Float:z, Float:angle, color1, color2, respawntime, ownername[20]);
Right?
Reply
#4

Quote:
Originally Posted by Mauzen
Посмотреть сообщение
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.
How slow? Very very very very slow? O.o

If so, i will quit scripting forever, lol. I have made really really (really) unique Dynamic Vehicle system for me, and it uses only one .cfg file, and the file will include about 200-500 vehicles (ridicilious right?).. So, shall i stop scripting this script? (cuz im not going to REscript this, takes ages)
Reply
#5

Keyway, just test it xD
Reply
#6

My car system, to load 2000 vehicles from Cars.cfg only takes 46 ms, and to load 500 vehicles only takes 12 ms.. it's not so bad.
Reply
#7

Jesus 2000 cars thats alot lol. But nice loading time
Reply
#8

@ Muzen, just an amazing post there. Never seen someone to help in Scripting Discussion, as much as yopu just did for Anthony.
Reply
#9

@KeyWey & The_Moddler: Ok, seems to be faster than i thought then. But deleting or changing single vehicles will always be slower than with one file per vehicle. You will have to rewrite the whole file, even if you only want to change a single line, because samps native file functions have no random access, only linear.
But if you keep an eye open for every optimization, also a single file system will be more than fast enough to use on a server.

@Zh3r0: I just started explaining it, without thinking about how long it will be, and if I start with asnwering ones question, I dont like to stop
Reply
#10

Quote:
Originally Posted by Zh3r0
Посмотреть сообщение
@ Muzen, just an amazing post there. Never seen someone to help in Scripting Discussion, as much as yopu just did for Anthony.
It was copy and pasted from his tutorial.. -.-
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)