[Tutorial] Vehicle Data
#1

Vehicle Data

Ever wanted to store data for a vehicle? It's very simple. If you know how to store player data, you know how to store vehicle data.

Basics

Let's take a look at 2 key terms:

playerid and vehicleid

Both these terms represent integers, which is why it's possible to create "for" loops starting at zero and passing "i" as a playerid.

What We Know

Let's look at a simple player data table:

pawn Code:
enum playerdata
{
    pass[24],
    money,
    score
}
new pInfo[MAX_PLAYERS][playerdata];
From what we know, if we wanted to set a player's money, all we would need to do is change pInfo[playerid][money].

This same concept can be applied to vehicles:

pawn Code:
enum vehicledata
{
    owner[24],
    engine,
    tires
}
new vInfo[MAX_VEHICLES][vehicledata];
To use this data, all we need is a vehicleid.

Applications

To get a vehicleid, the easiest way would be to assign a vehicle with a custom variable:

pawn Code:
new WorldCar[10];

//Under OnGameModeInIt
WorldCar[0] = CreateVehicle...
WorldCar[1] = CreateVehicle...
To access and change the data, all we would have to do is use vInfo[WorldCar[0]][owner], or whatever you may choose for your variables. WorldCar[0] is a placeholder for an integer, the same way playerid is a placeholder for an integer.

The reason I chose to assign WorldCar to an array would be to easily loop all selected vehicles to detect a specific one easier, or to change 1 variable for all cars at once:

pawn Code:
for(new i=0; i<MAX_VEHICLES; i++)
{
     vInfo[WorldCar[i]][owner] = "Rental";
}
To test the data, you can set up a command to show vehicle data for a vehicle you entered.

pawn Code:
CMD:test(playerid, params[])
{
    for(new i=0; i<MAX_VEHICLES; i++)
    {
        if(IsPlayerInVehicle(playerid, WorldCar[i], 0))
        {
            PrintF("VDATA: owner: %s engine: %i tires: %i", vInfo[WorldCar[i]][owner], vInfo[WorldCar[i]][engine], vInfo[WorldCar[i]][tires]);
        }
    }
    return 1;
}
After testing to make sure that data is working per vehicle, you can display vehicle data using chat, textdraws, 3dtextlabels, or anywhere you can place text.

I hope this tutorial was somewhat informative, thanks for reading.
Reply


Messages In This Thread
Vehicle Data - by Aerotactics - 01.12.2014, 13:09
Re: Vehicle Data - by Luis- - 01.12.2014, 13:55
Re: Vehicle Data - by Aerotactics - 01.12.2014, 13:56
Re: Vehicle Data - by Luis- - 01.12.2014, 13:57
Re: Vehicle Data - by PowerPC603 - 01.12.2014, 14:01
Re: Vehicle Data - by Aerotactics - 01.12.2014, 14:06
Re: Vehicle Data - by PowerPC603 - 01.12.2014, 14:59
Re: Vehicle Data - by Aerotactics - 01.12.2014, 15:31

Forum Jump:


Users browsing this thread: 1 Guest(s)