Vehicle ID's Loading in random order. MySQL R41
#1

Hey guys. I have a vehicle system and I've rebuilt it so that in the database it isn't just carid that is auto incremented and so when deleting and creating vehicles it doesn't make a mess.

However now I've come into an issue.

My issue, is in my database it won't the vehicle to load in order of the carid's in the database. For example I've made a check statement for loading the vehicles in the output and I get.

Код:
Vehicle ID: 0 Vehicle Model: 523
Vehicle ID: 1 Vehicle Model: 523
Vehicle ID: 2 Vehicle Model: 596
Vehicle ID: 3 Vehicle Model: 596
Vehicle ID: 4 Vehicle Model: 596
Vehicle ID: 5 Vehicle Model: 426
Vehicle ID: 6 Vehicle Model: 426
Vehicle ID: 7 Vehicle Model: 560
Vehicle ID: 8 Vehicle Model: 560
Vehicle ID: 9 Vehicle Model: 541
Vehicle ID: 10 Vehicle Model: 544
It seems to be grouping the vehicle model somewhat backwards However what it looks like in the DB is.
Код:
Vehicle ID: 0 Vehicle Model: 544
Vehicle ID: 1 Vehicle Model: 541
Vehicle ID: 2 Vehicle Model: 560
Vehicle ID: 3 Vehicle Model: 560
Vehicle ID: 4 Vehicle Model: 426
Vehicle ID: 5 Vehicle Model: 426
Vehicle ID: 6 Vehicle Model: 523
Vehicle ID: 7 Vehicle Model: 523
Vehicle ID: 8 Vehicle Model: 596
Vehicle ID: 9 Vehicle Model: 596
Vehicle ID: 10 Vehicle Model: 596
Here is my load code.
Код:
public Car_Load()
{
	static
	    rows,
	    fields,
		str[128];

	cache_get_row_count(rows); 
	cache_get_field_count(fields);
	for (new i = 0; i < rows; i ++) if (i < MAX_DYNAMIC_CARS)
	{
	    CarData[i][carExists] = true;
	    cache_get_value_name_int(i, "carID",CarData[i][carID]);
	    cache_get_value_name_int(i, "carModel",CarData[i][carModel]);
	    cache_get_value_name_int(i, "carOwner",CarData[i][carOwner]);
	    cache_get_value_name_float(i, "carPosX",CarData[i][carPos][0]);
	    cache_get_value_name_float(i, "carPosY",CarData[i][carPos][1]);
		cache_get_value_name_float(i, "carPosZ",CarData[i][carPos][2]);
	    cache_get_value_name_float(i, "carPosR",CarData[i][carPos][3]);
	    cache_get_value_name_int(i, "carColor1",CarData[i][carColor1]);
	    cache_get_value_name_int(i, "carColor2",CarData[i][carColor2]);
	    cache_get_value_name_int(i, "carPaintjob",CarData[i][carPaintjob]);
	    cache_get_value_name_int(i, "carLocked",CarData[i][carLocked]);
	    cache_get_value_name_int(i, "carImpounded",CarData[i][carImpounded]);
	    cache_get_value_name_int(i, "carImpoundPrice",CarData[i][carImpoundPrice]);
        cache_get_value_name_int(i, "carFaction",CarData[i][carFaction]);
        cache_get_value_name_int(i, "carSiren",CarData[i][carSiren]);
		printf("Vehicle ID: %d Vehicle Model: %d", Iter_Count(Vehs), CarData[i][carModel]);
		for (new j = 0; j < 14; j ++)
		{
		    if (j < 5)
		    {
		        format(str, sizeof(str), "carWeapon%d", j + 1);
		        cache_get_value_name_int(i, str, CarData[i][carWeapons][j]);

		        format(str, sizeof(str), "carAmmo%d", j + 1);
				cache_get_value_name_int(i, str,CarData[i][carAmmo][j]);
	        }
	        format(str, sizeof(str), "carMod%d", j + 1);
	        cache_get_value_name_int(i, str,CarData[i][carMods][j]);
	    }
		
		Iter_Add(Vehs, i);
		Car_Spawn(i);
	}
	for (new j = 0; j < MAX_DYNAMIC_CARS; j ++) if (CarData[j][carExists]) {
		format(str, sizeof(str), "SELECT * FROM `carstorage` WHERE `ID` = '%d'", CarData[j][carID]);

		mysql_tquery(g_iHandle, str, "OnLoadCarStorage", "d", j);
	}
	printf("  [Vehicle System] Loaded %d Vehicles.", Iter_Count(Vehs));
	return 1;
}
My Car save, also I haven't used the car save function I've inputted these in through the database SQL option.
Код:
Car_Save(carid)
{
	static
	    query[900];
	if(!Iter_Contains(Vehs, carid)) return 0;
	if (CarData[carid][carVehicle] != INVALID_VEHICLE_ID)
	{
	    for (new i = 0; i < 14; i ++) {
			CarData[carid][carMods][i] = GetVehicleComponentInSlot(CarData[carid][carVehicle], i);
	    }
	}
	format(query, sizeof(query), "UPDATE `Vehicles` SET `carModel` = '%d', `carOwner` = '%d', `carPosX` = '%.4f', `carPosY` = '%.4f', `carPosZ` = '%.4f', `carPosR` = '%.4f', `carColor1` = '%d', `carColor2` = '%d', `carPaintjob` = '%d', `carLocked` = '%d'",
        CarData[carid][carModel],
        CarData[carid][carOwner],
        CarData[carid][carPos][0],
        CarData[carid][carPos][1],
        CarData[carid][carPos][2],
        CarData[carid][carPos][3],
        CarData[carid][carColor1],
        CarData[carid][carColor2],
        CarData[carid][carPaintjob],
        CarData[carid][carLocked]
	);
	format(query, sizeof(query), "%s, `carMod1` = '%d', `carMod2` = '%d', `carMod3` = '%d', `carMod4` = '%d', `carMod5` = '%d', `carMod6` = '%d', `carMod7` = '%d', `carMod8` = '%d', `carMod9` = '%d', `carMod10` = '%d', `carMod11` = '%d', `carMod12` = '%d', `carMod13` = '%d', `carMod14` = '%d'",
		query,
		CarData[carid][carMods][0],
		CarData[carid][carMods][1],
		CarData[carid][carMods][2],
		CarData[carid][carMods][3],
		CarData[carid][carMods][4],
		CarData[carid][carMods][5],
		CarData[carid][carMods][6],
		CarData[carid][carMods][7],
		CarData[carid][carMods][8],
		CarData[carid][carMods][9],
		CarData[carid][carMods][10],
		CarData[carid][carMods][11],
		CarData[carid][carMods][12],
		CarData[carid][carMods][13]
	);
	format(query, sizeof(query), "%s, `carImpounded` = '%d', `carImpoundPrice` = '%d', `carFaction` = '%d' , `carSiren` = '%d', `carWeapon1` = '%d', `carWeapon2` = '%d', `carWeapon3` = '%d', `carWeapon4` = '%d', `carWeapon5` = '%d', `carAmmo1` = '%d', `carAmmo2` = '%d', `carAmmo3` = '%d', `carAmmo4` = '%d', `carAmmo5` = '%d' WHERE `carID` = '%d'",
		query,
		CarData[carid][carImpounded],
		CarData[carid][carImpoundPrice],
		CarData[carid][carFaction],
		CarData[carid][carSiren],
		CarData[carid][carWeapons][0],
		CarData[carid][carWeapons][1],
		CarData[carid][carWeapons][2],
		CarData[carid][carWeapons][3],
		CarData[carid][carWeapons][4],
		CarData[carid][carAmmo][0],
		CarData[carid][carAmmo][1],
		CarData[carid][carAmmo][2],
		CarData[carid][carAmmo][3],
		CarData[carid][carAmmo][4],
		CarData[carid][carID]
	);
	return mysql_tquery(g_iHandle, query);
}
Any help would be much appreciated
Reply
#2

