SA-MP Forums Archive
Help with 'new' - 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)
+--- Thread: Help with 'new' (/showthread.php?tid=571020)



Help with 'new' - ATGOggy - 15.04.2015

I had this doubt when writing codes for someone in 'Scripting Help'.

The problem is..
Imagine this is my code:
PHP код:
OnPlayerConnect(playerid)
{
    
count++;
    new 
hello[count][128];

Can 'new' be created with its size set to any variable?
If no, is there any other way?


Re: Help with 'new' - BleverCastard - 15.04.2015

It can be, yes. Though, I don't understand fully what you mean by that code.


Re: Help with 'new' - MP2 - 15.04.2015

Quote:
Originally Posted by BleverCastard
Посмотреть сообщение
It can be, yes. Though, I don't understand fully what you mean by that code.
You are wrong. Array sizes MUST be determined before compilation. You cannot use a variable as a size.

The above code would produce the following errors:
Quote:

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

You have to set the array size to the highest possible value that might be used.


Re: Help with 'new' - ATGOggy - 15.04.2015

Okay, thanks for your help.

Also, if anyone knows any other answer, please post it.


Re: Help with 'new' - MP2 - 15.04.2015

There is no other answer. You must set a constant value (known at compile time). The only other option is to use foreach/y_iterate/linked lists with plugins, but only if they're relevant to what you're trying to do.