My /fishing command isn't working with an array.
#1

So I have a array of coordinates, simple as that. I'd like to detect if a player is in range of any of the coordinates of the array. But for some reason it's not working. It duplicates the action 5+ times, as if it's looping through players and not the positions.

pawn Код:
CMD:fish(playerid, params[])
{
    if(GameProgress == 0) return SCM(playerid, COLOR_GREY, "Error: You can't fish when a game is not in progress.");
    if(IsPlayerReady[playerid] == 0) return SCM(playerid, COLOR_GREY, "Error: You're not in a game.");
    if(fish[playerid] == MAX_INVENTORY[playerid]) return SCM(playerid,COLOR_GREY, "Error: You can't hold any more fish.");
    if(currentlyfishing[playerid] == 1) return SCM(playerid,COLOR_GREY, "Error: You're already fishing.");
    for(new i = 0; i < sizeof(fishingspots); i++)
    {
        if(IsPlayerInRangeOfPoint(playerid, 3.0, fishingspots[i][0], fishingspots[i][1], fishingspots[i][2]))
        {
            currentlyfishing[playerid] = 1;
            SetTimerEx("FishTimer", 5000, false, "i", playerid);
            TogglePlayerControllable(playerid, 0);
            ApplyAnimation(playerid,"BOMBER","BOM_Plant_Loop",4,1,0,0, 0,0,1);
        }
    }
    return 1;
}
Reply
#2

When they actually fish, you need to break the loop.

pawn Код:
CMD:fish(playerid, params[])
{
    if(GameProgress == 0) return SCM(playerid, COLOR_GREY, "Error: You can't fish when a game is not in progress.");
    if(IsPlayerReady[playerid] == 0) return SCM(playerid, COLOR_GREY, "Error: You're not in a game.");
    if(fish[playerid] == MAX_INVENTORY[playerid]) return SCM(playerid,COLOR_GREY, "Error: You can't hold any more fish.");
    if(currentlyfishing[playerid] == 1) return SCM(playerid,COLOR_GREY, "Error: You're already fishing.");
    for(new i = 0; i < sizeof(fishingspots); i++)
    {
        if(IsPlayerInRangeOfPoint(playerid, 3.0, fishingspots[i][0], fishingspots[i][1], fishingspots[i][2]))
        {
            currentlyfishing[playerid] = 1;
            SetTimerEx("FishTimer", 5000, false, "i", playerid);
            TogglePlayerControllable(playerid, 0);
            ApplyAnimation(playerid,"BOMBER","BOM_Plant_Loop",4,1,0,0, 0,0,1);
            break; //The player IS in range, so stop the loop.
        }
    }
    return 1;
}
Reply
#3

I never even knew "break" existed. So it breaks a loop? What does it do exactly? I was assuming the loop stopped by itself.
Reply
#4

You can read more on break and other control structures here.
Reply
#5

Hm, alright. I've tried implementing a message saying when they're not near a fishing spot. It works, but when they ARE in range of a spot then the message still occurs and no actions happen. What could be the issue?

pawn Код:
for(new i = 0; i < sizeof(fishingspots); i++)
    {
        if(!IsPlayerInRangeOfPoint(playerid, 3.0, fishingspots[i][0], fishingspots[i][1], fishingspots[i][2])) return SCM(playerid, COLOR_GREY, "Error: You're not near a fishing location. Search near water.");
        {
            currentlyfishing[playerid] = 1;
            SetTimerEx("FishTimer", 5000, false, "i", playerid);
            TogglePlayerControllable(playerid, 0);
            ApplyAnimation(playerid,"BOMBER","BOM_Plant_Loop",4,1,0,0, 0,0,1);
            break;
        }
    }
Reply
#6

What you were doing wrong is checking if the player was not in range of the FIRST fishing location only.

Because you returned the SendClientMessage if the player wasn't in range of the first point, the command basically ended.

This is the way you need to do it.

"continue;" will stop all the code inside the loop after it is called and go back to the start of the loop with the next check.

Sorry if this isn't easy to understand but its 6:30am for me xD


pawn Код:
CMD:fish(playerid, params[])
{
    if(GameProgress == 0) return SCM(playerid, COLOR_GREY, "Error: You can't fish when a game is not in progress.");
    if(IsPlayerReady[playerid] == 0) return SCM(playerid, COLOR_GREY, "Error: You're not in a game.");
    if(fish[playerid] == MAX_INVENTORY[playerid]) return SCM(playerid,COLOR_GREY, "Error: You can't hold any more fish.");
    if(currentlyfishing[playerid] == 1) return SCM(playerid,COLOR_GREY, "Error: You're already fishing.");
    for(new i = 0; i < sizeof(fishingspots); i++)
    {
        if(!IsPlayerInRangeOfPoint(playerid, 3.0, fishingspots[i][0], fishingspots[i][1], fishingspots[i][2])) continue; //skip the code after this and go to the next iteration of the loop. If the loop is on the last check and the player is still not in range, it will continue running code AFTER the loop.
       
        currentlyfishing[playerid] = 1;
        SetTimerEx("FishTimer", 5000, false, "i", playerid);
        TogglePlayerControllable(playerid, 0);
        ApplyAnimation(playerid,"BOMBER","BOM_Plant_Loop",4,1,0,0, 0,0,1);
        return 1; //if the person is in range, the command will finish here and therefore the message "Error: You're not near a fishing location. Search near water." will not show.
    }
    //When the player is not in range of any of the fishing spots, the code will continue here.
    SCM(playerid, COLOR_GREY, "Error: You're not near a fishing location. Search near water.");
    return 1;
}
Reply
#7

Thanks for the help. I'm starting to get it. Looks like I've digged myself a bit deeper, beginning to learn complex loops.

Appreciated. I'll still continue to learn structures and the way loops work.
Reply
#8

No problem

It's a bit confusing at first but you will learn it in no time
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)