SA-MP Forums Archive
Array problem - Wiki says that it must be possible - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+---- Forum: Help Archive (https://sampforum.blast.hk/forumdisplay.php?fid=89)
+---- Thread: Array problem - Wiki says that it must be possible (/showthread.php?tid=104450)



Array problem - Wiki says that it must be possible - Remi-X - 24.10.2009

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.


Re: Array problem - Wiki says that it must be possible - Pawno_Master - 24.10.2009

Maybe use for the first new

#define blabla = blabla
new Blabla[blabla]


Re: Array problem - Wiki says that it must be possible - Remi-X - 24.10.2009

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


Re: Array problem - Wiki says that it must be possible - yom - 24.10.2009

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.


Re: Array problem - Wiki says that it must be possible - Pawno_Master - 24.10.2009

that's what i mean xd


Re: Array problem - Wiki says that it must be possible - Finn - 24.10.2009

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];




Re: Array problem - Wiki says that it must be possible - Westie - 24.10.2009

You can however:

pawn Код:
const
    iVariable = 5;

new
    aArray[iVariable];