Help with arrays
#1

I need to return the first empty slot in array, So i wrote this function:

pawn Code:
public GetFirstEmpty(array[])
{
    new size = sizeof(array[]);
    for(new a = 0; a < size; a++)
    {
      if(array[a] == 0)
      {
        return a;
      }
    }
    return -1;
}
Problem is, When the array is empty it returns 0 (Which is correct)
But when i put something in slot 0, It returns -1.
Halp?

I think its the if(array[a] == 0)
But what should i replace the 0 with?
Or does anyone else have a working function?
Reply
#2

I really need this.
Reply
#3

youll need to set it as 0 somewhere before it recognises it as a 0

for example (i know this isnt using an array but its the same method)

Code:
  totalon = 0;
  for(new i=0; i<MAX_PLAYERS; i++) {
    if(IsPlayerConnected(i)) {
      if(totalon == 0){
        totalon = 1;
      }
      else if (totalon > 0){
        totalon++;
      }
    }
  }
firstly i set it to 0 so that it can start from 0

so lets say that you need the array to be 0 at the game mode start

so run a loop that sets array[a] = 0 under OnGameModeInit()
Reply
#4

Ah thanks.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)