[Tutorial] How to create a vehicle system using Y_INI and Y_CMD
#1

Introduction:
In this tutorial, I will be showing you how to create an advance vehicle system using YINI and YCMD.






Prerequisites:
Y_INI => HERE
Y_COMMANDS => HERE





Step 1: The System

In this step, we will be doing the system. A system is the group of logics behind the function which can be reused.
pawn Код:
enum vInfo // The name of the enum. For naming convention use the system's first letter in lowercase.
{
    vID, // The vehicle id used on the SA-MP to represent the vehicle.
    vModel, // The vehicle model of the vehicle.
    Float:vLoc[4], // The array which contains position and the rotation of the vehicle in 4 cells. So, basically {X = 0, Y = 1, Z = 2, R = 3}
    vColor1, // The primary color of the vehicle.
    vColor2, // The secondary color of the vehicle.
    vRespawn, // The vehicle respawn time.
    vOwner[MAX_PLAYER_NAME], // The vehicle's owner name. Remember string is a group of literal characters and the cells you provide should be the maximum length for that string.
    bool:vLocked // Bool representing if the vehicle is locked or not. (0 = False, 1 = True)
}
new VehicleInfo[MAX_VEHICLES][vInfo]; // We are now making use of our ENUM by declaring an array which will hold all of the dynamic vehicle's information.
// The first cell is going to be the vehicle id based on our system(Not SA-MP). It's use to reference the dynamic vehicle for later usage.
// The second cell is the ENUM we made it. ENUMs are bunch of constant variables stacked up with each other.
// Imagine the array when the system will be ready. VehicleInfo[0][vID] or VehicleInfo[1][vID].

new bool:vCreated[MAX_VEHICLES]; // This will account the vehicles which has been created for later usage. The reason why we use this is to save/load
// the vehicle only if the vehicle is created. Otherwise not.

