CMD:createhouse(playerid, params[])
{
if(!IsAdminLevel(playerid, 5)) return NoPermsMSG(playerid);
new price;
if(sscanf(params, "i", price)) return SendUsageMSG(playerid, "/createhouse [PRICE]");
if(price < 666) return SendErrorMSG(playerid, "Invalid house price.");
new Float:X, Float:Y, Float:Z, string[128];
GetPlayerPos(playerid, X, Y, Z);
CreateHouse(price, X, Y, Z);
SendAdminMSG(playerid, "You have created a new house.");
format(string, sizeof(string), "%s(%d) has created a new house.", PlayerName(playerid), playerid);
SendALogMSG(string);
return 1;
}
stock CreateHouse(price, Float:x, Float:y, Float:z)
{
new query[128];
mysql_format(mysql, query, sizeof(query), "INSERT INTO `houses` (`EnterX`, `EnterY`, `EnterZ`, `Price`) VALUES ('%f', '%f', '%f', '%d')", x, y, z, price);
mysql_tquery(mysql, query, "", "");
}
In mysql_tquery, specify a callback and in that callback use cache_insert_id to retrieve the unique ID (the column has to be set with auto increment). I can't say about the index (if you have variables for the house data at all) much as you didn't post any additional information but having a global variable that stores the number of loaded houses will come in handy, not only for the loops but it is the index of the next house (the house to be created).
|
forward LoadHouses();
public LoadHouses()
{
new rows, fields;
cache_get_data(rows, fields, mysql);
if(rows)
{
new housepw[4], housetitle[MAX_HOUSE_NAME];
for(new i = 0; i < cache_get_row_count(); i++)
{
hInfo[i][hID] = cache_get_row_int(i, 0);
cache_get_row(i, 2, housetitle);
cache_get_row(i, 3, housepw);
hInfo[hInfo[i][hID]][hOwner] = cache_get_row_int(i, 1);
hInfo[hInfo[i][hID]][hInterior] = cache_get_row_int(i, 10);
hInfo[hInfo[i][hID]][hPrice] = cache_get_row_int(i, 11);
hInfo[hInfo[i][hID]][hEnterX] = cache_get_row_float(i, 4);
hInfo[hInfo[i][hID]][hEnterY] = cache_get_row_float(i, 5);
hInfo[hInfo[i][hID]][hEnterZ] = cache_get_row_float(i, 6);
hInfo[hInfo[i][hID]][hExitX] = cache_get_row_float(i, 7);
hInfo[hInfo[i][hID]][hExitY] = cache_get_row_float(i, 8);
hInfo[hInfo[i][hID]][hExitZ] = cache_get_row_float(i, 9);
hInfo[hInfo[i][hID]][hTitle] = housetitle;
hInfo[hInfo[i][hID]][hPassword] = housepw;
hInfo[hInfo[i][hID]][hWorldID] = hInfo[i][hID];
new hEntStr[200];
if(hInfo[hInfo[i][hID]][hOwner] == -1)
{
format(hEntStr, sizeof(hEntStr), ""COL_GOLD"House: "COL_WHITE"%s(%d)\n"COL_GOLD"Owner: "COL_WHITE"No-one\n"COL_GOLD"Price: "COL_WHITE"$%s", hInfo[hInfo[i][hID]][hTitle], hInfo[i][hID], AC(hInfo[hInfo[i][hID]][hPrice]));
}
if(hInfo[hInfo[i][hID]][hOwner] != -1)
{
format(hEntStr, sizeof(hEntStr), ""COL_GOLD"House: "COL_WHITE"%s(%d)\n"COL_GOLD"Owner: "COL_WHITE"%s\n"COL_GOLD"Price: "COL_WHITE"$%s", hInfo[hInfo[i][hID]][hTitle], hInfo[i][hID], GetNameFromMySQLID(hInfo[hInfo[i][hID]][hOwner]), AC(hInfo[hInfo[i][hID]][hPrice]));
}
hInfo[hInfo[i][hID]][sEnterLabel] = CreateDynamic3DTextLabel(hEntStr, -1, hInfo[hInfo[i][hID]][hEnterX], hInfo[hInfo[i][hID]][hEnterY], hInfo[hInfo[i][hID]][hEnterZ], 20.0, INVALID_PLAYER_ID, INVALID_VEHICLE_ID, 1, -1, 0, -1, 100.0);
hInfo[hInfo[i][hID]][sExitLabel] = CreateDynamic3DTextLabel(""COL_GOLD"[EXIT]", -1, hInfo[hInfo[i][hID]][hExitX], hInfo[hInfo[i][hID]][hExitY], hInfo[hInfo[i][hID]][hExitZ], 20.0, INVALID_PLAYER_ID, INVALID_VEHICLE_ID, 1, hInfo[hInfo[i][hID]][hWorldID], hInfo[hInfo[i][hID]][hInterior], -1, 100.0);
hInfo[hInfo[i][hID]][hEnterCP] = CreateDynamicCP(hInfo[hInfo[i][hID]][hEnterX], hInfo[hInfo[i][hID]][hEnterY], hInfo[hInfo[i][hID]][hEnterZ], 1.0, -1, 0, -1, 100.0);
hInfo[hInfo[i][hID]][hExitCP] = CreateDynamicCP(hInfo[hInfo[i][hID]][hExitX], hInfo[hInfo[i][hID]][hExitY], hInfo[hInfo[i][hID]][hExitZ], 1.0, hInfo[hInfo[i][hID]][hWorldID], hInfo[hInfo[i][hID]][hInterior], -1, 100.0);
LoadedHouses++;
}
printf("Loaded %d houses", LoadedHouses);
}
else if(!rows)
{
printf("There are NO houses to load");
}
return 1;
}
if (LoadedHouses + 1 == sizeof hInfo) return ....
// error that limit was reached
...
// execute the query
// in the callback from mysql_tquery:
hInfo[LoadedHouses][hID] = cache_insert_id();
LoadedHouses++;
for (new i = 0; i < rows; i++)
Okay, your way can be vastly improved. "hID" will hold the unique ID in the table and that's all you need - you shouldn't use it as an index in the "hInfo" array because reloading the houses would be necessary and you don't want that. Use "i" as an index instead.
When it comes to create a new house: pawn Код:
On another note, you keep calling cache_get_row_count multiple times. You got "rows" already so just: pawn Код:
|
I can't understand anything tbh, because you're just saying put "load" bla bla without telling me where to put them.
|
hInfo[i][hID] = cache_get_row_int(i, 0); ... hInfo[hInfo[i][hID]][...]
hInfo[i][hID] = cache_get_row_int(i, 0); ... hInfo[i][...]
if (LoadedHouses + 1 == sizeof hInfo) return ... error: that limit was reached // rest of your code.. CreateHouse(price, X, Y, Z); // rest of your code..
mysql_tquery(mysql, query, "OnHouseCreate", "");
hInfo[LoadedHouses][hID] = cache_insert_id(); // retrieve the last unique ID from the table .. // .. "LoadedHouses" is the index of the array LoadedHouses++; // Increasing by 1 as a new house created
You are using the unique ID from the table as the index of the array
Код:
hInfo[i][hID] = cache_get_row_int(i, 0); ... hInfo[hInfo[i][hID]][...] Код:
hInfo[i][hID] = cache_get_row_int(i, 0); ... hInfo[i][...] In the /createhouse command: Код:
if (LoadedHouses + 1 == sizeof hInfo) return ... error: that limit was reached // rest of your code.. CreateHouse(price, X, Y, Z); // rest of your code.. Код:
mysql_tquery(mysql, query, "OnHouseCreate", ""); Код:
hInfo[LoadedHouses][hID] = cache_insert_id(); // retrieve the last unique ID from the table .. // .. "LoadedHouses" is the index of the array LoadedHouses++; // Increasing by 1 as a new house created |