Loop isn't working?
#1

pawn Код:
CMD:createhouse(playerid, params[])
{
    new string[128], Float:pos[3], number;
    number = 1;
    if(!IsPlayerLoggedIn(playerid))
    {
        SendClientMessage(playerid, -1, "You are restricted from using commands until you log in.");
        return 1;
    }
    if(PlayerInfo[playerid][pAdminLevel] < 1)
    {
        SendClientMessage(playerid, -1, "You do not have the authority to use this command.");
        return 1;
    }
    if(!AdminDuty[playerid])
    {
        SendClientMessage(playerid, -1, "You are not on duty as an Administrator (/aduty).");
        return 1;
    }
    for(new houseid=1; houseid<MAX_HOUSES; houseid++)
    {
        GetPlayerPos(playerid, pos[0], pos[1], pos[2]);
        format(HouseInfo[houseid][hAddress], 32, "%d San Fierro Way", number);
        HouseInfo[houseid][hPrice] = 200000;
        HouseInfo[houseid][hEnterPos][0] = pos[0];
        HouseInfo[houseid][hEnterPos][1] = pos[1];
        HouseInfo[houseid][hEnterPos][2] = pos[2];
        HouseInfo[houseid][hPickup] = CreateDynamicPickup(1273, 1, pos[0], pos[1], pos[2], -1, -1, -1, 30.0);
        format(string, sizeof(string), "House ID: %d\nAddress: %s\nPrice: $%d", houseid, HouseInfo[houseid][hAddress], HouseInfo[houseid][hPrice]);
        HouseInfo[houseid][hText] = CreateDynamic3DTextLabel(string, COLOR_WHITE, pos[0], pos[1], pos[2]+0.3, 15);
        format(string, sizeof(string), "You have created House ID %d.", houseid);
        SendClientMessage(playerid, -1, string);
        houseid = MAX_HOUSES;
    }
    number++;
    return 1;
}


Every time I /createhouse, it keeps the same house ID.
Reply
#2

I think you should remove the 'number++', just try that.

