Dual Loop
#1

Hello!

So for some reason the code i made is not working...
So the code is to check if is player in range of a object... Every player can have 200 objects... And the coordinates are saved as
posX[playerid][object-number], posY...
So now if i want to check all the object positions i need a dual loop(one loop for all the players, other for all the objects)
Код:
    
        for(new pid = 0; pid < MAX_PLAYERS; pid++)
	{
		for(new id=0; id<201; id++)
		{
			if(IsPlayerInRangeOfPoint(playerid,3.0,posX[pid][id],posY[pid][id],posZ[pid][id]))
			{
                              return SendClientMessage(playerid,COLOR_RED,"You are too close.");
			}
                        //something here...
		}
	}
Hope someone understands me

EDIT: This script is for a command so a player can create a object - and he mustn't be close to other objects of other players (and of course his objects)...
Reply
#2

"playerud" Must be "pid"
PHP код:
for(new pid 0,id=0pid MAX_PLAYERS,id<201pid++,id++)
    {
        if(!
IsPlayerInRangeOfPoint(pid,3.0,posX[pid][id],posY[pid][id],posZ[pid][id])) continue;
        
                                  
//something here...
        
    

Reply
#3

Isn't that going to check every player?
I want only if playerid is in range...
This is a command to create a object for player and he mustn't be close to other objects...
Reply
#4

As far as I understand: under OnPlayerUpdate:
pawn Код:
new hhh = -1;
        for(new id=0; id<201; id++)
        {
            if(IsPlayerInRangeOfPoint(playerid,3.0,posX[playerid][id],posY[playerid][id],posZ[playerid][id]))
            {
                                                    hhh = 1;
                              //something here...
            }
                        else if(hhh = -1) return SendClientMessage(playerid,COLOR_RED,"You are too close.");
        }
    }
Reply
#5

No, this is not going to be under OnPlayerUpdate...
It is a command so players can spawn objects and the objects mustn't be close...
Reply
#6

Код:
if(IsPlayerInRangeOfPoint(playerid,3.0,posX[pid][id],posY[pid][id],posZ[pid][id]))
			{
                              //something here...
			}
                        else return SendClientMessage(playerid,COLOR_RED,"You are too close.");
This statement is in reverse. You are checking if the player is within the range of an object and if he is not, you are echoing out 'you are too close'.. for every loop.. that is 201xMAX_PLAYERS.

Your best bet is to create a variable and save if the player is too close
Код:
var tooClose = false;
foreach(MAX_PLAYERS as Player) {
 foreach(PlayerObjects as Object) {
  if(IsPlayerCloseToObject(Player, Object)) {
    tooClose = true;
    break;
  }
 }
}

if(tooClose) {
 return 'You are too close to another object';
}

// Do your real processing code.
Note that the above code is pseudo code. This means that you have to implement it into your own programming language first.
Reply
#7

I fixed it. Thank you.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)