Problem with loop
#1

I have problem, with ATM loop. It should check, is PlayerIn near of any ATMs, but it always says, im not. I thought i should test break, because i read, that return stops loop, but i cant figure out, how to fix it.

pawn Код:
CMD:atm(playerid, params[])
{
            for(new i = 1; i < MAX_ATMS; i++)
            {

           
           
            if(!IsPlayerInRangeOfPoint(playerid, 3.0,aInfo[i][atmX],aInfo[i][atmY],aInfo[i][atmZ]))
            {
            SendClientMessage(playerid, COLOR_RED, "Sa pole ATMi lдhedal!");
            break;
            }
            else
            {

            if(PlayerInfo[playerid][pAtmpass] == 0)
            {
                ShowPlayerDialog(playerid, 5518, DIALOG_STYLE_PASSWORD, "Bank Of America", "Vali endale sobiv parool, et endale pangakonto luua", "Loo", "Sulge");
            }
            else
            {
                ShowPlayerDialog(playerid,4329, DIALOG_STYLE_PASSWORD, "Bank Of America", "Sisesta oma salasхna, et sisse logida", ">>", "Katkesta");
            }
            }
}
    return 1;
}
Reply
#2

Your code does: if the player is not near the atm with ID 1, it will stop the loop.

pawn Код:
CMD:atm(playerid, params[])
{
    new bool: atm_found;
    for (new i = 1; i < MAX_ATMS; i++)
    {
        if (IsPlayerInRangeOfPoint(playerid, 3.0,aInfo[i][atmX],aInfo[i][atmY],aInfo[i][atmZ]))
        {
            if (!PlayerInfo[playerid][pAtmpass] ) ShowPlayerDialog(playerid, 5518, DIALOG_STYLE_PASSWORD, "Bank Of America", "Vali endale sobiv parool, et endale pangakonto luua", "Loo", "Sulge");
            else ShowPlayerDialog(playerid,4329, DIALOG_STYLE_PASSWORD, "Bank Of America", "Sisesta oma salasхna, et sisse logida", ">>", "Katkesta");
            atm_found = true;
            break;
        }
    }
    if (!atm_found) SendClientMessage(playerid, COLOR_RED, "Sa pole ATMi lдhedal!");
    return 1;
}
Stop the loop when it found. By the way, why does the ID of the ATM start with 1 and not with 0?
Reply
#3

Thanks man! I know a lot of more now about loops.
Reply
#4

//E: When im not near the ATM it doesnt send me a message, im not near of it, but it send "SERVER:unknown command"
Reply
#5

It sets atm_found to true if you're in range with any atm, else it is false by default. At the end we check, if atm_found is 0 (not in range of any atm) and send the message.

The code is fine so I guess it's caused by some runtime error (maybe index out of bounds), that depends on the MAX_ATMS and the size of aInfo.

Just to be sure, load crashdetect plugin: https://github.com/Zeex/samp-plugin-...ases/tag/v4.12
Re-compile your scripts with debug info: https://github.com/Zeex/samp-plugin-...ith-debug-info

Start the server, join and test it. In case it prints anything, post it here.
Reply
#6

You could always do what i did with my dynamic ATM system.

Nothing against you Konstantinos, Your way works just as good. I just don't like using bools in that way waste of time honestly.

pawn Код:
CMD:atm(playerid, params[])
{
    // Remove the bool
    for (new i = 1; i < MAX_ATMS; i++)
    {
        if (IsPlayerInRangeOfPoint(playerid, 3.0,aInfo[i][atmX],aInfo[i][atmY],aInfo[i][atmZ]))
        {
            if (!PlayerInfo[playerid][pAtmpass] ) ShowPlayerDialog(playerid, 5518, DIALOG_STYLE_PASSWORD, "Bank Of America", "Vali endale sobiv parool, et endale pangakonto luua", "Loo", "Sulge");
            else ShowPlayerDialog(playerid,4329, DIALOG_STYLE_PASSWORD, "Bank Of America", "Sisesta oma salasхna, et sisse logida", ">>", "Katkesta");
            // Removed atm_found = true;
            break;
        } else {
            SendClientMessage(playerid, COLOR_RED, "Sa pole ATMi lдhedal!");
            break;
        }

    }
    // Remove the if statement to check if it was found.
    return 1;
}
Reply
#7

Quote:
Originally Posted by zT KiNgKoNg
Посмотреть сообщение
You could always do what i did with my dynamic ATM system.

Nothing against you Konstantinos, Your way works just as good. I just don't like using bools in that way waste of time honestly.

pawn Код:
CMD:atm(playerid, params[])
{
    // Remove the bool
    for (new i = 1; i < MAX_ATMS; i++)
    {
        if (IsPlayerInRangeOfPoint(playerid, 3.0,aInfo[i][atmX],aInfo[i][atmY],aInfo[i][atmZ]))
        {
            if (!PlayerInfo[playerid][pAtmpass] ) ShowPlayerDialog(playerid, 5518, DIALOG_STYLE_PASSWORD, "Bank Of America", "Vali endale sobiv parool, et endale pangakonto luua", "Loo", "Sulge");
            else ShowPlayerDialog(playerid,4329, DIALOG_STYLE_PASSWORD, "Bank Of America", "Sisesta oma salasхna, et sisse logida", ">>", "Katkesta");
            // Removed atm_found = true;
            break;
        } else {
            SendClientMessage(playerid, COLOR_RED, "Sa pole ATMi lдhedal!");
            break;
        }

    }
    // Remove the if statement to check if it was found.
    return 1;
}
The point is that if you got more than 1 ATM, this will simply fail.

Let's say you got 5 ATMs, your code checks if you're in range of the ATM 1. If you're not, then it will send the message without even checking if you're indeed in range with the rest of the ATMs. The boolean variable is the best option.

Quote:
Originally Posted by AA9
Посмотреть сообщение
Okay, i figured it out. MAX_ATMS was defined as 50, but aInfo new aInfo[11][atmPos]. Thanks!
Glad you fixed it!
Reply
#8

Okay, i figured it out. MAX_ATMS was defined as 50, but aInfo new aInfo[11][atmPos]. Thanks!
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)