EDIT: remove everything get belongs to definition of 'number' (number = 1, number++, new number
Reply
#3

Yeah I added the number bit for the address, did it after it wasn't working. The house ID should loop and add on.. Don't understand this :/
Reply
#4

Quote:
Originally Posted by AphexCCFC
Посмотреть сообщение
Yeah I added the number bit for the address, did it after it wasn't working. The house ID should loop and add on.. Don't understand this :/
Read my previous post.

Also can you post your house enum? might something wrong there.
Reply
#5

What you are trying to do, works with a global variable increment. As of now, everytime you execute that command, 'number' gets set to 1, and it is incremented AFTER the loop which is useless because you will still reset it the next you execute the command.

pawn Код:
static number = 0;

CMD:createhouse(playerid, params[])
{
    new string[128], Float:pos[3];
   
    if(!IsPlayerLoggedIn(playerid))
    {
        SendClientMessage(playerid, -1, "You are restricted from using commands until you log in.");
        return 1;
    }
    if(PlayerInfo[playerid][pAdminLevel] < 1)
    {
        SendClientMessage(playerid, -1, "You do not have the authority to use this command.");
        return 1;
    }
    if(!AdminDuty[playerid])
    {
        SendClientMessage(playerid, -1, "You are not on duty as an Administrator (/aduty).");
        return 1;
    }
       
    number++;
   
    GetPlayerPos(playerid, pos[0], pos[1], pos[2]);
    format(HouseInfo[number][hAddress], 32, "%d San Fierro Way", number);
    HouseInfo[number][hPrice] = 200000;
    HouseInfo[number][hEnterPos][0] = pos[0];
    HouseInfo[number][hEnterPos][1] = pos[1];
    HouseInfo[number][hEnterPos][2] = pos[2];
    HouseInfo[number][hPickup] = CreateDynamicPickup(1273, 1, pos[0], pos[1], pos[2], -1, -1, -1, 30.0);
    format(string, sizeof(string), "House ID: %d\nAddress: %s\nPrice: $%d", number, HouseInfo[number][hAddress], HouseInfo[number][hPrice]);
    HouseInfo[number][hText] = CreateDynamic3DTextLabel(string, COLOR_WHITE, pos[0], pos[1], pos[2]+0.3, 15);
    format(string, sizeof(string), "You have created House ID %d.", number);
    SendClientMessage(playerid, -1, string);
   
    return 1;
}
Note that this is really not good code, you should add a lot of checks and there are a lot of better ways of doing the same thing, but since you posted for this, this should work.
Reply
#6

pawn Код:
#define MAX_HOUSES          200
pawn Код:
enum hInfo
{
    hHouseID,
    hAddress[32],
    hPrice,
    hPickup,
    Text3D:hText,
    Float:hEnterPos[3],
    Float:hExitPos[3]
}
pawn Код:
new HouseInfo[MAX_HOUSES][hInfo];
pawn Код:
CMD:createhouse(playerid, params[])
{
    new string[128], Float:pos[3];
    if(!IsPlayerLoggedIn(playerid))
    {
        SendClientMessage(playerid, -1, "You are restricted from using commands until you log in.");
        return 1;
    }
    if(PlayerInfo[playerid][pAdminLevel] < 1)
    {
        SendClientMessage(playerid, -1, "You do not have the authority to use this command.");
        return 1;
    }
    if(!AdminDuty[playerid])
    {
        SendClientMessage(playerid, -1, "You are not on duty as an Administrator (/aduty).");
        return 1;
    }
    for(new houseid=1; houseid<MAX_HOUSES; houseid++)
    {
        GetPlayerPos(playerid, pos[0], pos[1], pos[2]);
        format(HouseInfo[houseid][hAddress], 32, "1 San Fierro Way");
        HouseInfo[houseid][hPrice] = 200000;
        HouseInfo[houseid][hEnterPos][0] = pos[0];
        HouseInfo[houseid][hEnterPos][1] = pos[1];
        HouseInfo[houseid][hEnterPos][2] = pos[2];
        HouseInfo[houseid][hPickup] = CreateDynamicPickup(1273, 1, pos[0], pos[1], pos[2], -1, -1, -1, 30.0);
        format(string, sizeof(string), "House ID: %d\nAddress: %s\nPrice: $%d", houseid, HouseInfo[houseid][hAddress], HouseInfo[houseid][hPrice]);
        HouseInfo[houseid][hText] = CreateDynamic3DTextLabel(string, COLOR_WHITE, pos[0], pos[1], pos[2]+0.3, 15);
        format(string, sizeof(string), "You have created House ID %d.", houseid);
        SendClientMessage(playerid, -1, string);
        houseid = MAX_HOUSES;
    }
    return 1;
}
Updated. The whole "number" thing I've removed.
I want the MAX_HOUSES ID to loop and add one on like it's supposed to, but when I /createhouse, the houseid (MAX_HOUSES) stays at 1.
Reply
#7

Quote:
Originally Posted by Rajat_Pawar
Посмотреть сообщение
What you are trying to do, works with a global variable increment. As of now, everytime you execute that command, 'number' gets set to 1, and it is incremented AFTER the loop which is useless because you will still reset it the next you execute the command.

pawn Код:
static number = 0;

CMD:createhouse(playerid, params[])
{
    new string[128], Float:pos[3];
   
    if(!IsPlayerLoggedIn(playerid))
    {
        SendClientMessage(playerid, -1, "You are restricted from using commands until you log in.");
        return 1;
    }
    if(PlayerInfo[playerid][pAdminLevel] < 1)
    {
        SendClientMessage(playerid, -1, "You do not have the authority to use this command.");
        return 1;
    }
    if(!AdminDuty[playerid])
    {
        SendClientMessage(playerid, -1, "You are not on duty as an Administrator (/aduty).");
        return 1;
    }
       
    number++;
   
    GetPlayerPos(playerid, pos[0], pos[1], pos[2]);
    format(HouseInfo[number][hAddress], 32, "%d San Fierro Way", number);
    HouseInfo[number][hPrice] = 200000;
    HouseInfo[number][hEnterPos][0] = pos[0];
    HouseInfo[number][hEnterPos][1] = pos[1];
    HouseInfo[number][hEnterPos][2] = pos[2];
    HouseInfo[number][hPickup] = CreateDynamicPickup(1273, 1, pos[0], pos[1], pos[2], -1, -1, -1, 30.0);
    format(string, sizeof(string), "House ID: %d\nAddress: %s\nPrice: $%d", number, HouseInfo[number][hAddress], HouseInfo[number][hPrice]);
    HouseInfo[number][hText] = CreateDynamic3DTextLabel(string, COLOR_WHITE, pos[0], pos[1], pos[2]+0.3, 15);
    format(string, sizeof(string), "You have created House ID %d.", number);
    SendClientMessage(playerid, -1, string);
   
    return 1;
}
Note that this is really not good code, you should add a lot of checks and there are a lot of better ways of doing the same thing, but since you posted for this, this should work.
How could I make it better? I'll probably use a stock for this then call the stock in the command.

I could do:

pawn Код:
new houseid = 1;
Then do:

pawn Код:
houseid++;
But it won't work with MAX_HOUSES.
Sorry for double post
Reply
#8

I believe you're looking for something like this:

pawn Код:
for(new i = 1; i < MAX_HOUSES; i++)
    {
        if(strlen(HouseInfo[i][hAddress]) <= 1)//MAX HOUSES IS DEFINED, so the string length of the remaining houses = 0, so if they're all taken, house limit is full!
        {
            houseid = i;
            break;
        }
    }
    if(houseid == -1) return SendClientMessage(playerid, COLOUR_GREY, "House Limit Exceeded.");
What you're currently doing is creating the same thing for each house, aha!

Let me know if you need anything else.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)