stock VehiclePath(vehicleID) // Creating a function which will get the vehicle path dynamically based on the vehicle id of the system.
{
    new strPath[64]; // Declaring the variable which will hold and return the vehicle path by vehicle id.
    format(strPath, sizeof(strPath), "/vehicle/%d.ini"); // We are formatting the string so that it holds the file path based on the vehicle id. '%d' means it will be a whole number.
   
    return strPath; // Return the file path so that it can be manipulate later on.
}  
stock VehicleGetFreeSlot() // This function is used to get the free slot available to be used on the vehicle. It will be the vehicle id.
{
    for(new i = 0; i < MAX_VEHICLES; i++) // Loop through all of the vehicles on the sa-mp world and increments the variable 'i' by 1.
    {
        if(!vCreated[i]) return i; // If that 'i' isn't being used on any of our system's vehicle then return this vehicle id and make use of it.
    }
   
    return -1; // Return -1 because 0 is a valid vehicle id for our system as we are left with -1.
}
stock VehicleCreate(vehicleModel, Float:vehicleLoc[4], vehicleColor1, vehicleColor2, vehicleRespawn, vehicleOwner[], bool:vehicleLocked)
// This is our most important function. This will create the vehicle with the valid information provided on the arguments.
{
    new vehicleid = VehicleGetFreeSlot(); // Get the free slot and store it on the vehicleid variable.
    //Now, we will basically put the information from the arguments to our newly created vehicle's array.
    VehicleInfo[vehicleid][vModel] = vehicleModel; // Assign our vehicle's model id to the model id provided.
    VehicleInfo[vehicleid][vLoc] = vehicleLoc; // Assign our vehicle's position/rotation to the position/rotation provided.
    VehicleInfo[vehicleid][vColor1] = vehicleColor1; // Assign our vehicle's primary color to the primary color provided.
    VehicleInfo[vehicleid][vColor2] = vehicleColor2; // Assign our vehicle's secondary color to the secondary color provided.
    VehicleInfo[vehicleid][vRespawn] = vehicleRespawn; // Assign our vehicle's respawn time to the respawn time provided.
    format(VehicleInfo[vehicleid][vOwner], MAX_PLAYER_NAME, vehicleOwner); // Assign our vehicle's owner name to the owner name provided.
    VehicleInfo[vehicleid][vLocked] = vehicleLocked; // Assign our vehicle's lock data to the lock data provided.
    VehicleInfo[vehicleid][vID] = CreateVehicle(vehicleModel, vehicleLoc[0], vehicleLoc[1], vehicleLoc[2], vehicleLoc[3], vehicleColor1, vehicleColor2,
    vehicleRespawn); // Now we are doing two things. We are creating our vehicle on the sa-mp world based on the data provided as well as assigning the
    // sa-mp vehicle id to our system's vehicle id so that we can alter it later.
    vCreated[vehicleid] = true; // Tell our vehicle management variable that this vehicle creation has been done and it's now on the system.
    VehicleLock(vehicleid, VehicleInfo[vehicleid][vLocked]); // Toggle our vehicle's door based on the parameter.
   
    return vehicleid; // Return the newly created vehicle's system id.
}
stock VehicleGet(vehicleID)
// Get the current sa-mp vehicle information for the provided vehicle id
{
    GetVehiclePos(VehicleInfo[vehicleID][vID], VehicleInfo[vehicleID][vLoc][0], VehicleInfo[vehicleID][vLoc][1], VehicleInfo[vehicleID][vLoc][2]);
    // Get the current position of that vehicle and assign it to our system's vehicle information.
    GetVehicleZAngle(VehicleInfo[vehicleID][vID], VehicleInfo[vehicleID][vLoc][3]);
    // Get the current rotation of that vehicle and assign it to our system's vehicle information.
}
stock VehicleLoad(vehicleID, file[])
// This function will load the vehicle provided by the id when the server starts.
{
    INI_ParseFile(file, "LoadVehicleData", .bExtra = true, .extra = vehicleID); // Parse the vehicle data from the INI file.
    VehicleCreate(VehicleInfo[vehicleID][vModel],
    VehicleInfo[vehicleID][vLoc], VehicleInfo[vehicleID][vColor1], VehicleInfo[vehicleID][vColor2], VehicleInfo[vehicleID][vRespawn],
    VehicleInfo[vehicleID][vOwner], VehicleInfo[vehicleID][vLocked]); // Creates the vehicle from the information we loaded and parsed. Pretty self-explanatory.
}
forward public LoadVehicleData(vehicleID, name[], value[]);
public LoadVehicleData(vehicleID, name[], value[]) // This a callback with the parameter of the vehicleID send from our VehicleLoad function. This function will pretty much parse the data from the file.
{
    new strLoc[8]; // Will hold the location key name dynamically.
    // The first argument is the key and the second argument is the data which will hold the value of that key.
    INI_Int("model", VehicleInfo[vehicleID][vModel]);
    for(new i = 0; i < 4; i++) format(strLoc, sizeof(strLoc), "Loc%d", i), INI_Float(strLoc, VehicleInfo[vehicleID][vLoc][i]);
    // Looping through our location data and retrive it. Loc0 = LocX, Loc1 = LocY, Loc2 = LocZ, Loc3 = LocA.
    INI_Int("color1", VehicleInfo[vehicleID][vColor1]); // You should've guessed it by now.
    INI_Int("color2", VehicleInfo[vehicleID][vColor2]);
    INI_Int("respawn", VehicleInfo[vehicleID][vRespawn]);
    INI_String("owner", VehicleInfo[vehicleID][vOwner], MAX_PLAYER_NAME);
    VehicleInfo[vehicleID][vLocked] = INI_Int("locked") == 1 ? true : false;
    //Converting the locked data from int(whole number) to bool(true/false) and assign it.

    return 1;
}
stock VehicleSave(vehicleID) // This function will save our vehicle on the INI file and will create it if it doesn't exist.
{
    new INI:dFile = INI_Open(VehiclePath(vehicleID)); // Open/Create the file for our vehicle.
    new strLoc[8]; // Will hold the location key name dynamically.
    // The first argument is the file to write the second is the key and the third argument is the value it will write.
    INI_WriteInt(dFile, "model", VehicleInfo[vehicleID][vModel]);
    for(new i = 0; i < 4; i++) format(strLoc, sizeof(strLoc), "Loc%d", i), INI_Float(dFile, strLoc, VehicleInfo[vehicleID][vLoc][i]);
    // Looping through our location data and write it. Loc0 = LocX, Loc1 = LocY, Loc2 = LocZ, Loc3 = LocA.
    INI_WriteInt(dFile, "color1", VehicleInfo[vehicleID][vColor1]); // You should've guessed it by now.
    INI_WriteInt(dFile, "color2", VehicleInfo[vehicleID][vColor2]);
    INI_WriteInt(dFile, "respawn", VehicleInfo[vehicleID][vRespawn]);
    INI_WriteString(dFile, "owner", VehicleInfo[vehicleID][vOwner]);
    INI_WriteInt(dFile, "locked", VehicleInfo[vehicleID][vLocked] ? 1 : 0);
    //Converting the locked data from bool(true/false) to int(whole number) and write it.
    INI_Close(dFile);
}
stock VehicleLoadAll() // This function will load all of the system vehicles created and saved.
{
    new index = 0; // We need this variable to keep track of the id or the index of the file. Start it with 0 as the id.

    while(fexist(VehiclePath(index))) // If any system vehicle has been created before with that 0 id
    {
        VehicleLoad(index, VehiclePath(index)); // Then load that vehicle.
        index++; // And move on to another system vehicle id.
    }
   
    printf("Vehicles Loaded: %d", index); // Print out how many vehicles has been loaded.
}
stock VehicleSaveAll()
{
    new index = 0; // We need this variable to keep track of the id or the index of the file. Start it with 0 as the id.

    for(new i = 0; i < MAX_VEHICLES; i++) // Loop through the maximum amount of vehicles allowed by sa-mp.
    {
        if(vCreated[i]) // If that is our system's vehicle and if it has been created
        {
            VehicleGet(index); // Get that vehicle's current data.
            VehicleSave(index); // Save that vehicle's data on the file.
            index++; // Move on to next vehicle.
        }
    }
   
    printf("Vehicles Saved: %d", index); // Print out how many vehicles has been saved.
}




