Question on loops.
#1

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
Reply
#2

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
Reply
#3

Why not return?

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

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))
Reply
#5

Instead of return false, I can use return SendClientMessage?
Reply
#6

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

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


Forum Jump:


Users browsing this thread: 1 Guest(s)