Correct loop to use here?
#1

Say I was going to loop through all the players and make sure the new number I was creating was not the same as another number and I wanted it to create a new number that doesn't match it.

So effectively > loop until (number) != someone elses.

How could I do that?
Reply
#2

Number of what?
Reply
#3

new number[MAX_PLAYERS];
new myNumber = 5;

for(new i = 0; i < MAX_PLAYERS; i++)
{
if (myNumber == number[i])
{
myNumber = number[i] + 1;
}
}

Maybe this will help. But as Vince said.... we need more info to make best answer.
I'm was not "pawning" for a few years, but I'm more interested in java now, so maybe you define variables differently in pawno. I dont remember

Well this should "scan" for all players and their variables that you want to compare your number with, and if their number equeals to your number, it then your number will be incremented by 1. That your number will no longer be same as others number. And if next players number will be same again, it will increment by 1 again and so on until the end where your number will be different than number of any player. If that is how you wanted it to work.

You just have to define what to put in variables. I put number 5 as myNumber. Numbers of others are not defined yet, but it is not neccessary I think. Just to show.
Reply
#4

Make use of the continue keyword.

Код:
foreach(Player, i)
{
if(i==playerid) continue;

// Do code
}
Will loop through all players with the exception of playerid variable.
Reply
#5

PHP код:
for(new 0GetPlayerPoolSize(); <= ji++){


Reply
#6

Always lay a simple foreach loop, when you reach your desired result (or exact opposite of it in some cases) break or return, that way your control structures are simple and you run into infinite loops less (which is a bane to most of the coders)
Here's an example
PHP код:
GenerateUniqueNum()
{
new 
generatednum=random(10);
 foreach(new 
Players)
 {
  if(
Number[i]==generatednum)
  {
   return 
GenerateUniqueNum();
  }
 }
 return 
generatednum;

Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)