Get number of houses from array? - 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: Get number of houses from array? (
/showthread.php?tid=608227)
Get number of houses from array? -
CloudLEL - 29.05.2016
Hi,
I was wondering, is it possible to count how many houses their are without using mysql.
I've been doing it by getting the row count from the database, but I feel like their must be an easier way to do it.
Thanks.
Re: Get number of houses from array? -
iKarim - 29.05.2016
why don't you just use row count? it is harder to count an array. you'll have to loop through all elements until it reaches an unused cell.
Re: Get number of houses from array? -
CloudLEL - 29.05.2016
If that's what you think is best, I'll continue doing so.
Re: Get number of houses from array? -
DRIFT_HUNTER - 29.05.2016
Quote:
Originally Posted by PawnHunter
why don't you just use row count? it is harder to count an array. you'll have to loop through all elements until it reaches an unused cell.
|
And mysql has to send request, wait for mysql server to select all rows, count them and than return result. Looping thru array is 1000x times faster (literally).
Just loop thru your houses and skip unused cells/rows. My simple trick is to check if houses entrance Z is 0 or if label/icon is invalid. Simple example:
pawn Код:
stock CountHouses()
{
new count = 0;
for(new i = 0; i < MAX_HOUSES; i++)
{
if(HouseInfo[i][EntZ] != 0.0) count++;
}
return count;
}
While EntZ should be float and comparing floats is discouraged feel free to do it in pawn.
Re: Get number of houses from array? -
Konstantinos - 29.05.2016
Quote:
Originally Posted by DRIFT_HUNTER
Just loop thru your houses and skip unused cells/rows.
|
Storing the number of houses loaded from the database when the server starts in a global variable and increasing/decreasing by 1 when a house is created/deleted respectively is the best way.
Re: Get number of houses from array? -
DRIFT_HUNTER - 29.05.2016
Quote:
Originally Posted by Konstantinos
Storing the number of houses loaded from the database when the server starts in a global variable and increasing/decreasing by 1 when a house is created/deleted respectively is the best way.
|
Or that
Re: Get number of houses from array? -
Vince - 29.05.2016
If the only purpose of a query is to get a count of something then the COUNT() aggregate function should be used. Sending over a single value is obviously much faster than sending over an entire result set which isn't going to be used.
Re: Get number of houses from array? -
AlexBlack - 29.05.2016
Or more simple, use foreach & y_iterator.