SA-MP Forums Archive
House system problem - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: House system problem (/showthread.php?tid=602680)



House system problem - Sanady - 11.03.2016

Hello SA-MP Forums, week before, I made a load and save system for house system which I want to use, I am using mySQL. But there is a problem like a always. When I want to create house with command, error shows up to me that there is not free slot in mySQL database. Here is command:
pawn Код:
ADMCMD:createhouse(playerid, params[], 5)
{
    new price, Query[500];
    if(sscanf(params,"d", price)) return SendServer(playerid, "/createhouse [price]");
   
    new houseid = GetHouseFreeSlot();
    if(houseid == -1) return SendError(playerid, "There is no free slot on server for house at the moment. Please try again later.");
   
    format(Query, sizeof(Query), "INSERT INTO `houses` SET `ID` = '%d', `PosX` = '%f', `PosY` = '%f', `PosZ` = '%f', `Price` = '%d', `Owner` = '-1'", houseid, hInfo[houseid][hPosX], hInfo[houseid][hPosY], hInfo[houseid][hPosZ], price);
    mysql_query(Query);
   
    hInfo[houseid][hID] = mysql_insert_id();
    hInfo[houseid][hPrice] = price;
   
    SendMessage(playerid, -1, embed_housecolor"[House - #%d] "embed_white"You have succssesfully created house!", houseid);
    return 1;
}
Also I will post my function which is checking if mysql have free slot:
pawn Код:
stock GetHouseFreeSlot()
{
    for(new i = 0; i < MAX_HOUSES; i++)
    {
        if(hInfo[i][hID] == INVALID_HOUSE_ID) return i;
    }
    return -1;
}



Re: House system problem - dominik523 - 11.03.2016

Are you setting hID of each house to INVALID_HOUSE_ID when starting the server and under deleting houses?


Re: House system problem - Sanady - 11.03.2016

Quote:
Originally Posted by dominik523
Посмотреть сообщение
Are you setting hID of each house to INVALID_HOUSE_ID when starting the server and under deleting houses?
No I don`t. INVALID_HOUSE_ID is -1. All houses are starting from ID 0, so -1 is invalid.


Re: House system problem - dominik523 - 11.03.2016

Well, every hID is initialized to 0 by default and you are checking if it's equal to -1, since it will never be -1, you are always getting a message that all the house slots are full.
Do something like:
pawn Код:
// under OnGameModeInit
for(new i=0; i < MAX_HOUSES; i++)
    hInfo[i][hID] = INVALID_HOUSE_ID;



Re: House system problem - Sanady - 11.03.2016

Quote:
Originally Posted by dominik523
Посмотреть сообщение
Well, every hID is initialized to 0 by default and you are checking if it's equal to -1, since it will never be -1, you are always getting a message that all the house slots are full.
Do something like:
pawn Код:
// under OnGameModeInit
for(new i=0; i < MAX_HOUSES; i++)
    hInfo[i][hID] = INVALID_HOUSE_ID;
hInfo[i][hID] = INVALID_HOUSE_ID, that part of code I am using for getting index for my array.


Re: House system problem - Runn3R - 11.03.2016

You probably defined it as INVALID_HOUSE_ID inside of an enumator and that won't work.

Simply add if the house exists like Bool:hExists if it's false it doesn't exist if its true it exists.

Note that it will always be 0 when initializing so when you load a house you set it to true..

And you check it like:
Код:
stock GetHouseFreeSlot()
{
    for(new i = 0; i < MAX_HOUSES; i++)
	{
        if(hInfo[i][hExists] != true) return i;
	}
	return -1;
}



Re: House system problem - dominik523 - 11.03.2016

Just set every hID to -1 and then load the houses. Loaded houses will have it's index that will be loaded, all other houses that are not loaded will be able to be created because their hID will be -1. Right now you can't create and houses because your command thinks that every house has been spawned will ID of 0.


Re: House system problem - Sanady - 11.03.2016

Quote:
Originally Posted by Runn3R
Посмотреть сообщение
You probably defined it as INVALID_HOUSE_ID inside of an enumator and that won't work.

Simply add if the house exists like Bool:hExists if it's false it doesn't exist if its true it exists.

Note that it will always be 0 when initializing so when you load a house you set it to true..

And you check it like:
Код:
stock GetHouseFreeSlot()
{
    for(new i = 0; i < MAX_HOUSES; i++)
	{
        if(hInfo[i][hExists] != true) return i;
	}
	return -1;
}
I don`t understand, why should I made one more variable, is impossible to check free slots with unique house ID?


Re: House system problem - Runn3R - 11.03.2016

Yeah but you have to loop it in order to set it to -1.


Re: House system problem - Sanady - 11.03.2016

Quote:
Originally Posted by Runn3R
Посмотреть сообщение
Yeah but you have to loop it in order to set it to -1.
Something like I posted on the top of the topic ?