One thing that comes into mind as well when looking at your enum structure.
For the ID of the dealership, you can simply use the index of the array for that.
So the index is actually the dealership ID.
So you could load your data the same way as you do now, but use the ID field from your database as the index of your array to store all the data.
Then you're also sure you won't get duplicate ID's in your database, because arrays don't have duplicate ID's (or indices).
When creating a new dealership in-game, you could simply loop through your array and check for each index if the DsSpawned (or DsIcon) variables hold 0.
Then you know that ID isn't taken and you can use it to store your new dealership.
Also, when you create the new dealership and want to save the data, you can use that empty index of your array, where you stored the new data, as the ID of your new dealership.
I'm also in the process of creating everything in-game and I'm doing the same thing.
Some sample code, taken directly from my gamemode PPC Trucking V2:
pawn Код:
// OnFilterScriptInit:
// Send a query to load all gas-stations from MySQL
format(Query, sizeof(Query), "SELECT * FROM %s", table_gasstations);
result = mysql_query(SQL_db, Query, true);
// Print some debug info to the server console
printf("*** Loading gas-stations from MySQL using \"%s\"", Query);
GasStationsLoad();
cache_delete(result, SQL_db);
// This function is called to load the gas-stations
GasStationsLoad()
{
// Setup local variables
new Rows, GasID, Name[50], Float:x, Float:y, Float:z, CountSuccess, CountFailed;
// Get the amount of rows (gas-stations)
Rows = cache_get_row_count(SQL_db);
// If there are any rows (gas-stations) loaded, load data and create them
if (Rows >= 1)
{
// Loop through all rows
for (new Row; Row < Rows; Row++)
{
// Load the data
GasID = cache_get_field_content_int(Row, "ID", SQL_db);
cache_get_field_content(Row, "Name", Name, SQL_db, sizeof(Name));
x = cache_get_field_content_float(Row, "X", SQL_db);
y = cache_get_field_content_float(Row, "Y", SQL_db);
z = cache_get_field_content_float(Row, "Z", SQL_db);
// Check if the GasID is invalid (out of range)
if ((GasID < 0) || (GasID >= MAX_GASSTATIONS))
{
// Count the amount of failed gas-stations (invalid GasID's)
CountFailed++;
printf("*** ERROR: Invalid GasID found in table %s: %i", table_gasstations, GasID);
// Continue with the next gas-station from the MySQL query
continue;
}
// Create the gas-station at the given GasID (there should be no duplicate gas-stations with the same ID as the table-structure doesn't allow it, ID = primary key)
SetupGasStation(x, y, z, Name, GasID);
// Count the succesfully created gas-stations
CountSuccess++;
}
}
// Print the amount of gas-stations loaded for debugging
printf("*** Gas-stations loaded: %i (successful: %i, failed: %i)", Rows, CountSuccess, CountFailed);
return 1;
}
// This function creates a gas-station (store data and create pickup/map-icon)
SetupGasStation(Float:x, Float:y, Float:z, Name[], ID = -1)
{
// Setup local variables
new GasID, bool:SlotFound = false;
// Check if an ID has been given
if (ID == -1)
{
// No ID has been given, search for a free slot to create a new gas-station
// Loop through all gas-station slots
for (GasID = 0; GasID < MAX_GASSTATIONS; GasID++)
{
// If an empty slot is found (no valid pickup)
if (!IsValidDynamicPickup(AGasStations[GasID][PickupID]))
{
// An empty slot has been found
SlotFound = true;
// Exit the loop
break;
}
}
}
else // An ID has been specified
{
// Check if this slot is available
if (!IsValidDynamicPickup(AGasStations[ID][PickupID]))
{
// An empty slot has been found
SlotFound = true;
// Store the given ID as GasID
GasID = ID;
}
}
// Check if an empty slot was found
if (SlotFound == true)
{
// Store the pickup-id in this empty slot
AGasStations[GasID][PickupID] = CreateDynamicPickup(1244, 1, x, y, z, 0); // Type 1, cannot be pickup up, exists all the time
AGasStations[GasID][GasX] = x;
AGasStations[GasID][GasY] = y;
AGasStations[GasID][GasZ] = z;
format(AGasStations[GasID][GasName], 50, Name);
// Add a 3DText message above the pickup icon
AGasStations[GasID][PickupText] = CreateDynamic3DTextLabel("Press and hold Y-key\nto refuel your vehicle", 0x008080FF, x, y, z + 0.8, 30.0);
// Add an icon to the map for this gas-station
AGasStations[GasID][PickupIcon] = CreateDynamicMapIcon(x, y, z, 56, 0, 0, 0, -1, 300.0);
return GasID;
}
// If no gas-station could be added (no free slots), return -1 to indicate adding a gas-station has failed
return -1;
}
You'll see that the ID from my database is used as the index of my array.
You'll also see the I'm using mysql_query to load my data, you don't need to do that.
I did it this way because I want to be sure my data is loaded before the server is running.
Otherwise, with ALOT of data, it might take a few seconds to load it all and players might be able to connect already when the script didn't finish loading all the data yet.
Using mysql_query, the script halts execution of OnFilterScriptInit until all data has been loaded.
After the data is loaded, the rest of the code is executed and the server is open to the public.
Another issue in your code:
You seem to have one global variable "DsPickups" to which you assign the handle (or ID) of the pickup.
But this will only point to the last pickup you created, since the variable can only hold one value.
You should consider putting that variable inside your enum as well.
Then it's easier to delete the pickup when you delete the dealership in-game.
PS: sorry for the long post.