[Tutorial] Vehicle Number Plate (Reg) Generation
#1

VEHICLE NUMBER PLATE
GENERATING SYSTEM

Note: This system does use ZCMD so if you don't have experience using this include, I would not recommend reading this tutorial.


CODE

pawn Code:
enum vDetails
{
    vReg
}

CMD:registration(playerid, params[])
{
    new registration = GenerateRegistration();
    new string[128];
    new vehicleid = GetPlayerVehicleID(playerid);

    //Reg Formatting
    format(string, sizeof(string), "15 LV %d", registration);

    // Setting & Saving the number plate
    SetVehicleNumberPlate(vehicleid, string);
    format(vDetails[playerid][vReg], 64, "%s", string);

    // Confirmation message
    format(string, sizeof(string), "Your license plate was set to {FF9900}%s{AFAFAF}.", string);
    SendClientMessage(playerid, COLOR_GREY, string);
    return 1;
}
pawn Code:
stock GenerateRegistration()
{
    new registration = random(9999);
    foreach(Player, i)
    {
        if(vDetails[i][vReg] == registration || registration < 1000)
        {
            GenerateRegistration();
            return 1;
        }
    }
    return registration;
}
Just copy and paste the above and your done!
I'm joking, then it wouldn't be a tutorial.

SKIP TO "THE ACTUAL GENERATING" IF YOU ARE EXPERIENCED IN SCRIPTING.

EXPLAINING

The code below creates an enum that will store details about the vehicle, in this case the car number plate. We named it vReg for ease of usage. If you want an in-depth tutorial on what enmus are, I suggest you look HERE.
pawn Code:
enum vDetails
{
    vReg
}
This is basic ZCMD Language. It just states that when the command /registration is used, it will do the following.

pawn Code:
CMD:registration(playerid, params[])
{
I'm not going into much detail about the following three variables, mainly because that's not what the tutorial is about and it's fairly basic.

This bit of code uses the stock we will be creating in a minute to generate a random plate for us.
pawn Code:
new registration = GenerateRegistration();
This is just creating a new array to store our string in.
pawn Code:
new string[128];
This just creates a variable to store the vehicle ID in.
pawn Code:
new vehicleid = GetPlayerVehicleID(playerid);
All this code does is, it sets the start of the vehicle plate to 15 LV followed by the number we will generate. The 15 states the year and the LV states the city. You can obviously change the LV to LS or just remove the code if need be.
pawn Code:
//Reg Formatting
format(string, sizeof(string), "15 LV %d", registration);
Next is an important part of the code, it sets the vehicles plate to our generated vehicle plate, aswell as storing the plate into the enum vReg that we made above.

We use the vehicleid in SetVehicleNumberPlate to set the plate of the vehicle you are currently in. If you have a vehicle system in your server and you want to change a specific vehicle plate, you put the vehicleid here. The official samp callback is HERE.

We then use the string we stored our vehicle plate in above to set our plate to it. The string is also used at the end of saving the plate to the enum. If you want more information on formatting, click HERE.

Again, format(PlayerInfo[playerid][vReg], 64, "%s", string) isn't really relative to this tutorial so click THIS link on more information.
pawn Code:
// Setting & Saving the number plate
SetVehicleNumberPlate(vehicleid, string);
format(PlayerInfo[playerid][vReg], 64, "%s", string);
Again, this is just basic coding with strings to send the player a message on what plate was generated for them, use the links above for information on how to do it.
pawn Code:
// Confirmation message
format(string, sizeof(string), "Your license plate was set to {FF9900}%s{AFAFAF}.", string);
SendClientMessage(playerid, COLOR_GREY, string);
THE ACTUAL GENERATING
pawn Code:
stock GenerateRegistration() //states the name of the stock
{
    new registration = random(9999); //Generates a number out of 9999
    foreach(Player, i) //Searches every user in the server checking if the vehicle plate is used.
    {
        if(PlayerInfo[i][vReg] == registration || registration < 1000) // Checks if the reg is in use or if its below 1000.
        {
            GenerateRegistration(); //Regenerates a reg if the criteria is not met
            return 1;
        }
    }
    return registration; //returns the reg if the criteria is met
}
Hopefully it was of use to someone, but if not-.. well fu*k.
Reply


Messages In This Thread
Vehicle Number Plate (Reg) Generation - by Ciarannn - 10.03.2015, 19:32
Re: Vehicle Number Plate (Reg) Generation - by zT KiNgKoNg - 10.03.2015, 19:39
Re: Vehicle Number Plate (Reg) Generation - by Ciarannn - 10.03.2015, 19:41
Re: Vehicle Number Plate (Reg) Generation - by Luis- - 11.03.2015, 00:38
Re: Vehicle Number Plate (Reg) Generation - by Ciarannn - 11.03.2015, 01:50
Re: Vehicle Number Plate (Reg) Generation - by SickAttack - 11.03.2015, 02:29
Re: Vehicle Number Plate (Reg) Generation - by PT - 11.03.2015, 02:30
Re: Vehicle Number Plate (Reg) Generation - by Ciarannn - 11.03.2015, 03:00

Forum Jump:


Users browsing this thread: 1 Guest(s)