/createhouse help, REP !
#1

Hello, I've made a command /createhouse [price] [level] and my problem is when I do /createhouse 10000 1, many time, the house id always the same, I want the house id from 0 to 1,2,3,4, etc. Help me... I will rep if my problem is fixed.

Код:
CMD:createhouse(playerid, params[])
{
	if(PlayerInfo[playerid][pAdmin] < 5)
	{
		SCM(playerid, COLOR_GRAD2,""COL_RED"You are not authorized to use this command!");
		return 1;
	}
	if(AdminDuty[playerid] != 1 && PlayerInfo[playerid][pAdmin] < 6)
 	{
  		SCM(playerid,COLOR_WHITE,""COL_RED"You're not on-duty as admin. To access your admin commands you must be on-duty. Type /aduty to go on-duty.");
		return 1;
 	}
	new price, string[128], level;
    if(sscanf(params, "dd", price, level))
    {
        SCM(playerid, COLOR_GRAD1, ""COL_BLUE"USAGE:"COL_WHITE" /createhouse [price] [level]");
        return 1;
	}
	for(new idx=1; idx<MAX_HOUSES; idx++)
	{
		GetPlayerPos(playerid, HouseInfo[idx][hExteriorX], HouseInfo[idx][hExteriorY], HouseInfo[idx][hExteriorZ]);
		GetPlayerFacingAngle(playerid, HouseInfo[idx][hExteriorA]);
		HouseInfo[idx][hValue] = price;
		HouseInfo[idx][hLevel] = level;
		HouseInfo[idx][hOwned] = 0;
		HouseInfo[idx][hLock] = 1;
		format(string, sizeof(string), "This house is for sale!\n Level: %d\nID: %d\nPrice: $%d\nTo buy this house type /buyhouse",HouseInfo[idx][hLevel],idx,HouseInfo[idx][hValue]);
		HouseInfo[idx][hTextID] = CreateDynamic3DTextLabel(string, COLOR_HOUSEGREEN, HouseInfo[idx][hExteriorX], HouseInfo[idx][hExteriorY], HouseInfo[idx][hExteriorZ]+0.5,3);
		HouseInfo[idx][hPickupID] = CreateDynamicPickup(1273, 23, HouseInfo[idx][hExteriorX], HouseInfo[idx][hExteriorY], HouseInfo[idx][hExteriorZ]);
		SaveHouses();
		format(string, sizeof(string), "AdmcCmd: %s has created house ID %d.", GetPlayerNameEx(playerid), idx);
		ABroadCast(COLOR_LIGHTRED, string, 2);
		idx = MAX_HOUSES;
	}
	return 1;
}
Reply
#2

Ain't sure but give a try

pawn Код:
GetPlayerPos(playerid, HouseInfo[idx][hExteriorX], HouseInfo[idx][hExteriorY], HouseInfo[idx][hExteriorZ]);
        GetPlayerFacingAngle(playerid, HouseInfo[idx][hExteriorA]);
        HouseInfo[idx][hValue] = price;
        HouseInfo[idx][hLevel] = level;
        HouseInfo[idx][hOwned] = 0;
        HouseInfo[idx][hLock] = 1;
        format(string, sizeof(string), "This house is for sale!\n Level: %d\nID: %d\nPrice: $%d\nTo buy this house type /buyhouse",HouseInfo[idx][hLevel],idx,HouseInfo[idx][hValue]);
        HouseInfo[idx][hTextID] = CreateDynamic3DTextLabel(string, COLOR_HOUSEGREEN, HouseInfo[idx][hExteriorX], HouseInfo[idx][hExteriorY], HouseInfo[idx][hExteriorZ]+0.5,3);
        HouseInfo[idx][hPickupID] = CreateDynamicPickup(1273, 23, HouseInfo[idx][hExteriorX], HouseInfo[idx][hExteriorY], HouseInfo[idx][hExteriorZ]);
        SaveHouses();
        idx = MAX_HOUSES;
        format(string, sizeof(string), "AdmcCmd: %s has created house ID %d.", GetPlayerNameEx(playerid), idx);
        ABroadCast(COLOR_LIGHTRED, string, 2);
Reply
#3

You need to count total house when loading
Example :
pawn Код:
#define MAX_HOUSES (500)
new g_TotalHouses = 0;