Its nothing wrong, when data is loadin from database its loading from ID 0 to maximum ID. If you want the cars to be loaded by model you can use GROUP BY in the SELECT query but its same thing
Reply
#3

But I want them to load so that the ID matches the database as ingame? I want the vehicles to be loaded so that Car ID 0 in the database is equal to car ID 0 in the game.
Reply
#4

How would you suggest I create this system then? Can I get some recommendations?
Reply
#5

Database should have a primary key (auto increment) to identify each vehicle. The rest of the data such as model is what you only need. Then you store in variable the ID of the vehicle so you can update the database if needed or delete records from it. The in-game vehicleid is not meant to be stored, you use it as index in arrays.
Reply
#6

Can you give me an example?
Reply
#7

CarData has size of say MAX_VEHICLES. What you can do is store the data in local variables:
pawn Код:
cache_get_value_name_int(i, "carID", car_ID);
cache_get_value_name_int(i, "carModel", car_Model);
cache_get_value_name_int(i, "carOwner", car_Owner);
cache_get_value_name_float(i, "carPosX", car_Pos[0]);
cache_get_value_name_float(i, "carPosY", car_Pos[1]);
cache_get_value_name_float(i, "carPosZ", car_Pos[2]);
cache_get_value_name_float(i, "carPosR", car_Pos[3]);
cache_get_value_name_int(i, "carColor1", car_Color1);
cache_get_value_name_int(i, "carColor2", car_Color2);
...
and then create the vehicle
pawn Код:
new created_vehicle = CreateVehicle(car_Model, car_Pos[0], car_Pos[1], car_Pos[2], car_Pos[3], car_Color1, car_Color2, respawn_delay_here, siren_here..);
pawn Код:
if (created_vehicle != INVALID_VEHICLE_ID) // not reached limit or invalid modelid
{
    //assign vehicleid as index
    CarData[created_vehicle][carID] = car_ID;
    CarData[created_vehicle][carModel] = car_Model;
    ...
}
so now anything related to vehicle in-game as to what player is currently in, you can use the index to retrieve data from it. However, when you want to save the said vehicle:
pawn Код:
"UPDATE ... SET ... WHERE carID=%d", ..., CarData[iteration_variable_here][carID]);
And do not store components in the same table, use a different table (child) and carID will be the FOREIGN KEY: https://sampforum.blast.hk/showthread.php?tid=420363
Reply
#8

