Vehicle System MYSQL Help
#1

Hello!

I'm making a MySQL vehicles system and I need some help. First of all, I create vehicles IG, then I sell this vehicles generating one copy of the vehicle created. That's not the problem, the car creates good. I want to make something but I don't know how I can do it.

I want the user can "despawn" the car. So, he has a dialog with his buyed cars and if he click on one car, the car dissapears. Then If he click another time the car (on list dialog) the car will be respawned.

Actually I store vehicles in arrays (Vehicles for ALL vehicles and VehicleUser for user vehicles). The system on my head works but then, on my script not. Some code:

Код:
public OnFilterScriptInit()
{
        // Here loads ALL the vehicles
	LoadVehicles();
}
public OnFilterScriptExit()
{
	SaveVehicles();
	return 1;
}
public OnPlayerSpawn(playerid)
{
        // Here ONLY USER vehicles
	LoadUserVehicles(playerid);
}

CMD:mycars(playerid, params[])
{
    new string[240];
    new stats[30];
    new count;
    for(new i=0;i<DYNAMIC_MAX;i++)
	{
		if(strmatch(VehicleUser[i][Owner],PlayerName(playerid)))
 		{
 		    if (VehicleUser[i][Stats] == 0)
 		    {
    			stats = "{B2F289}Spawnead";
    		}
    		else if (VehicleUser[i][Stats] == 1)
    		{
    		    stats = "{CAE1FF}Saved";
    		}
		 	format(string,sizeof(string), "%s%s (%d) - [%s] - %s\n", string, GetVehicleName(VehicleUser[i][Model]), VehicleUser[i][ID], VehiclePlate(VehicleUser[i][ID]), stats);
		 	ListSelection[playerid][count] = i;
		 	count++;
	 	}
	}
	if (count >= 1)
	{
		ShowPlayerDialog(playerid, DIALOG_MYCARS, DIALOG_STYLE_LIST, "My Cars", string, "Change", "Exit");
	}
	else
	{
	    SendClientMessage(playerid, -1, "{40D3FF}[SERVIDOR]: {FFFFFF}You don't own any car.");
	}
	return 1;
}

public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
{
    if (dialogid == DIALOG_MYCARS)
    {
        if(response)
        {
            new sel[300];
			new stats[30];
			if (VehicleUser[ListSelection[playerid][listitem]][Stats] == 0)
 			{
    			new Float:XX1, Float:YY1, Float:ZZ1;
      			GetVehiclePos(VehicleUser[ListSelection[playerid][listitem]][ID], XX1, YY1, ZZ1);
				if(IsPlayerInRangeOfPoint(playerid, 3.0, XX1, YY1, ZZ1))
				{
     				stats = "{CAE1FF}saved";
					format(sel,sizeof(sel),"{FFFFFF}You have %s the vehicleid %d.", stats, VehicleUser[ListSelection[playerid][listitem]][ID]);
    				ShowPlayerDialog(playerid, DIALOG_ALERT, DIALOG_STYLE_MSGBOX, "Attention!", sel, "Okay!", "");
       				DeleteVehicleFromServer(playerid, GetVehicleID(VehicleUser[ListaSeleccion[playerid][listitem]][ID]));
					SaveStats(ListSelection[playerid][listitem], 1); // Just update DB. Works fine.
					FreeVehicleID();
					LoadUserVehicles(playerid);
				}
				else
				{
				    SendClientMessage(playerid,-1,"{40D3FF}[SERVER]: {FFFFFF}3m to your vehicle!");
				}
			}
			else
			{
				stats = "{B2F289}spawned";
				format(sel,sizeof(sel),"{FFFFFF}You have %s the vehicle.",stats);
		     	ShowPlayerDialog(playerid, DIALOG_ALERT, DIALOG_STYLE_MSGBOX, "Attention!", sel, "Okay!", "");
		     	SaveStats(ListSelection[playerid][listitem], 0);
       			CreateUserVehicleByID(ListSelection[playerid][listitem]);
       			SetVehicleHealth(VehicleUser[ListSelection[playerid][listitem]][ID], VehicleUser[GetVehicleID(VehicleUser[ListSelection[playerid][listitem]][ID])][Health]);
       			LoadUserVehicles(playerid);
			}
        }
    }
    return 1;
}
Works, but I have a bug. When I "save" a car, the other cars gets another ID's. So I do all via MySQL ID's (row ID's, an autoincrement). But I only can Save and Spawn a car only one time, the second time I try to save or spawn a car, doen't do nothing (I think it's problem of the IDs). If anyone is interested on a big help, I can provide the entire script and a SQL file...

Lot, lot lot of thanks!
Reply
#2

I can't quite understand what the problem is, but you mention something about ID's.
If by that you mean vehicle ID's, then keep in mind the following:
  • When a vehicle is created, it takes up a unique ID from 1 (not 0 for some reason) to MAX_VEHICLES - 1.
  • When a vehicle is destroyed, the ID gets freed up and can be taken up by the next vehicle that is created in the server.
  • So if for example you have 5 vehicles on your server with ID's 1, 2, 3, 4, 5 occupied. The next vehicle you create will probably use ID 6.
  • Say you again have 5 vehicles on your server with ID's 1, 2, 3, 4, 5 occupied and you destroy vehicle with ID 2, that ID gets freed up. The next vehicle you create will probably use ID 2.
  • In other words if you store the vehicle ID in your database that ID is not at all guaranteed to work! So you should not rely on that!
If I read your post correctly, you already figured that out and used RowID's instead. Or was that not what you meant?
If this is not at all the problem, can you show your Save and Load functions?
Reply
#3

Sure!

Код:
// Loading ALL vehicles
public OnVehiclesLoad()
{
	for(new i = 0; i < cache_num_rows(); i++)
	{
		Vehicle[i][ID] = cache_get_field_content_int(i, "ID");
		cache_get_field_content(i, "plate", str);
		strpack(Vehicle[i][Plate], str, 30);
		Vehicle[i][Model] = cache_get_field_content_int(i, "model");
		Vehicle[i][Color1] = cache_get_field_content_int(i, "color1");
		Vehicle[i][Color2] = cache_get_field_content_int(i, "color2");
		Vehicle[i][PosX] = cache_get_field_content_float(i, "posx");
		Vehicle[i][PosY] = cache_get_field_content_float(i, "posy");
		Vehicle[i][PosZ] = cache_get_field_content_float(i, "posz");
		Vehicle[i][PosA] = cache_get_field_content_float(i, "posa");
                cache_get_field_content(i, "llaves1", str);
		strpack(Vehicle[i][Llaves1], str, 30);
		cache_get_field_content(i, "llaves2", str);
		strpack(Vehicle[i][Llaves2], str, 30);
		cache_get_field_content(i, "llaves3", str);
		strpack(Vehicle[i][Llaves3], str, 30);
		cache_get_field_content(i, "llaves4", str);
		strpack(Vehicle[i][Llaves4], str, 30);
		cache_get_field_content(i, "dueno", str);
		strpack(Vehicle[i][Dueno], str, 30);
		Vehicle[i][Bloqueo] = cache_get_field_content_int(i, "bloqueo");
		Vehicle[i][Precio] = cache_get_field_content_int(i, "precio");
		Vehicle[i][Venta] = cache_get_field_content_int(i, "venta");
		Vehicle[i][Stats] = cache_get_field_content_int(i, "stats");
		Vehicle[i][ExpoX] = cache_get_field_content_float(i, "expox");
		Vehicle[i][ExpoY] = cache_get_field_content_float(i, "expoy");
		Vehicle[i][ExpoZ] = cache_get_field_content_float(i, "expoz");
		Vehicle[i][ExpoA] = cache_get_field_content_float(i, "expoa");
		Vehicle[i][TotalLlaves] = cache_get_field_content_int(i, "totalllaves");
		Vehicle[i][A1] = cache_get_field_content_int(i, "a1");
		Vehicle[i][A2] = cache_get_field_content_int(i, "a2");
		Vehicle[i][A3] = cache_get_field_content_int(i, "a3");
		Vehicle[i][A4] = cache_get_field_content_int(i, "a4");
		Vehicle[i][A5] = cache_get_field_content_int(i, "a5");
		Vehicle[i][M1] = cache_get_field_content_int(i, "m1");
		Vehicle[i][M2] = cache_get_field_content_int(i, "m2");
		Vehicle[i][M3] = cache_get_field_content_int(i, "m3");
		Vehicle[i][M4] = cache_get_field_content_int(i, "m4");
		Vehicle[i][M5] = cache_get_field_content_int(i, "m5");
		Vehicle[i][Vida] = cache_get_field_content_float(i, "vida");
	        Vehicle[i][Created] = false;
		CreateVehicleByIDX(i);
	}
	printf(">> Loaded %d vehicles from DB.", cache_num_rows());
}

// Creating the vehicles by IDX (idx is the row ID).
CreateVehicleByIDX(idx)
{
	if(Vehicle[idx][Created]) {
		DestroyVehicle(Vehicle[idx][VehicleID]);
		Delete3DTextLabel(Vehicle[idx][NameLabel]);
	}
        // We create the car ONLY if STATS = 0 (SPAWNED)
	if(Vehicle[idx][Stats] == 0) {
		Vehicle[idx][VehicleID] = CreateVehicle(Vehicle[idx][Model], Vehicle[idx][PosX], Vehicle[idx][PosY], Vehicle[idx][PosZ], Vehicle[idx][PosA], Vehicle[idx][Color1], Vehicle[idx][Color1], -1);
		SetVehicleNumberPlate(Vehicle[idx][VehicleID], VehiclePlate(idx));
		SetVehicleHealth(Vehicle[idx][VehicleID], Vehicle[idx][Vida]);
		Vehicle[idx][Created] = true;
	}
}

// Loading the user vehicles (onplayerspawn, this is the tquery function response).
public OnVehiclesUserLoad()
{
	for(new i = 0; i < cache_num_rows(); i++)
	{
		VehicleUser[i][ID] = cache_get_field_content_int(i, "ID");
		cache_get_field_content(i, "plate", str);
		strpack(VehicleUser[i][Plate], str, 30);
		VehicleUser[i][Model] = cache_get_field_content_int(i, "model");
		VehicleUser[i][Color1] = cache_get_field_content_int(i, "color1");
		VehicleUser[i][Color2] = cache_get_field_content_int(i, "color2");
		VehicleUser[i][PosX] = cache_get_field_content_float(i, "posx");
		VehicleUser[i][PosY] = cache_get_field_content_float(i, "posy");
		VehicleUser[i][PosZ] = cache_get_field_content_float(i, "posz");
		VehicleUser[i][PosA] = cache_get_field_content_float(i, "posa");
        cache_get_field_content(i, "llaves1", str);
		strpack(VehicleUser[i][Llaves1], str, 30);
		cache_get_field_content(i, "llaves2", str);
		strpack(VehicleUser[i][Llaves2], str, 30);
		cache_get_field_content(i, "llaves3", str);
		strpack(VehicleUser[i][Llaves3], str, 30);
		cache_get_field_content(i, "llaves4", str);
		strpack(VehicleUser[i][Llaves4], str, 30);
		cache_get_field_content(i, "dueno", str);
		strpack(VehicleUser[i][Dueno], str, 30);
		VehicleUser[i][Bloqueo] = cache_get_field_content_int(i, "bloqueo");
		VehicleUser[i][Precio] = cache_get_field_content_int(i, "precio");
		VehicleUser[i][Venta] = cache_get_field_content_int(i, "venta");
		VehicleUser[i][Stats] = cache_get_field_content_int(i, "stats");
		VehicleUser[i][TotalLlaves] = cache_get_field_content_int(i, "totalllaves");
		VehicleUser[i][A1] = cache_get_field_content_int(i, "a1");
		VehicleUser[i][A2] = cache_get_field_content_int(i, "a2");
		VehicleUser[i][A3] = cache_get_field_content_int(i, "a3");
		VehicleUser[i][A4] = cache_get_field_content_int(i, "a4");
		VehicleUser[i][A5] = cache_get_field_content_int(i, "a5");
		VehicleUser[i][M1] = cache_get_field_content_int(i, "m1");
		VehicleUser[i][M2] = cache_get_field_content_int(i, "m2");
		VehicleUser[i][M3] = cache_get_field_content_int(i, "m3");
		VehicleUser[i][M4] = cache_get_field_content_int(i, "m4");
		VehicleUser[i][M5] = cache_get_field_content_int(i, "m5");
		VehicleUser[i][Vida] = cache_get_field_content_float(i, "vida");
		if(VehicleUser[i][Stats] == 0)
		{
	    	VehicleUser[i][Created] = true;
		}
		else
		{
  			VehicleUser[i][Created] = false;
		}
	}
}
Reply
#4

Quote:
Originally Posted by HidroDF
Посмотреть сообщение
...
Don't think I really get what the problem is.

Quote:
Originally Posted by HidroDF
Посмотреть сообщение
Works, but I have a bug. When I "save" a car, the other cars gets another ID's.
Mainly this part.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)