SA-MP Forums Archive
Assigning strings to a global variable - 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: Assigning strings to a global variable (/showthread.php?tid=418158)



Assigning strings to a global variable - Da_Noob - 23.02.2013

So, I'm assigning strings to a global variable and I was wondering this:

Why would this work:

pawn Код:
new Msg[][MAX_PLAYERS];
But why do you need to change this:

pawn Код:
new Msg[][MAX_PLAYERS][5];
to this:

pawn Код:
new Msg[MAX_PLAYERS][5][128] // 128 for string length.
I'm trying to understand it but I don't really get it, and I really wanna know.

Thank you in advance.


Re: Assigning strings to a global variable - MP2 - 23.02.2013

If you want a string for every player, you should do this:

new Msg[MAX_PLAYERS][string_length];

AFAIK, Only literal constant strings can have an unspecified size ('[]') i.e.:

new Msg[] = "Hello"; // Will be 6 cells (5 letters + null terminator).


Re: Assigning strings to a global variable - Da_Noob - 23.02.2013

I see, but then if you use:

pawn Код:
new Msg[MAX_PLAYERS][5];
Why can you do:

pawn Код:
Msg[playerid][0] = blabla;
Msg[playerid][1] = blabla;
This really confuses me.

Also:

this worked fine for me

pawn Код:
new Msg[][MAX_PLAYERS];
while this didn't work when I tried it:

pawn Код:
new Msg[MAX_PLAYERS][128]; // 128 for strin length.



Re: Assigning strings to a global variable - Misiur - 23.02.2013

pawn Код:
new Msg[MAX_PLAYERS][5];
Creates 2d array, MAX_PLAYERS wide, 5 cells high. You can do whatever you want within the boundaries.

pawn Код:
new Msg[MAX_PLAYERS][5][128];
Creates 3d object, MAX_PLAYERS wide, 5 cells long, and 128 cells high. So it takes MAX_PLAYERS * 5 * 128 cells (you can store total MAX_PLAYERS * 5 127 char long strings. Also, a awful lot of heap).

The unspecified size is magic for me, and I don't have pawn-lang.pdf here, but I think MP2 is right, and it should be only used for constant strings

#edit:

pawn Код:
new Msg[][MAX_PLAYERS];
If this syntax were correct, then you would create array of undefined number of strings, with max char length of MAX_PLAYERS - 1


Re: Assigning strings to a global variable - Scenario - 23.02.2013

You probably received an error about the array sizes needing to match. MP2 described it as a literal constant string, doing this:

pawn Код:
new msg[] = "NULL";
In that case, the compiler will only assign 5 cells to that variable. Whereas when you do this:

pawn Код:
new msg[MAX_PLAYERS][128] = "NULL";
... the compiler doesn't know how long msg is at the time you do = "NULL".

Make a little sense, at all?