Results 1 to 4 of 4

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

  1. #1
    Corporal guiismiti's Avatar
    Join Date
    Dec 2013
    Location
    Brazil
    Posts
    244
    Thanks
    121
    Thanked 42 Times in 31 Posts

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

    Hello,

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

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

    Code:
    flag[i][j] = 1;

  2. #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.

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

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

  4. #3
    Assadministrator IzNoGoD's Avatar
    Join Date
    Aug 2012
    Posts
    1,718
    Thanks
    17
    Thanked 1,068 Times in 674 Posts
    Quote Originally Posted by serthy View Post
    1.


    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;
    This will crash the game in developer mode, so i would advise you to just properly initialize the array = [];
    "Does not work" is an error report for a bug between keyboard and chair.

    All hail Artie Effem

  5. The Following User Says Thank You to IzNoGoD For This Useful Post:

    guiismiti (27th September 2014)

  6. #4
    Sergeant serthy's Avatar
    Join Date
    Nov 2012
    Posts
    450
    Thanks
    96
    Thanked 296 Times in 188 Posts
    Quote Originally Posted by IzNoGoD View Post
    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:
    Code:
    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

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
  •