If and Else statements problem
#1

The problem I'm having is that I have an if and else but it calls both. As you can see in the output below, else is called a lot of times before it prints out what happens in the if statement (the playerpos, etc).

Код:
			
			for (HouseID = 1; HouseID < MAX_HOUSES; HouseID++)
			{
				if (strcmp(AHouseData[HouseID][Owner], Name, false) == 0)
				{
					hLevel = AHouseData[HouseID][HouseLevel];
					SetPlayerVirtualWorld(playerid, 5000 + HouseID);
					printf("SetPlayerVirtualWorld(%d, %d);", playerid, 5000 + HouseID);
					SetPlayerInterior(playerid, AHouseInteriors[hLevel][InteriorID]);
					printf("SetPlayerInterior(%d, %d);", playerid, AHouseInteriors[hLevel][InteriorID]);
					SetPlayerPos(playerid, AHouseInteriors[hLevel][IntX], AHouseInteriors[hLevel][IntY], AHouseInteriors[hLevel][IntZ]);
					printf("SetPlayerPos(%d, %f, %f, %f);", playerid, AHouseInteriors[hLevel][IntX], AHouseInteriors[hLevel][IntY], AHouseInteriors[hLevel][IntZ]);
					APlayerData[playerid][CurrentHouse] = HouseID;
					SendClientMessage(playerid, COLOR_WHITE, "{FFFF00}Use {FFFF00}/housemenu{FFFF00} to change options for your house");
					return 1;
				}
				else
				{
				    printf("Else called");
                                }
Код:
Else called
Else called
Else called
Else called
Else called
Else called
Else called
Else called
Else called
Else called
Else called
Else called
Else called
Else called
Else called
Else called
Else called
Else called
Else called
Else called
Else called
Else called
Else called
Else called
Else called
Else called
Else called
Else called
Else called
Else called
Else called
Else called
Else called
Else called
Else called
Else called
Else called
Else called
Else called
Else called
Else called
Else called
Else called
Else called
Else called
Else called
Else called
Else called
Else called
Else called
Else called
Else called
Else called
Else called
SetPlayerInterior(0, 10);
SetPlayerPos(0, 2260.760009, -1210.449951, 1049.020019);
If the player doesn't actually own a house, it crashes GTA:SA for the player and console is spammed with even more elses.
It still outputs else called but the last part now does this:

Код:
SetPlayerVirtualWorld(0, 5828);
SetPlayerInterior(0, 0);
SetPlayerPos(0, 0.000000, 0.000000, 0.000000);
5828 (5000 + HouseID)
827 is the last house id and the player doesn't own a house? What is going on?
Reply
#2

The first one was about the variable indexing error. I posted a similar thing there but it was irrelevant to the actual topic and some people may have been put off by the title and wouldn't have bothered finding my other problem. I can delete that other post if necessary. Apologies.
Reply
#3

You're looping to check if the playername matches any of the ownernames of the houses in the server.

so it's normal that it shows "else called" when the name of the player is not the same as the ownername of the house you're checking in the loop.

If the names match, it will execute the "if", if they don't it will execute the "else".

the loop shouldn't crash the players though, If I were you I would rewrite all that code.

Also make sure you read important notes for the function "strcmp".

https://sampwiki.blast.hk/wiki/Strcmp
Reply
#4

How would I go about stopping the loop when their house is found? Would adding 'break' after the house has been found work?
Reply
#5

yes
'break' means breaking the loop.
Reply
#6

Quote:
Originally Posted by Exhibit
Посмотреть сообщение
yes
'break' means breaking the loop.
Alright thanks. I'll try it later.
Reply
#7

just return 0;
Reply
#8

Quote:
Originally Posted by MEGADETHS
Посмотреть сообщение
You're looping to check if the playername matches any of the ownernames of the houses in the server.

so it's normal that it shows "else called" when the name of the player is not the same as the ownername of the house you're checking in the loop.

If the names match, it will execute the "if", if they don't it will execute the "else".

the loop shouldn't crash the players though, If I were you I would rewrite all that code.

Also make sure you read important notes for the function "strcmp".

https://sampwiki.blast.hk/wiki/Strcmp
Where should I put the 'break' in the code? When I put it in the if statement that checks if a house has been found it just outputs 'unreachable code'. If I put it before the final bracket in the for statement it just calls else (obviously).

Edit: I tried adding break at the end of the if statement but still doesn't work.
Reply
#9

Quote:
Originally Posted by Proxus
Посмотреть сообщение
Where should I put the 'break' in the code? When I put it in the if statement that checks if a house has been found it just outputs 'unreachable code'. If I put it before the final bracket in the for statement it just calls else (obviously).

Edit: I tried adding break at the end of the if statement but still doesn't work.
remove "return 1;" and put "break;".
Reply
#10

Quote:
Originally Posted by MEGADETHS
Посмотреть сообщение
remove "return 1;" and put "break;".
Didn't work, still spams the else and things.
Reply
#11

Quote:
Originally Posted by Proxus
Посмотреть сообщение
Didn't work, still spams the else and things.
Show us the full code, where you're implementing this one!
Reply
#12

Код:
			for (HouseID = 1; HouseID < MAX_HOUSES; HouseID++)
			{
				if (strcmp(AHouseData[HouseID][Owner], Name, false) == 0)
				{
					hLevel = AHouseData[HouseID][HouseLevel];
					SetPlayerVirtualWorld(playerid, 5000 + HouseID);
					printf("SetPlayerVirtualWorld(%d, %d);", playerid, 5000 + HouseID);
					SetPlayerInterior(playerid, AHouseInteriors[hLevel][InteriorID]);
					printf("SetPlayerInterior(%d, %d);", playerid, AHouseInteriors[hLevel][InteriorID]);
					SetPlayerPos(playerid, AHouseInteriors[hLevel][IntX], AHouseInteriors[hLevel][IntY], AHouseInteriors[hLevel][IntZ]);
					printf("SetPlayerPos(%d, %f, %f, %f);", playerid, AHouseInteriors[hLevel][IntX], AHouseInteriors[hLevel][IntY], AHouseInteriors[hLevel][IntZ]);
					APlayerData[playerid][CurrentHouse] = HouseID;
					SendClientMessage(playerid, COLOR_WHITE, "{FFFF00}Use {FFFF00}/housemenu{FFFF00} to change options for your house");
					break;
				}
				else
				{
					printf("Else called");
                                }
Reply
#13

Quote:
Originally Posted by Proxus
Посмотреть сообщение
Код:
			for (HouseID = 1; HouseID < MAX_HOUSES; HouseID++)
			{
				if (strcmp(AHouseData[HouseID][Owner], Name, false) == 0)
				{
					hLevel = AHouseData[HouseID][HouseLevel];
					SetPlayerVirtualWorld(playerid, 5000 + HouseID);
					printf("SetPlayerVirtualWorld(%d, %d);", playerid, 5000 + HouseID);
					SetPlayerInterior(playerid, AHouseInteriors[hLevel][InteriorID]);
					printf("SetPlayerInterior(%d, %d);", playerid, AHouseInteriors[hLevel][InteriorID]);
					SetPlayerPos(playerid, AHouseInteriors[hLevel][IntX], AHouseInteriors[hLevel][IntY], AHouseInteriors[hLevel][IntZ]);
					printf("SetPlayerPos(%d, %f, %f, %f);", playerid, AHouseInteriors[hLevel][IntX], AHouseInteriors[hLevel][IntY], AHouseInteriors[hLevel][IntZ]);
					APlayerData[playerid][CurrentHouse] = HouseID;
					SendClientMessage(playerid, COLOR_WHITE, "{FFFF00}Use {FFFF00}/housemenu{FFFF00} to change options for your house");
					break;
				}
				else
				{
					printf("Else called");
                                }
Okay, i get it but where you're using this code?
Reply
#14

Quote:
Originally Posted by ConnorW
Посмотреть сообщение
Okay, i get it but where you're using this code?
Under my OnPlayerRequestSpawn function
Reply
#15

pawn Код:
for (HouseID = 1; HouseID < MAX_HOUSES; HouseID++)
You sure this isn't
pawn Код:
for (new HouseID = 1; HouseID < MAX_HOUSES; HouseID++)
?
Also, why are you doing that OnPlayerRequestSpawn? If you're trying to spawn the player at his own house, use OnPlayerSpawn instead.

Printf in a loop, that's why it's spamming else called.
Reply
#16

Quote:
Originally Posted by ConnorW
Посмотреть сообщение
pawn Код:
for (HouseID = 1; HouseID < MAX_HOUSES; HouseID++)
You sure this isn't
pawn Код:
for (new HouseID = 1; HouseID < MAX_HOUSES; HouseID++)
?
Also, why are you doing that OnPlayerRequestSpawn? If you're trying to spawn the player at his own house, use OnPlayerSpawn instead.

Printf in a loop, that's why it's spamming else called.
I'll try it in OnPlayerSpawn and get back to you with the result. I'm pretty sure it isn't for (new HouseID = 1; HouseID < MAX_HOUSES; HouseID++) because I've already defined it.
Reply
#17

Thank you so far- it now spawns the player at the house if they own a house.
However, if they don't have a house, another thing happens.

OnPlayerRequestSpawn:
Код:
			printf("Else called");
			Index = random(sizeof(ASpawnLocationsTrucker)); 
			x = ASpawnLocationsTrucker[Index][SpawnX];
			y = ASpawnLocationsTrucker[Index][SpawnY]; 
			z = ASpawnLocationsTrucker[Index][SpawnZ]; 
			Angle = ASpawnLocationsTrucker[Index][SpawnAngle];
			SetPlayerTruckerClassRank(playerid);
OnPlayerSpawn:
Код:
	new Index, Float:x, Float:y, Float:z, Float:Angle, Name[24];
	GetPlayerName(playerid, Name, sizeof(Name));
	new hLevel;
	for (new HouseID = 1; HouseID < MAX_HOUSES; HouseID++)
	{
		if (strcmp(AHouseData[HouseID][Owner], Name, false) == 0)
		{
			hLevel = AHouseData[HouseID][HouseLevel];
			SetPlayerVirtualWorld(playerid, 5000 + HouseID);
			printf("SetPlayerVirtualWorld(%d, %d);", playerid, 5000 + HouseID);
			SetPlayerInterior(playerid, AHouseInteriors[hLevel][InteriorID]);
			printf("SetPlayerInterior(%d, %d);", playerid, AHouseInteriors[hLevel][InteriorID]);
			SetPlayerPos(playerid, AHouseInteriors[hLevel][IntX], AHouseInteriors[hLevel][IntY], AHouseInteriors[hLevel][IntZ]);
			printf("SetPlayerPos(%d, %f, %f, %f);", playerid, AHouseInteriors[hLevel][IntX], AHouseInteriors[hLevel][IntY], AHouseInteriors[hLevel][IntZ]);
			APlayerData[playerid][CurrentHouse] = HouseID;
			SendClientMessage(playerid, COLOR_WHITE, "{FFFF00}Use {FFFF00}/housemenu{FFFF00} to change options for your house");
			break;
		}
	}
If the player does not own a house, this is what the console outputs:
Код:
Else called
SetPlayerVirtualWorld(0, 5828);
SetPlayerInterior(0, 0);
SetPlayerPos(0, 0.000000, 0.000000, 0.000000);
5828 = HouseID + 5000
5827 is my last houseid

However when a player does own a house (they own house id 55) this is what it outputs:
Код:
Else called
SetPlayerVirtualWorld(0, 5055);
SetPlayerInterior(0, 10);
SetPlayerPos(0, 2260.760009, -1210.449951, 1049.020019);
Which is good, else is called because it tries to call that before a player spawns. When a player spawns, it teleports them inside of their house which works. The only thing which doesn't work is the if the player doesn't have a house and stopping them from teleporting to the middle of Blueberry and falling through the map.
Reply
#18

You can use SetPlayerPos if user doesn't own a house.

pawn Код:
printf("Else called");
            Index = random(sizeof(ASpawnLocationsTrucker));
            x = ASpawnLocationsTrucker[Index][SpawnX];
            y = ASpawnLocationsTrucker[Index][SpawnY];
            z = ASpawnLocationsTrucker[Index][SpawnZ];
            Angle = ASpawnLocationsTrucker[Index][SpawnAngle];
            SetPlayerTruckerClassRank(playerid);
            SetPlayerPos(playerid, x, y, z);
            SetPlayerFacingAngle(playerid, Angle);
Reply
#19

Quote:
Originally Posted by ConnorW
Посмотреть сообщение
You can use SetPlayerPos if user doesn't own a house.

pawn Код:
printf("Else called");
            Index = random(sizeof(ASpawnLocationsTrucker));
            x = ASpawnLocationsTrucker[Index][SpawnX];
            y = ASpawnLocationsTrucker[Index][SpawnY];
            z = ASpawnLocationsTrucker[Index][SpawnZ];
            Angle = ASpawnLocationsTrucker[Index][SpawnAngle];
            SetPlayerTruckerClassRank(playerid);
            SetPlayerPos(playerid, x, y, z);
            SetPlayerFacingAngle(playerid, Angle);
Thanks, I'll try it tomorrow.
Reply
#20

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.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)