Step 2: Implementing it on the gamemode
In this step, we will be implementing the vehicle system on the gamemode.
pawn Код:
public OnGameModeInit()
{
    VehicleLoadAll(); // Load all vehicles when the main script starts.

    return 1;
}

public OnGameModeExit()
{
    VehicleSaveAll(); // Save all vehicles when the main script stops/ends.
   
    return 1;
}




Step 3: Creating the commands.
In this step, we will be creating the commands to be able to use for our vehicle system.
pawn Код:
stock VehicleValid(vehicleID) //Get's the system's vehicle id from sa-mp's vehicle id.
{
    for(new i = 0; i < MAX_VEHICLES; i++) // For every vehicles till the maximum amount
    {
        if((vCreated[i]) && (VehicleInfo[i][vID] == vehicleID)) return i; // Is this vehicle created and it's the same sa-mp id as the argument provided? Return the system's id then.
    }
   
    return -1; // Return invalid system's vehicle id if it was not found.
}
stock VehicleEngine(vehicleID, bool:toggle) // Toggles the vehicle engine with the first parameter being the system's vehicle id not sa-mp's
{
    new vehicleid = VehicleInfo[vehicleID][vID]; // Get's the sa-mp's vehicle id.
    new engine, lights, alarm, doors, bonnet, boot, objective; // The variables to hold the current vehicle state.
    GetVehicleParamsEx(vehicleid, engine, lights, alarm, doors, bonnet, boot, objective); // Get's the current variable state and reference it to the variables.
    SetVehicleParamsEx(vehicleid, toggle, lights, alarm, doors, bonnet, boot, objective); // Set's the current vehicle state to be the same as before with just the engine being controlled by the bool:toggle parameter.
}
stock VehicleLock(vehicleID, bool:toggle) // Toggles the vehicle doors with the first parameter being the system's vehicle id not sa-mp's
{
    new vehicleid = VehicleInfo[vehicleID][vID]; // Get's the sa-mp's vehicle id.
    new engine, lights, alarm, doors, bonnet, boot, objective; // The variables to hold the current vehicle state.
    GetVehicleParamsEx(vehicleid, engine, lights, alarm, doors, bonnet, boot, objective); // Get's the current variable state and reference it to the variables.
    SetVehicleParamsEx(vehicleid, engine, lights, alarm, toggle, bonnet, boot, objective); // Set's the current vehicle state to be the same as before with just the door being controlled by the bool:toggle parameter.
}

new bool:engine; // This will check if we should turn on/turn off the engine. (TRUE/FALSE)
YCMD:engine(playerid, params[], help) // The base YCMD command structure with the command name being /engine which will toggle the engine.
{
    if(help) return SendClientMessage(playerid, -1, "Allows to toggle the vehicle engine."); // Shows him the help messages. Check out Y_CMD for more info.

    new vehicleid = VehicleID(GetPlayerVehicleID(playerid)); // Get's our system vehicle id from our sa-mp's vehicle id.
   
    if((IsPlayerInAnyVehicle(playerid)) && (vehicleid != -1)) // If they are on a vehicle and if that vehicle is the system's vehicle.
    {
        engine = !engine; // Inverse our boolean. So, if it's false it should be true and if it's true it should be false.
        VehicleEngine(vehicleid, engine); // Call the function we have just created.
    }
   
    return 1;
}
YCMD:createvehicle(playerid, params[], help) // The base YCMD command structure with the command name being /createvehicle which will create a system's vehicle.
{
    if(help) return SendClientMessage(playerid, -1, "Allows to create a dynamic vehicle."); // Shows him the help messages. Check out Y_CMD for more info.

    new vehModel, vehColor1, vehColor2, vehRespawnTime; // This variables will have the arguments value stored in it which will be parsed from sscanf.
   
    if(sscanf(params, "iiii", vehModel, vehColor1, vehColor2, vehRespawnTime)) // If they don't meet our arguments.
        return SendClientMessage(playerid, -1, "USAGE: /createvehicle [Vehicle Model] [Color 1] [Color 2] [Respawn Time]"); // Return a message to convey them the correct order/format.
   
    new Float:pLoc[4]; // This will hold our player's position and rotation data.
    new strPlayerName[MAX_PLAYER_NAME]; // This will hold our player's name.
    new vehicleid; // This will hold our newly created sa-mp vehicle id.
    GetPlayerPos(playerid, pLoc[0], pLoc[1], pLoc[2]); // Get's our player's position and store it in the variable.
    GetPlayerFacingAngle(playerid, pLoc[3]); // Get's our player's rotation and store it in the variable.
    GetPlayerName(playerid, strPlayerName, MAX_PLAYER_NAME); // Get's our player's name and store it in the variable.
    vehicleid = VehicleCreate(vehModel, pLoc, vehColor1, vehColor2, vehRespawnTime, strPlayerName, false);
    //Set's the model based on the argument, the position based on the player's position, the two color's based on the argument, the respawn time
    //based on the argument, the owner name based on the player's name and the vehicle lock to be false.
    PutPlayerInVehicle(playerid, vehicleid, 0); // Put the player in our vehicle.
   
    return 1;
}
//Updating...
Reply


