Spawn at House (one line problem)
#1

Hey. I'm trying to make my player spawn at his house but it's not really working. I have debuged it and the problem lays in this line.
pawn Код:
if(HouseInfo[idz][hOwned] == 1 && strcmp(HouseInfo[idz][hOwner], playername) == 0)
I'm trying to look through all the houses and find a owned house and then compare the name of the owner of the house and the player's name.

pawn Код:
if(PlayerInfo[playerid][pPlayerOwnsHouse] == 1)
        {
            new playername[24];
            new string[64];
            GetPlayerName(playerid, playername, sizeof(playername));
            for(new idz = 1; idz < sizeof(HouseInfo); idz++)
            {
                if(HouseInfo[idz][hOwned] == 1 && strcmp(HouseInfo[idz][hOwner], playername) == 0)
                {
                    SetPlayerPos(playerid, HouseInfo[idz][hEntranceX], HouseInfo[idz][hEntranceY], HouseInfo[idz][hEntranceZ]);
                    SendClientMessage(playerid,COLOR_WHITE, string);
                    format(string,sizeof(string), "Welcome back to your home, %s.", GetName(playerid));
                }
            }
        }
Reply
#2

Try this:
pawn Код:
if((HouseInfo[idz][hOwned] == 1) && (strcmp(HouseInfo[idz][hOwner], playername) == 0))
Always group your condition checks to be safe.
With the extra ( and ), you're telling the compiler to check if hOwned == 1, and take a separate check to see if strcmp returns 0.


The compiler may otherwise interpret it like:
pawn Код:
if(HouseInfo[idz][hOwned] == (1 && strcmp(HouseInfo[idz][hOwner], playername)) == 0)
This will of course check every house:
- if it's owned
- if the owner-name is identical

It does both checks even if the house isn't owned.
In such a case, it has no use to check if the owner is identical.

Comparing strings is quite slow.

You can avoid this by putting 2 separate if's below eachother:
pawn Код:
if(HouseInfo[idz][hOwned] == 1)
{
    if (strcmp(HouseInfo[idz][hOwner], playername) == 0)
    {
        SetPlayerPos(playerid, HouseInfo[idz][hEntranceX], HouseInfo[idz][hEntranceY], HouseInfo[idz][hEntranceZ]);
        SendClientMessage(playerid,COLOR_WHITE, string);
        format(string,sizeof(string), "Welcome back to your home, %s.", GetName(playerid));
    }
}
This way, the owner is only checked if the house is owned.
For available houses, this check isn't done and you put less stress on your server, increasing performance.
Reply
#3

As long as it doesn't contain both AND (&&) and OR (||) in the same statement, it's fine.

I believe that hOwned is not 1 so even if the owner's name matches with the player's, it will fail.

Do a better debugging:
pawn Код:
printf("idz: %i & hOwned: %i & hOwner: \"%s\" & playername: \"%s\"", idz, HouseInfo[idz][hOwned], HouseInfo[idz][hOwner], playername);
if(HouseInfo[idz][hOwned] == 1 && strcmp(HouseInfo[idz][hOwner], playername) == 0)
PS: idx stands for index so idz makes no-sense.
Reply
#4

I found the problem, I didn't return it. But.. now I have the problem of finding the right place for my return 1;.

pawn Код:
if(PlayerInfo[playerid][pPlayerOwnsHouse] == 1)
        {
            new playername[24];
            new string[64];
            GetPlayerName(playerid, playername, sizeof(playername));
            for(new idz = 1; idz < sizeof(HouseInfo); idz++)
            {
                if(HouseInfo[idz][hOwned] == 1 && strcmp(HouseInfo[idz][hOwner], playername) == 0)
                {
                    SetPlayerPos(playerid, HouseInfo[idz][hEntranceX], HouseInfo[idz][hEntranceY], HouseInfo[idz][hEntranceZ]);
                    return 1;
                }
                TextDrawHideForPlayer(playerid,Textdraw0);
                TextDrawHideForPlayer(playerid,Textdraw1);
                SendClientMessage(playerid,COLOR_WHITE, string);
                format(string,sizeof(string), "Welcome back to your home, %s.", GetName(playerid));
                LoadPlayerSpawnData(playerid);
            }
        }
This code spawns me at my house but it doesn't LoadPlayerSpawnData, send me that message or hide textdraws.
Reply
#5

You don't need a return, break will break the loop. By the way, the message and LoadPlayerSpawnData must be inside the if statement about owned and the same owner.

pawn Код:
if(PlayerInfo[playerid][pPlayerOwnsHouse] == 1)
{
    new playername[24];
    new string[64];
    GetPlayerName(playerid, playername, sizeof(playername));
    for(new idz = 1; idz < sizeof(HouseInfo); idz++)
    {
        if(HouseInfo[idz][hOwned] == 1 && strcmp(HouseInfo[idz][hOwner], playername) == 0)
        {
            SetPlayerPos(playerid, HouseInfo[idz][hEntranceX], HouseInfo[idz][hEntranceY], HouseInfo[idz][hEntranceZ]);
            TextDrawHideForPlayer(playerid,Textdraw0);
            TextDrawHideForPlayer(playerid,Textdraw1);
            SendClientMessage(playerid,COLOR_WHITE, string);
            format(string,sizeof(string), "Welcome back to your home, %s.", GetName(playerid));
            LoadPlayerSpawnData(playerid);
            break;
        }
    }
}
@PowerPC603: 2 separated if is the same thing. If the house is not owned, it will not check the strcmp match.
Reply
#6

I tried that but now nothing happens. It's weird because when I put that return there I actually spawned at my house.
Reply
#7

pawn Код:
if(PlayerInfo[playerid][pPlayerOwnsHouse] == 1)
{
    new playername[24];
    new string[64];
    GetPlayerName(playerid, playername, sizeof(playername));
    for(new idz = 1; idz < sizeof(HouseInfo); idz++)
    {
        if(HouseInfo[idz][hOwned] == 1 && strcmp(HouseInfo[idz][hOwner], playername) == 0)
        {
            SetPlayerPos(playerid, HouseInfo[idz][hEntranceX], HouseInfo[idz][hEntranceY], HouseInfo[idz][hEntranceZ]);
            TextDrawHideForPlayer(playerid,Textdraw0);
            TextDrawHideForPlayer(playerid,Textdraw1);
            format(string,sizeof(string), "Welcome back to your home, %s.", GetName(playerid));
                    SendClientMessage(playerid,COLOR_WHITE, string);
LoadPlayerSpawnData(playerid);
            return 1;
        }
            break;
    }
Reply
#8

Well. I fixed it with this instead.

pawn Код:
new house = PlayerInfo[playerid][HouseID];
        if(house != 0)
        {
            new string[128];
            InsideHouse[playerid] = house;
            SetPlayerInterior(playerid,HouseInfo[house][hInsideInt]);
            SetPlayerPos(playerid, HouseInfo[house][hExitX], HouseInfo[house][hExitY],HouseInfo[house][hExitZ]);
            SetPlayerVirtualWorld(playerid,house);
            ClearAnimations(playerid);
            format(string,sizeof(string), "Welcome back to your home, %s.", GetName(playerid));
            SendClientMessage(playerid,COLOR_WHITE, string);
        }
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)