Okay. Well I had that originally. Before I somewhat rebuilt my code. But the problem I was having was the fact that the ID in the database table is Auto Incremented so if I deleted a car I would lost it completely. So I tried to recreate it so it wasn't reliant on the carid being auto incremented. If you could help me with that, it would be awesome.

PHP код:
public Car_Load()
{
    static
        
rows,
        
fields,
        
str[128];
    
cache_get_data(rowsfieldsg_iHandle);
    for (new 
0rows++) if (MAX_DYNAMIC_CARS)
    {
        
CarData[i][carExists] = true;
        
CarData[i][carID] = cache_get_field_int(i"carID");
        
CarData[i][carModel] = cache_get_field_int(i"carModel");
        
CarData[i][carOwner] = cache_get_field_int(i"carOwner");
        
CarData[i][carPos][0] = cache_get_field_float(i"carPosX");
        
CarData[i][carPos][1] = cache_get_field_float(i"carPosY");
        
CarData[i][carPos][2] = cache_get_field_float(i"carPosZ");
        
CarData[i][carPos][3] = cache_get_field_float(i"carPosR");
        
CarData[i][carColor1] = cache_get_field_int(i"carColor1");
        
CarData[i][carColor2] = cache_get_field_int(i"carColor2");
        
CarData[i][carPaintjob] = cache_get_field_int(i"carPaintjob");
        
CarData[i][carLocked] = cache_get_field_int(i"carLocked");
        
CarData[i][carImpounded] = cache_get_field_int(i"carImpounded");
        
CarData[i][carImpoundPrice] = cache_get_field_int(i"carImpoundPrice");
        
CarData[i][carFaction] = cache_get_field_int(i"carFaction");
        for (new 
014++)
        {
            if (
5)
            {
                
format(strsizeof(str), "carWeapon%d"1);
                
CarData[i][carWeapons][j] = cache_get_field_int(istr);
                
format(strsizeof(str), "carAmmo%d"1);
                
CarData[i][carAmmo][j] = cache_get_field_int(istr);
            }
            
format(strsizeof(str), "carMod%d"1);
            
CarData[i][carMods][j] = cache_get_field_int(istr);
        }
        
Car_Spawn(i);
    }
    for (new 
0MAX_DYNAMIC_CARS++) if (CarData[i][carExists]) {
        
format(strsizeof(str), "SELECT * FROM `carstorage` WHERE `ID` = '%d'"CarData[i][carID]);
        
mysql_tquery(g_iHandlestr"OnLoadCarStorage""d"i);
    }
    return 
1;

Reply
#9

Again, the ID in the database is meant to be used as a way to identify a vehicle to update/delete. Nothing more, nothing less. You should not care if there are 2 records like this:
pawn Код:
ID
----
1
2345
as the array would have 2 only indexes:
pawn Код:
CarData[1][carID] = 1;
CarData[2][carID] = 2345;
Since index 0 is empty, you can set any default value so you can reset any index very easy.

I advise you with a better method, it is up to you what you will use. Especially consider the components in another table, it is bad design all-in-one.
Reply
#10

I'm not really understanding how the method you said is any better than the one currently? I have most of my components in a different table these are mainly just checks and stuff. I really appreciate your help.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)