Results 1 to 4 of 4

Thread: How to initialize a field object? and a 2D matrix? (basics)

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #2
    Sergeant serthy's Avatar
    Join Date
    Nov 2012
    Posts
    450
    Thanks
    96
    Thanked 296 Times in 188 Posts
    1.
    Your array fields are structs, so you have to initialize it before you use it:

    Code:
    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:

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

    You have to create the matrix yourself:

    Code:
    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;
        }
    }
    Last edited by serthy; 27th September 2014 at 18:15.

  2. The Following 2 Users Say Thank You to serthy For This Useful Post:

    guiismiti (27th September 2014),kung foo man (27th September 2014)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •