Help regarding a for loop.
#1

I'd just like to throw it out there that I'm still learning the way loops and structures go. I'm not an expert by any means, so if you could elaborate on what I've done wrong I would appreciate it so I can learn.

The issue is quite simple. When a player does /sellicecream I'd like it to say their not in a vehicle before it shows the "usage [params]" message. I'd also like to know how "break;" and "continue;" work in looping structures.

Here's the current code:
pawn Код:
//Icecream man
CMD:sellicecream(playerid, params[])
{
    if(pInfo[playerid][pJobID] !=3) return SCM(playerid,COLOR_GREY, "[ERROR]: You must be an Icecream Man to sell ice cream.");
    new price, target;
    for(new i=0;i<MAX_VEHICLES;i++)
    {
        if(GetPlayerVehicleID(playerid) == IceCreamJobVehicle[i])
        {
            break;
        }
        if(i==MAX_VEHICLES-1) return SCM(pid,COLOR_GREY, "You need to be in a icecream truck to use this command.");
    }
    if(sscanf(params, "ud", target, price)) return SCM(pid,COLOR_WHITE, "USAGE: /sellicecream [playerid] [price 2-25$]");
    if(price > 25 || price < 2) return SCM(pid,COLOR_GREY, "[ERROR]: Prices out of range. (2-25$)");
    if(target == INVALID_PLAYER_ID) return SCM(pid,COLOR_GREY, "[ERROR]: playerid is not connected to the server.");
    return 1;
   
}
Thanks for any assistance.
Reply
#2

"break" is a keyword which stops/execute the functions in a loop/switch case/if else.
For example:
pawn Код:
for(i=0;i<300;i++)
   {
     if(i==74) break; //here, When i==74 it will stop/execute the loop, and the loop will not proceed further.
     else AddPlayerClass(i, 1958.33, 1343.12, 15.36, 269.15, 26, 36, 28, 150, 0, 0);
    }
"continue" is a keyword which continues to proceed the function in a loop.
For example:
pawn Код:
for(i=0;i<300;i++)
   {
     if(i==74) continue; //here, When i==74 it will continue with the loop without going to the next function ,which is AddPlayerClass (below)
     else AddPlayerClass(i, 1958.33, 1343.12, 15.36, 269.15, 26, 36, 28, 150, 0, 0);
    }
Reply
#3

I found it way more simpler to create a function that verifies if a car is an IceCreamCar since you don't loop trough all vehicles, but just trough all the IceCream Vehicles. Simply, you verify if any of the IceCream car has the same id with the car that the player is in. If you find at least 1 you return 1(true) if not, you return 0. This way you don't use break or continue since "return" stops the execution of the function.

Anyway, if you'd like to read about this you can do it here https://sampwiki.blast.hk/wiki/Control_Structures#break

pawn Код:
//Icecream man
CMD:sellicecream(playerid, params[])
{
    if(pInfo[playerid][pJobID] !=3) return SCM(playerid,COLOR_GREY, "[ERROR]: You must be an Icecream Man to sell ice cream.");
    if(IsPlayerInAnyVehicle(playerid))
    {
          if(!IsAnIceCreamCar(GetPlayerVehicleID)) return SCM(playerid,COLOR_GREY, "[ERROR]: You must be in an Icecream Car to sell ice cream.");
    }
    else return SCM(playerid,COLOR_GREY, "[ERROR]: You must be in an Icecream Car to sell ice cream.");
    if(sscanf(params, "ud", target, price)) return SCM(pid,COLOR_WHITE, "USAGE: /sellicecream [playerid] [price 2-25$]");
    if(price > 25 || price < 2) return SCM(pid,COLOR_GREY, "[ERROR]: Prices out of range. (2-25$)");
    if(target == INVALID_PLAYER_ID) return SCM(pid,COLOR_GREY, "[ERROR]: playerid is not connected to the server.");
    return 1;
}
forward IsAnIceCreamCar(carid);
public IsAnIceCreamCar(carid)
{
for(new k=0; k<MAX_ICECREAMCARS; k++)
        {
            if(carid==IceCreamJobVehicle[k]) return 1;
        }
return 0;
}
Reply
#4

