PDA

View Full Version : How to initialize a field object? and a 2D matrix? (basics)



guiismiti
27th September 2014, 16:40
Hello,

1 - I'm trying to set attributes to a variable.



flag[i].points = 0; //positive - allies are conquering; negative - axis is conquering
flag[i].team = 0; //-1 for axis, 0 for neutral, 1 for allies


If I leave it like that, I'll get the error that flag is an unitialized variable.
If I add flag = []; or flag = 1; I'll get the error that undefined is not a field object or int is not a field object.
Any help here?


2 - Is there any way to use a 2D matrix, like in C?



flag[i][j] = 1;

serthy
27th September 2014, 18:11
1.
Your array fields are structs, so you have to initialize it before you use it:


array = [];
array[array.size] = spawnStruct();
array[array.size - 1].key = value;

You can initialize an array even without =[] by just calling an index, CoD2 does the rest for you:


array[0] = spawnStruct();
array[0].key = value;

2.

You have to create the matrix yourself:



matrix = [];

for( rowID = 0 ; rowID < rowNum ; rowID++ )
{
matrix[rowID] = [];

for( colID = 0 ; colID < colNum ; colID++ )
{
matrix[rowID][colID] = spawnStruct();
matrix[rowID][colID].key = value;

// better way (so CoD2 doesnt access the matrix each call):
struct = spawnStruct();
struct.key = value;
struct.do_stuff_here_with_struct = instead_of_the_matrix_field;
matrix[rowID][colID] = struct;
}
}

IzNoGoD
27th September 2014, 18:32
1.


You can initialize an array even without =[] by just calling an index, CoD2 does the rest for you:


array[0] = spawnStruct();
array[0].key = value;


This will crash the game in developer mode, so i would advise you to just properly initialize the array = [];

serthy
27th September 2014, 18:37
This will crash the game in developer mode, so i would advise you to just properly initialize the array = [];

It does not crash for me on 1.3:


E:\cod2\CoD2MP_s.exe +set dedicated 2 +set fs_game serthy_mods/starwars +set developer 1 +set developer_script 1 +set logfile 2 +exec server.cfg

init()
{
array[0] = 1337;
iPrintLn( "1337 is " + array[0] );
}

Must be changed over to a localized string: "1337 is 1337"

Anyway I'd also go with = [] in every case