[Tutorial] How to create your own vehicle system
#1

(I originally wrote this as an answer to a scripting question, so it wasnt really planned to be posted hera as a tutorial. But when I finished writing this, I realized it became one.)


You have to decide if you want to put all vehicles into one file, which is a little hard to implement, because most ini systems cant handel the big number of lines you will need, so you would have to write your own file management; or if you want to create an own file for every vehicle, which is easier for beginners.

I will explain the second variant here.
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[MAX_PLAYER_NAME]
}  

new carData[MAX_VEHICLES][carDataEnum];

stock CreateVehicleEx(modelid, Float:x, Float:y, Float:z, Float:angle, color1, color2, respawntime, ownername[MAX_PLAYER_NAME])
{
    //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.


Feel free to give some feedback or suggestions, so I can keep this tutorial up to date.
Reply
#2

Nice work!
Reply
#3

nice job 10/10
This forum requires that you wait 120 seconds between posts. Please try again in 42 seconds.
-.-
edit: can u add a pastebin?
Reply
#4

Heh, thanks for the help, it was best help ive ever received
Reply
#5

Great work.
Reply
#6

great work and tnx !!!
Reply
#7

Dam nice tut veryhandy
Reply
#8

great work
Reply
#9

This is good for newbs. 10/10
Reply
#10

How do i add vehicles?
I mean,i never scripted a Vehicle System.
Reply
#11

SkizzoTrick, CreateVehicleEx.
Reply
#12

Ohh,ffs that's logic )
Reply
#13

Quote:
Originally Posted by jameskmonger
View Post
SkizzoTrick, CreateVehicleEx.
Two more questions:
1)Where do i put CreateVehicleEx?
In the script?
pawn Code:
CreateVehicleEx(422,1657.3982,-2324.4800,-2.6797,90.8645,0,0,30000,ownername);
2)What do i put at OwnerName?

Sorry for all these questions but i never worked with this ;|
Reply
#14

thanks dawg just what i was looking for
Reply
#15

Can someone lighten up this question how to buy/create the vehicle. An example command would be appreciated.
Reply
#16

Quote:
Originally Posted by Omecken
View Post
Can someone lighten up this question how to buy/create the vehicle. An example command would be appreciated.
Someone?
Reply
#17

Nice tutorial edit the tut to save even the modifications like paintjob, bumpers e.t.c and someone please give me your finished script , im new to this so i want to understand thanks
Reply
#18

Uhm i keep getting this : Run time error 6 :"Invalid instruction" thing after i add this to the script.

Can anyone help me out?
Reply
#19

How do I load/save the owner ? can you give me an example for a /park cmd or something like that?

and aobut the vehicle creating

new string[256];
format(string,sizeof(string),"%s",GetName(playerid ));
CreateVehicleEx(model,x,y,z,.....blablabla,string) ;

YEAH ?
Reply
#20

Quote:
Originally Posted by bijoyekuza
View Post
How do I load/save the owner ? can you give me an example for a /park cmd or something like that?

and aobut the vehicle creating

new string[256];
format(string,sizeof(string),"%s",GetName(playerid ));
CreateVehicleEx(model,x,y,z,.....blablabla,string) ;

YEAH ?
Yeah, I have a same qestion. I do something like this:

Quote:
Originally Posted by My Script
new BuyerName[MAX_PLAYER_NAME];
GetPlayerName(playerid, BuyerName, sizeof(BuyerName));
CreateVehicleEx(405, 102.0081, 1109.1520, 13.5795, 181.9413, 69, 1, 3600, BuyerName);
And it gives me an error like that:

Quote:
Originally Posted by WTF?!!
C:\Users\user\Desktop\Role-Play\gamemodes\car.pwn(25 : error 047: array sizes do not match, or destination array is too small
What's wrong? I would be very happy if someone could say what's wrong.
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)