Messages In This Thread
How to create a vehicle system using Y_INI and Y_CMD - by T0pAz - 16.02.2013, 04:50
Re: How to create a vehicle system using Y_INI and Y_CMD - by seanny - 16.02.2013, 08:19
Re: How to create a vehicle system using Y_INI and Y_CMD - by xDaidal0S - 16.02.2013, 09:15
Re: How to create a vehicle system using Y_INI and Y_CMD - by THE_KING$5$ - 16.02.2013, 09:18
Re: How to create a vehicle system using Y_INI and Y_CMD - by Jason_Dealley - 16.02.2013, 11:44
Re: How to create a vehicle system using Y_INI and Y_CMD - by T0pAz - 16.02.2013, 15:46
Re: How to create a vehicle system using Y_INI and Y_CMD - by DiGiTaL_AnGeL - 16.02.2013, 17:20
Re: How to create a vehicle system using Y_INI and Y_CMD - by Cameryn - 17.02.2013, 15:52
Re: How to create a vehicle system using Y_INI and Y_CMD - by TommyVer - 21.02.2013, 22:34
Re: How to create a vehicle system using Y_INI and Y_CMD - by [CG]Milito - 21.02.2013, 23:28
Re: How to create a vehicle system using Y_INI and Y_CMD - by Cameryn - 22.02.2013, 03:54
Respuesta: How to create a vehicle system using Y_INI and Y_CMD - by CrossOv3r - 24.02.2013, 00:28
AW: How to create a vehicle system using Y_INI and Y_CMD - by Blackazur - 24.02.2013, 11:57
AW: How to create a vehicle system using Y_INI and Y_CMD - by Ryan_Obeles - 24.02.2013, 12:56
Re: AW: How to create a vehicle system using Y_INI and Y_CMD - by Cameryn - 24.02.2013, 16:51
AW: How to create a vehicle system using Y_INI and Y_CMD - by Ryan_Obeles - 25.02.2013, 09:05
Re: AW: How to create a vehicle system using Y_INI and Y_CMD - by Cameryn - 25.02.2013, 18:10
Re: How to create a vehicle system using Y_INI and Y_CMD - by MadafakaPro - 07.04.2013, 11:58
Re: How to create a vehicle system using Y_INI and Y_CMD - by mrkiller90 - 11.04.2014, 13:19
Re: How to create a vehicle system using Y_INI and Y_CMD - by BleverCastard - 11.04.2014, 21:58
Re: How to create a vehicle system using Y_INI and Y_CMD - by Hayden_Almeida - 16.11.2015, 21:55
Re: How to create a vehicle system using Y_INI and Y_CMD - by Hayden_Almeida - 17.11.2015, 17:06
Re: How to create a vehicle system using Y_INI and Y_CMD - by N0FeaR - 19.11.2015, 06:36
Re: How to create a vehicle system using Y_INI and Y_CMD - by Karan007 - 19.11.2015, 09:18
Re: How to create a vehicle system using Y_INI and Y_CMD - by Hayden_Almeida - 20.11.2015, 13:00
Re: How to create a vehicle system using Y_INI and Y_CMD - by DiamondGaming - 25.12.2017, 15:01
Re: How to create a vehicle system using Y_INI and Y_CMD - by DiamondGaming - 25.12.2017, 15:12
Re: How to create a vehicle system using Y_INI and Y_CMD - by Matyaas - 22.02.2018, 04:17
Re: How to create a vehicle system using Y_INI and Y_CMD - by freddiebox - 01.04.2018, 10:57

Forum Jump:


Users browsing this thread: 2 Guest(s)