If and Else statements problem
#21

Quote:
Originally Posted by Calisthenics
Посмотреть сообщение
When `Owner` is empty, `strcmp` function will return 0. A simple solution would be:
pawn Код:
if (!isnull(AHouseData[HouseID][Owner]) && strcmp(AHouseData[HouseID][Owner], Name, false) == 0)
Every time the player will (re)spawn, it will loop through all the houses and compare names. There are many ways to improve it.

1) Global variable as a house counter. If MAX_HOUSES is 1000 but only 10 houses were loaded, it will loop 10 times instead of 1000.
2) Assign owner unique ID and compare integers instead of strings.
3) On success login: find the house index and assign it to `APlayerData[playerid][CurrentHouse]`. No more unnecessary loops.
4) Change the spawn location using `SetSpawnInfo` function. If the player owns a house, set the x, y and z of a house otherwise from the random trucker location. In `OnPlayerSpawn` check the value of `CurrentHouse`, if it is a house then set the interior to the correct one.
Thanks for your detailed reply. For the first part of your code
pawn Код:
if (!isnull(AHouseData[HouseID][Owner]) && strcmp(AHouseData[HouseID][Owner], Name, false) == 0)
, do I put this in OnPlayerSpawn or OnPlayerRequestSpawn with the trucker spawn location? One of the previous posts suggested that I put it in OnPlayerSpawn but I can change it back if deemed necessary.
Reply
#22

Neither of them. Find the house (if any) only once after the player successfully logs in.

My approach on this would be to select the house unique id from the database that a player owns. If no rows found (not owning any house), I would set spawn info for the random trucker location otherwise I would compare house unique id to find the house index and assign to `CurrentHouse`. With this method, you do not need a loop at all if the player does not own a house.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)