[Include] vehicles.inc - Automatic MySQL vehicle information loading and saving.
#1

Hey there,

this include helps to save and load information of vehicles without much effort.

Available functions:

Code:
// Get unique vehicle id (sql id)
stock GetVehicleUID(vehicleid);

// Create a vehicle. This function will create an entry in database for vehicle too.
stock AddVehicle(model, Float:x, Float:y, Float:z, Float:a, color1 = -1, color2 = -1);

// Load vehicle from database by vehicles UID (sqlid) or from active cache (use row parameter for that). Set applydata to false, if you dont want to apply loaded vehicle information (position, color, health)
stock LoadVehicle(sqlid = -1, row = 0, applydata = true);

// Save vehicle. Set update_pos to false if you don't want to save current vehicle position, and set remove to true, if you want to destroy vehicle from world.
stock SaveVehicle(vehicleid, update_pos = true, remove = false);

// Deletes vehicle entirely, both from game and from database. Cannot be undone.
stock DeleteVehicle(vehicleid);
Callbacks:

Code:
// Gets called when new vehicle is inserted into database
forward OnVehicleInsert(vehicleid);

// In this callback you can specify which information you want to be automatically loaded and saved
forward OnCreateVehicleORM(ORM:ormid, vehicleid);
Example:

PHP Code:
#include <a_samp>
#include <a_mysql>
#include <vehicles>
// you vehicle information enum
enum vehicle_e_DATA {
    
owner[MAX_PLAYER_NAME], // name, because I wanted to make example using string variable
    
Float:fuel,
    
Float:run,
    
price
};
new 
vehicleDB[MAX_VEHICLES][vehicle_e_DATA];
/* ... other code ... */
public OnVehicleInsert(vehicleid) {
    print(
"Vehicle (id: %i, sql id: %i) was successfully inserted into database!"
        
vehicleidGetVehicleUID(vehicleid));
}
public 
OnCreateVehicleORM(ORM:ormidvehicleid) {
    
// information you want to be automatically saved and loaded
    // don't forget to create additional fields in `vehicles` table
    
orm_addvar_string(ormidvehicleDB[vehicleid][owner], "owner");
    
orm_addvar_float(ormidvehicleDB[vehicleid][fuel], "fuel");
    
orm_addvar_float(ormidvehicleDB[vehicleid][run], "run");
    
orm_addvar_int(ormidvehicleDB[vehicleid][price], "price");

This include covers this information by default:
  • Model
  • Position (including angle)
  • Color
  • Health

Questions or suggesions?

grammar is bit fked up, feel free to correct me if needed!
Reply
#2

Hardly useful until it saves all vehicle information, Specially:
- Components (moding shops)
- Paintjob
- Attached Objects
- Damage status (like poped tires and so)

pawn Code:
static
        database,
        ORM:vehicle_ORM[MAX_VEHICLES],
        vehicle_ID[MAX_VEHICLES],
        vehicle_Model[MAX_VEHICLES],
        vehicle_Color1[MAX_VEHICLES],
        vehicle_Color2[MAX_VEHICLES],
        Float:vehicle_Health[MAX_VEHICLES],
        Float:vehicle_PosX[MAX_VEHICLES],
        Float:vehicle_PosY[MAX_VEHICLES],
        Float:vehicle_PosZ[MAX_VEHICLES],
        Float:vehicle_PosA[MAX_VEHICLES]
;
Use arrays; because its much better to handle them and saves memory.

You have hooked ChangeVehicleColor but what if the player resprays his/her vehicle. This won't save the color.

pawn Code:
static query[500];
I would recommend a local array for queries or any purpose related to formatting strings.
Reply
#3

SQLite would have made a lot more sense for a portable include.
Reply
#4

Quote:
Originally Posted by Gammix
View Post
Hardly useful until it saves all vehicle information, Specially:
- Components (moding shops)
- Paintjob
- Attached Objects
- Damage status (like poped tires and so)
You can add any additional information by yourself, the point of this include is to have a base for easy vehicle information loading and saving.

Quote:
Originally Posted by Gammix
View Post
pawn Code:
static
        database,
        ORM:vehicle_ORM[MAX_VEHICLES],
        vehicle_ID[MAX_VEHICLES],
        vehicle_Model[MAX_VEHICLES],
        vehicle_Color1[MAX_VEHICLES],
        vehicle_Color2[MAX_VEHICLES],
        Float:vehicle_Health[MAX_VEHICLES],
        Float:vehicle_PosX[MAX_VEHICLES],
        Float:vehicle_PosY[MAX_VEHICLES],
        Float:vehicle_PosZ[MAX_VEHICLES],
        Float:vehicle_PosA[MAX_VEHICLES]
;
Use arrays; because its much better to handle them and saves memory.
I'm pretty sure these ARE indeed arrays lol.

Quote:
Originally Posted by Gammix
View Post
You have hooked ChangeVehicleColor but what if the player resprays his/her vehicle. This won't save the color.
Good point, thanks! Fixed.

Quote:
Originally Posted by Gammix
View Post
pawn Code:
static query[500];
I would recommend a local array for queries or any purpose related to formatting strings.
Ugh, not sure why? Just moment ago you were talking about saving memory lol

Quote:
Originally Posted by Calgon
View Post
SQLite would have made a lot more sense for a portable include.
SQLite doen't support orm.
Reply
#5

For those who are already using MySQL, this will be an easy integration (just has to include and with few modifications the vehicles from now on will get saved)!!!!

You just also add features to save components and paintjobs.I don't think anyone would need to store damage status.I find spawning damaged cars funny

You should also try to keep the include versatile by providing options for storage system(MySQL,SQLite,INI).

Good Work!
Reply
#6

Quote:
Originally Posted by Yashas
View Post
For those who are already using MySQL, this will be an easy integration (just has to include and vehicles from now on will get saved)!!!!
Well not quite. You still have to use the functions provided for them to be saved.
Reply
#7

My bad, I always screw things because I don't put what is on my mind on paper properly.

Here is what I meant by it, just include and with 2 loops and everything gets saved.

Anyway thanks for pointing that out, edited.
Reply
#8

When you replied with the follow.

"You can add any additional information by yourself, the point of this include is to have a base for easy vehicle information loading and saving."

I must disagree with your response and agree with Gammix the reason is I feel that there is realistically only so much a user is going to need to actually save. If a user really does indeed need to use more than standard saved data then to me that is an appropriate time in which adding their own load/unload data would be useful. I would like to expand on how it should be done.

My suggestion is to create a standard load/unload include that contains all information for a vehicle including support for attached objects this would greatly improve the use.

Another thing is you named this so damn generic in the topic "vehicles.inc" it means nothing really
Reply
#9

Quote:
Originally Posted by Pottus
View Post
Another thing is you named this so damn generic in the topic "vehicles.inc" it means nothing really.
yup, that's true. vehicles-orm.inc should do the job.

And yeah, support for vehicle components, paintjob and attached objects is in progress. Thanks for feedback!
Reply
#10

I also should mention to make sure it also can save objects that have text/materials/colors as well this is important.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)