forward LoadHouses();
public LoadHouses()
{
    new i;
    if(cache_num_rows() > 0)
    {
        while(i < cache_num_rows())
        {
            //loading data
            i++;
           
        }

        if(i > 0)
        {
            g_TotalHouses = i;
            printf("[DB] Total house loaded: %d", g_TotalHouses);
        }
    }
    else
    {
        print("[DB] Can't load any house from database, it's empty ?");
    }
}

new query[128];
format(query, sizeof query, "SELECT * FROM houses LIMIT %d", MAX_HOUSES);
mysql_tquery(g_MySQL, query, "LoadHouses", "");


CMD:createhouse(playerid, params[])
{
    if(PlayerInfo[playerid][pAdmin] < 5)
    {
        SCM(playerid, COLOR_GRAD2,""COL_RED"You are not authorized to use this command!");
        return 1;
    }
    if(AdminDuty[playerid] != 1 && PlayerInfo[playerid][pAdmin] < 6)
    {
        SCM(playerid,COLOR_WHITE,""COL_RED"You're not on-duty as admin. To access your admin commands you must be on-duty. Type /aduty to go on-duty.");
        return 1;
    }
    new price, string[128], level;
    if(sscanf(params, "dd", price, level))
    {
        SCM(playerid, COLOR_GRAD1, ""COL_BLUE"USAGE:"COL_WHITE" /createhouse [price] [level]");
        return 1;
    }
    if(g_TotalHouses >= MAX_HOUSES) return SCM(playerid, -1, "House slot already full !");
    new idx = g_TotalHouses;
    GetPlayerPos(playerid, HouseInfo[idx][hExteriorX], HouseInfo[idx][hExteriorY], HouseInfo[idx][hExteriorZ]);
    GetPlayerFacingAngle(playerid, HouseInfo[idx][hExteriorA]);
    HouseInfo[idx][hValue] = price;
    HouseInfo[idx][hLevel] = level;
    HouseInfo[idx][hOwned] = 0;
    HouseInfo[idx][hLock] = 1;
    format(string, sizeof(string), "This house is for sale!\n Level: %d\nID: %d\nPrice: $%d\nTo buy this house type /buyhouse",HouseInfo[idx][hLevel],idx,HouseInfo[idx][hValue]);
    HouseInfo[idx][hTextID] = CreateDynamic3DTextLabel(string, COLOR_HOUSEGREEN, HouseInfo[idx][hExteriorX], HouseInfo[idx][hExteriorY], HouseInfo[idx][hExteriorZ]+0.5,3);
    HouseInfo[idx][hPickupID] = CreateDynamicPickup(1273, 23, HouseInfo[idx][hExteriorX], HouseInfo[idx][hExteriorY], HouseInfo[idx][hExteriorZ]);
    SaveHouses();
    format(string, sizeof(string), "AdmcCmd: %s has created house ID %d.", GetPlayerNameEx(playerid), idx);
    ABroadCast(COLOR_LIGHTRED, string, 2);
    g_TotalHouses++;
    return 1;
}
Reply
#4

