Wird Prob
#1

pawn Код:
#define MAX_HOUSES 200

CMD:buy(playerid, params[])
{
    for(new i = 0; i < MAX_HOUSES; i++)
    {
        printf("MAX_HOUSES: %d", i);
        //rest of command
    }
    return 1;
}
the porblem is that it prints:

Код:
MAX_HOUSES: 0
but it is supposed to loop throuh all houses.. like this
Код:
MAX_HOUSES: 0
MAX_HOUSES: 1
MAX_HOUSES: 2
MAX_HOUSES: 3
... 4..197
MAX_HOUSES: 198
MAX_HOUSES: 199
Reply
#2

At the time, there is nothing that would stop the code from breaking the loop. Show the rest of the code associated with the command. You must be using a break or return inside the loop. Otherwise, some part of your code is causing the server to stop (most likely an array out-of-bounds).
Reply
#3

Here is rest of the code
pawn Код:
COMMAND:buy(playerid, params[])
{
    for(new i = 0; i < MAX_HOUSES; i++)
    {
        printf("MAX_HOUSES: %d", i);
        if(!IsPlayerInRangeOfPoint(playerid, 3, HouseInfo[i][PickupX], HouseInfo[i][PickupY], HouseInfo[i][PickupZ]))
        return SendClientMessage(playerid, 0xF60000AA, "You are not close enough to a house");

        if(strcmp(dini_Get("Owned.ini", HouseInfo[i][HouseNames]), PlayerName(playerid)))
        return SendClientMessage(playerid, 0xF60000AA, "You already have a house!");
       
        if(GetPlayerMoney(playerid) < HouseInfo[i][HouseCost])
        return SendClientMessage(playerid, 0xF60000AA, "You don't have enough money to buy this house");

        if(HouseInfo[i][Owned] == 1)
        return SendClientMessage(playerid, 0xF60000AA, "This house is already owned!");
       
        GivePlayerMoney(playerid, - HouseInfo[i][HouseCost]);
        GameTextForPlayer(playerid, "~r~House Purchased!", 2000, 3);
        HouseInfo[i][Owned] = 1;
        format(HouseInfo[i][HouseOwner], 24, PlayerName(playerid));
    }
    return 1;
}
Reply
#4

There's your problem. You're using returns in there, when a return is called the function will stop executing. So you need to re-structure your code more logically, for example:

pawn Код:
COMMAND:buy(playerid, params[])
{
    for(new i = 0; i < MAX_HOUSES; i++)
    {
        printf("MAX_HOUSES: %d", i);
        if(IsPlayerInRangeOfPoint(playerid, 3, HouseInfo[i][PickupX], HouseInfo[i][PickupY], HouseInfo[i][PickupZ]))
        {
            if(strcmp(dini_Get("Owned.ini", HouseInfo[i][HouseNames]), PlayerName(playerid)))
                return SendClientMessage(playerid, 0xF60000AA, "You already have a house!");
       
            if(GetPlayerMoney(playerid) < HouseInfo[i][HouseCost])
                return SendClientMessage(playerid, 0xF60000AA, "You don't have enough money to buy this house");

            if(HouseInfo[i][Owned] == 1)
                return SendClientMessage(playerid, 0xF60000AA, "This house is already owned!");
       
            GivePlayerMoney(playerid, - HouseInfo[i][HouseCost]);
            GameTextForPlayer(playerid, "~r~House Purchased!", 2000, 3);
            HouseInfo[i][Owned] = 1;
            format(HouseInfo[i][HouseOwner], 24, PlayerName(playerid));
            return 1;
        }
    }
    SendClientMessage(playerid, 0xF60000AA, "You are not close enough to a house");
    return 1;
}
Originally it would've only worked for the first house provided you were close enough to it, but when you weren't close enough to it, it returns the SendClientMessage return value and then the execution stopped.

This way it will go through all houses until one is actually found or if one is not found, send a message to the player informing him that the he is not near a house.
Reply
#5

Thank you, Thanks for explaining
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)