Array problem - Wiki says that it must be possible
#1

Hi, i'm busy with a new script, but a tiny bug halted me. The wiki says the following:
Quote:

new
myVariable = 5,
myArray[myVariable];

@ https://sampwiki.blast.hk/wiki/Scripting_Basics >> Arrays

But, when I use the following, which is slightly the same:
Код:
public SomePublic()
{
	new bananaAmount = GetMaxPlayers(); //Which gets a number
	new bananaSlot[bananaAmount]; // Error rule
	return 1;
}
I get three errors, all on the same rule, from the same thing, and makes a fourth fatal error:
Quote:

1. error 008: must be a constant expression; assumed zero
2. error 009: invalid array size (negative, zero or out of bounds)
3. error 036: empty statement
4. fatal error 107: too many error messages on one line

What is the problem? (Yeah, I know I can use MAX_PLAYERS also in stead of GetMaxPlayers, but this is an example script. The real script really needs away like this.)

I'm using the 0.3a server.

EDIT: Even the exact same as the wiki says doesn't work.
Reply
#2

Maybe use for the first new

#define blabla = blabla
new Blabla[blabla]
Reply
#3

Defines can't have functions. Like:
#define MAX_PLAYERS_2 GetMaxPlayers();
Crashed my compiler the last time I did that.
Reply
#4

No:
pawn Код:
#define MAX_PLAYERS_2 GetMaxPlayers()

printf("%d", MAX_PLAYERS_2); //this is valid.

new array[MAX_PLAYERS_2]; //this is not valid because the array MUST be initiallized with a constant value.
5 is a constant, GetMaxPlayers() isn't. So what you want to do is not possible.

RTFM for more informations.
Reply
#5

that's what i mean xd
Reply
#6

Can you read?

From SA-MP Wiki:
Quote:

That code will declare an array 5 slots big, so you can store 5 pieces of normal data at once in that single what you can't do is something like the following:

new
myVariable = 5,
myArray[myVariable];

Reply
#7

You can however:

pawn Код:
const
    iVariable = 5;

new
    aArray[iVariable];
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)