pawn Код:
CMD:createhouse(playerid, params[])
{
    if(PlayerInfo[playerid][pAdmin] < 5) return SCM(playerid, COLOR_GRAD2,""COL_RED"You are not authorized to use this command!");
    if(!AdminDuty[playerid] && PlayerInfo[playerid][pAdmin] < 6) return SCM(playerid,COLOR_WHITE,""COL_RED"You're not on-duty as admin. To access your admin commands you must be on-duty. Type /aduty to go on-duty.");
    new price, level;
    if(sscanf(params, "dd", price, level)) return SCM(playerid, COLOR_GRAD1, ""COL_BLUE"USAGE:"COL_WHITE" /createhouse [price] [level]");
    for(new idx = 1; idx < MAX_HOUSES; idx++)
    {
        if(HouseInfo[idx][hValue]) continue; //If the house exists, its value would be greater than 0. So skip owned houses...
        GetPlayerPos(playerid, HouseInfo[idx][hExteriorX], HouseInfo[idx][hExteriorY], HouseInfo[idx][hExteriorZ]);
        GetPlayerFacingAngle(playerid, HouseInfo[idx][hExteriorA]);
        HouseInfo[idx][hValue] = price;
        HouseInfo[idx][hLevel] = level;
        HouseInfo[idx][hOwned] = 0;
        HouseInfo[idx][hLock] = 1;
        format(string, sizeof(string), "This house is for sale!\n Level: %d\nID: %d\nPrice: $%d\nTo buy this house type /buyhouse",HouseInfo[idx][hLevel],idx,HouseInfo[idx][hValue]);
        HouseInfo[idx][hTextID] = CreateDynamic3DTextLabel(string, COLOR_HOUSEGREEN, HouseInfo[idx][hExteriorX], HouseInfo[idx][hExteriorY], HouseInfo[idx][hExteriorZ]+0.5,3);
        HouseInfo[idx][hPickupID] = CreateDynamicPickup(1273, 23, HouseInfo[idx][hExteriorX], HouseInfo[idx][hExteriorY], HouseInfo[idx][hExteriorZ]);
        SaveHouses();
        format(string, sizeof(string), "AdmcCmd: %s has created house ID %d.", GetPlayerNameEx(playerid), idx);
        ABroadCast(COLOR_LIGHTRED, string, 2);
        break;
    }
    return 1;
}
I'm just going to assume that when a house does not exist, its 'hValue' is equal to 0. Thus when a house value does not equal 0, we can assume that it already exists and we can skip it in the loop by using 'continue'.

Also, I'm not sure why you used idx = MAX_HOUSES; but you can just use 'break' to break a loop and stop it from continuing.
Reply
#5

Quote:
Originally Posted by Threshold
Посмотреть сообщение
pawn Код:
CMD:createhouse(playerid, params[])
{
    if(PlayerInfo[playerid][pAdmin] < 5) return SCM(playerid, COLOR_GRAD2,""COL_RED"You are not authorized to use this command!");
    if(!AdminDuty[playerid] && PlayerInfo[playerid][pAdmin] < 6) return SCM(playerid,COLOR_WHITE,""COL_RED"You're not on-duty as admin. To access your admin commands you must be on-duty. Type /aduty to go on-duty.");
    new price, level;
    if(sscanf(params, "dd", price, level)) return SCM(playerid, COLOR_GRAD1, ""COL_BLUE"USAGE:"COL_WHITE" /createhouse [price] [level]");
    for(new idx = 1; idx < MAX_HOUSES; idx++)
    {
        if(HouseInfo[idx][hValue]) continue; //If the house exists, its value would be greater than 0. So skip owned houses...
        GetPlayerPos(playerid, HouseInfo[idx][hExteriorX], HouseInfo[idx][hExteriorY], HouseInfo[idx][hExteriorZ]);
        GetPlayerFacingAngle(playerid, HouseInfo[idx][hExteriorA]);
        HouseInfo[idx][hValue] = price;
        HouseInfo[idx][hLevel] = level;
        HouseInfo[idx][hOwned] = 0;
        HouseInfo[idx][hLock] = 1;
        format(string, sizeof(string), "This house is for sale!\n Level: %d\nID: %d\nPrice: $%d\nTo buy this house type /buyhouse",HouseInfo[idx][hLevel],idx,HouseInfo[idx][hValue]);
        HouseInfo[idx][hTextID] = CreateDynamic3DTextLabel(string, COLOR_HOUSEGREEN, HouseInfo[idx][hExteriorX], HouseInfo[idx][hExteriorY], HouseInfo[idx][hExteriorZ]+0.5,3);
        HouseInfo[idx][hPickupID] = CreateDynamicPickup(1273, 23, HouseInfo[idx][hExteriorX], HouseInfo[idx][hExteriorY], HouseInfo[idx][hExteriorZ]);
        SaveHouses();
        format(string, sizeof(string), "AdmcCmd: %s has created house ID %d.", GetPlayerNameEx(playerid), idx);
        ABroadCast(COLOR_LIGHTRED, string, 2);
        break;
    }
    return 1;
}
I'm just going to assume that when a house does not exist, its 'hValue' is equal to 0. Thus when a house value does not equal 0, we can assume that it already exists and we can skip it in the loop by using 'continue'.

Also, I'm not sure why you used idx = MAX_HOUSES; but you can just use 'break' to break a loop and stop it from continuing.
It's working ! Thanks ! Repped..
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)