Vehicle System
#1

Hi there. I need help with my vehicle system for a RolePlay server. I don't know very well to do this thing, for this i come here to ask you. When i spawn a vehicle with /veh, to remain after restart.. Who can help me?
Reply
#2

You would need a mysql connection if you want to do this effectively. Do you know how to use Mysql with Pawn?
Reply
#3

I`m using MySQL now.. I have database with all it's need, but i doesn't know how to make /veh command to introduce that car in MySQL. Can someone help me ?
Reply
#4

pawn Код:
COMMAND:savevehicle(playerid, params[])
{
    if(!IsPlayerAdmin(playerid)) return SendClientMessage(playerid, 0xFF0000AA, "ERROR: You can't use this command!");
    if(!IsPlayerInAnyVehicle(playerid)) return SendClientMessage(playerid, 0xFF0000AA, "ERROR: Must be in a vehicle to use this command!");
    new Float:vehx, Float:vehy, Float:vehz, Float:veho, vehm, color1, color2, currentveh, string[128], query[250];
    if(sscanf(params, "dd", color1, color2)) return SendClientMessage(playerid, 0xFF0000AA, "USAGE: /savevehicle (color1) (color2)");
    currentveh = GetPlayerVehicleID(playerid);
    GetVehiclePos(currentveh, vehx, vehy, vehz);
    GetVehicleZAngle(currentveh, veho);
    vehm = GetVehicleModel(currentveh);
    format(string, sizeof(string), "SERVER: Static vehicle (%d) (%d) added at: %f, %f, %f, %f", type, color1, color2, vehx, vehy, vehz, veho);
    SendClientMessage(playerid, 0xFFFFFFAA, string); printf("string");
    format(query, sizeof(query), "INSERT INTO `vehicles` (`vehid`, `x`, `y`, `z`, `o`, `color1`, `color2`) VALUES ('%d', '%f', '%f', '%f', '%f', '%d', '%d');", vehm, vehx, vehy, vehz, veho, color1, color2);
    mysql_query(query);
    return 1;
}
If you have ZCMD and sscanf it should work just fine.

pawn Код:
CREATE TABLE IF NOT EXISTS `vehicles` (
  `id` int(5) NOT NULL AUTO_INCREMENT,
  `vehid` int(11) NOT NULL,
  `x` varchar(50) DEFAULT NULL,
  `y` varchar(50) DEFAULT NULL,
  `z` varchar(50) DEFAULT NULL,
  `o` varchar(50) DEFAULT NULL,
  `color1` int(3) DEFAULT NULL,
  `color2` int(3) unsigned DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `id` (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=0 ;
pawn Код:
forward SpawnVehicles();
public SpawnVehicles()
{
    new
        vehdata[150],
        query[120];
    format(query, sizeof(query), "SELECT * FROM `vehicles` ORDER BY `id`;");
    mysql_query(query); mysql_store_result();

    while(mysql_fetch_row(vehdata,"|"))
    {
        new Float:vehx, Float: vehy, Float: vehz, Float: veho, vehm, vehc1, vehc2, plate[32];
        mysql_fetch_field("x",  vehdata); vehx = floatstr(vehdata);
        mysql_fetch_field("y",  vehdata); vehy = floatstr(vehdata);
        mysql_fetch_field("z",  vehdata); vehz = floatstr(vehdata);
        mysql_fetch_field("o",  vehdata); veho = floatstr(vehdata);
        mysql_fetch_field("vehid",  vehdata); vehm = strval(vehdata);
        mysql_fetch_field("color1",  vehdata); vehc1 = strval(vehdata);
        mysql_fetch_field("color2",  vehdata); vehc2 = strval(vehdata);

        new
            number = 100+random(999),
            vehicleid = CreateVehicle(vehm, vehx, vehy, vehz, veho, vehc1, vehc2, 1800);

        format(plate, sizeof(plate), "LS %d", number);
        SetVehicleNumberPlate(vehicleid, plate);
    }
    printf("[MySQL] Loaded vehicles from database, a total of: %d", mysql_num_rows());
    mysql_free_result();
    return 1;
}
Under OnGameModeInit()
pawn Код:
SpawnVehicles();
This is something I just, made should work anyways.
Reply
#5

Please help me .
Reply
#6

Quote:
Originally Posted by _rAped
Посмотреть сообщение
pawn Код:
COMMAND:savevehicle(playerid, params[])
{
    if(!IsPlayerAdmin(playerid)) return SendClientMessage(playerid, 0xFF0000AA, "ERROR: You can't use this command!");
    if(!IsPlayerInAnyVehicle(playerid)) return SendClientMessage(playerid, 0xFF0000AA, "ERROR: Must be in a vehicle to use this command!");
    new Float:vehx, Float:vehy, Float:vehz, Float:veho, vehm, color1, color2, currentveh, string[128], query[250];
    if(sscanf(params, "dd", color1, color2)) return SendClientMessage(playerid, 0xFF0000AA, "USAGE: /savevehicle (color1) (color2)");
    currentveh = GetPlayerVehicleID(playerid);
    GetVehiclePos(currentveh, vehx, vehy, vehz);
    GetVehicleZAngle(currentveh, veho);
    vehm = GetVehicleModel(currentveh);
    format(string, sizeof(string), "SERVER: Static vehicle (%d) (%d) added at: %f, %f, %f, %f", type, color1, color2, vehx, vehy, vehz, veho);
    SendClientMessage(playerid, 0xFFFFFFAA, string); printf("string");
    format(query, sizeof(query), "INSERT INTO `vehicles` (`vehid`, `x`, `y`, `z`, `o`, `color1`, `color2`) VALUES ('%d', '%f', '%f', '%f', '%f', '%d', '%d');", vehm, vehx, vehy, vehz, veho, color1, color2);
    mysql_query(query);
    return 1;
}
If you have ZCMD and sscanf it should work just fine.

pawn Код:
CREATE TABLE IF NOT EXISTS `vehicles` (
  `id` int(5) NOT NULL AUTO_INCREMENT,
  `vehid` int(11) NOT NULL,
  `x` varchar(50) DEFAULT NULL,
  `y` varchar(50) DEFAULT NULL,
  `z` varchar(50) DEFAULT NULL,
  `o` varchar(50) DEFAULT NULL,
  `color1` int(3) DEFAULT NULL,
  `color2` int(3) unsigned DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `id` (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=0 ;
pawn Код:
forward SpawnVehicles();
public SpawnVehicles()
{
    new
        vehdata[150],
        query[120];
    format(query, sizeof(query), "SELECT * FROM `vehicles` ORDER BY `id`;");
    mysql_query(query); mysql_store_result();

    while(mysql_fetch_row(vehdata,"|"))
    {
        new Float:vehx, Float: vehy, Float: vehz, Float: veho, vehm, vehc1, vehc2, plate[32];
        mysql_fetch_field("x",  vehdata); vehx = floatstr(vehdata);
        mysql_fetch_field("y",  vehdata); vehy = floatstr(vehdata);
        mysql_fetch_field("z",  vehdata); vehz = floatstr(vehdata);
        mysql_fetch_field("o",  vehdata); veho = floatstr(vehdata);
        mysql_fetch_field("vehid",  vehdata); vehm = strval(vehdata);
        mysql_fetch_field("color1",  vehdata); vehc1 = strval(vehdata);
        mysql_fetch_field("color2",  vehdata); vehc2 = strval(vehdata);

        new
            number = 100+random(999),
            vehicleid = CreateVehicle(vehm, vehx, vehy, vehz, veho, vehc1, vehc2, 1800);

        format(plate, sizeof(plate), "LS %d", number);
        SetVehicleNumberPlate(vehicleid, plate);
    }
    printf("[MySQL] Loaded vehicles from database, a total of: %d", mysql_num_rows());
    mysql_free_result();
    return 1;
}
Under OnGameModeInit()
pawn Код:
SpawnVehicles();
This is something I just, made should work anyways.
It doesn't work.










EDIT: I have the command to save car in Mysql, i need function to respawn them.
Reply
#7

I need a function to respawn the cars from MySQL ...
Reply
#8

SpawnVehicles();

On OnGameModeInit
Reply
#9

That function doesn't work.
Reply
#10

When you create a vehicle, save all data to a file: it's model-id, spawn-coordinates (x, y, z, angle).
Use numeric values when saving the files, like "vehicle1.dat", "vehicle2.dat".
Each time you create a vehicle, search for the first free number (in this example it would be 3).

When you restart the server, reload all these files and re-create the vehicles during OnGameModeInit.
Very easy to do.
Creating houses works exactly the same way, creating them by "/createhouse", which also saves the house-data to a file.
During OnGameModeInit, just load all house-files you can find and re-create them all.
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)