SA-MP Forums Archive
Question on loops. - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Question on loops. (/showthread.php?tid=543466)



Question on loops. - Baltimore - 26.10.2014

Hello.

I have a question about loops:

If I have a code like this:

pawn Код:
for(new i = 0; i < sizeof(Obj); i++)
{
    if(IsPlayerInRangeOfPoint(playerid, 1.0, Obj[i][PosX], Obj[i][PosY], Obj[i][PosZ])
    {
        // In object.
    }  
    else
    {
        // Not in object.
    }
}
To display the error message if the player is not on the object, I do:

A return, a break ...?

Thx


Respuesta: Question on loops. - aoEXE - 26.10.2014

Quote:
Originally Posted by Baltimore
Посмотреть сообщение
Hello.

I have a question about loops:

If I have a code like this:

pawn Код:
for(new i = 0; i < sizeof(Obj); i++)
{
    if(IsPlayerInRangeOfPoint(playerid, 1.0, Obj[i][PosX], Obj[i][PosY], Obj[i][PosZ])
    {
        // In object.
    }  
    else
    {
        // Not in object.
    }
}
To display the error message if the player is not on the object, I do:

A return, a break ...?

Thx
you use break to exit the loop when the player have the object


Re : Question on loops. - Baltimore - 26.10.2014

Why not return?

And if the player is on the object, what i use?


Re: Re : Question on loops. - Dignity - 26.10.2014

Quote:
Originally Posted by Baltimore
Посмотреть сообщение
Why not return?

And if the player is on the object, what i use?
I believe returning false will also break the loop, I'd say returning a positive value (true) would as well but I am not entirely sure. You should use break regardless as it's usage is meant for loops.

Also, for your second question, consider using this method as it simplifies your code (making a boolean function):

pawn Код:
IsPlayerNearObject(playerid)
{
    for(new i = 0; i < sizeof(Obj); i++)
    {
        if(IsPlayerInRangeOfPoint(playerid, 1.0, Obj[i][PosX], Obj[i][PosY], Obj[i][PosZ])
        {
            return true; // Near object.
        }
    }

    return false; // Not near object
}
Usage:

pawn Код:
// Checking if they are in an object:
if(IsPlayerNearObject(playerid))

// Checking if they are not near an object:
if(!IsPlayerNearObject(playerid))



Re : Question on loops. - Baltimore - 26.10.2014

Instead of return false, I can use return SendClientMessage?


Re: Question on loops. - Dignity - 26.10.2014

Yeah. It's just an example but if you modify the actual stock, you won't be able to use it as a boolean.


Re: Re : Question on loops. - Anzipane - 26.10.2014

Quote:
Originally Posted by Baltimore
Посмотреть сообщение
Instead of return false, I can use return SendClientMessage?
Yes, you can.