Quote:
Originally Posted by Rittik
Посмотреть сообщение
"break" is a keyword which stops/execute the functions in a loop/switch case/if else.
For example:
pawn Код:
for(i=0;i<300;i++)
   {
     if(i==74) break; //here, When i==74 it will stop/execute the loop, and the loop will not proceed further.
     else AddPlayerClass(i, 1958.33, 1343.12, 15.36, 269.15, 26, 36, 28, 150, 0, 0);
    }
"continue" is a keyword which continues to proceed the function in a loop.
For example:
pawn Код:
for(i=0;i<300;i++)
   {
     if(i==74) continue; //here, When i==74 it will continue with the loop without going to the next function ,which is AddPlayerClass (below)
     else AddPlayerClass(i, 1958.33, 1343.12, 15.36, 269.15, 26, 36, 28, 150, 0, 0);
    }
Do you have any recommendations as to what I should do in this scenario?
Reply
#5

Quote:
Originally Posted by rangerxxll
Посмотреть сообщение
Do you have any recommendations as to what I should do in this scenario?
What you want to do actually ?
Reply
#6

I'd like to send the error message stating "You must be in a icecream truck to use this command" instead of displaying "usage: [params]" before the error message.
Reply
#7

pawn Код:
for(new i=0;i<MAX_VEHICLES;i++)
    {
        if(GetPlayerVehicleID(playerid) != IceCreamJobVehicle[i])
        {
           SCM(pid,COLOR_GREY, "You need to be in a icecream truck to use this command.");
           return 1;
        }
    }
Reply
#8

Testing it now. Will edit my result in a moment. Thank you for your help.

EDIT: Didn't work. It states the error when I'm in the actual vehicle.
Reply
#9

Quote:
Originally Posted by Koala818
Посмотреть сообщение
I found it way more simpler to create a function that verifies if a car is an IceCreamCar since you don't loop trough all vehicles, but just trough all the IceCream Vehicles. Simply, you verify if any of the IceCream car has the same id with the car that the player is in. If you find at least 1 you return 1(true) if not, you return 0. This way you don't use break or continue since "return" stops the execution of the function.

Anyway, if you'd like to read about this you can do it here https://sampwiki.blast.hk/wiki/Control_Structures#break

pawn Код:
//Icecream man
CMD:sellicecream(playerid, params[])
{
    if(pInfo[playerid][pJobID] !=3) return SCM(playerid,COLOR_GREY, "[ERROR]: You must be an Icecream Man to sell ice cream.");
    if(IsPlayerInAnyVehicle(playerid))
    {
          if(!IsAnIceCreamCar(GetPlayerVehicleID)) return SCM(playerid,COLOR_GREY, "[ERROR]: You must be in an Icecream Car to sell ice cream.");
    }
    else return SCM(playerid,COLOR_GREY, "[ERROR]: You must be in an Icecream Car to sell ice cream.");
    if(sscanf(params, "ud", target, price)) return SCM(pid,COLOR_WHITE, "USAGE: /sellicecream [playerid] [price 2-25$]");
    if(price > 25 || price < 2) return SCM(pid,COLOR_GREY, "[ERROR]: Prices out of range. (2-25$)");
    if(target == INVALID_PLAYER_ID) return SCM(pid,COLOR_GREY, "[ERROR]: playerid is not connected to the server.");
    return 1;
}
forward IsAnIceCreamCar(carid);
public IsAnIceCreamCar(carid)
{
for(new k=0; k<MAX_ICECREAMCARS; k++)
        {
            if(carid==IceCreamJobVehicle[k]) return 1;
        }
return 0;
}
Slowly walking away and akwardly leaving cuz my post hasn't been seen.

P.S: That won't work for sure because you verify if the PlayerVehicleId==IceCreamJobVehicle[0]. If not, you stop the execution by returning 1.
Reply
#10

Quote:
Originally Posted by Koala818
Посмотреть сообщение
Slowly walking away and akwardly leaving cuz my post hasn't been seen.

P.S: That won't work for sure because you verify if the PlayerVehicleId==IceCreamJobVehicle[0]. If not, you stop the execution by returning 1.
Sorry bud was sidetracked lol. I'll make sure to skim over your code and try it out if all else leads to no avail.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)