Structs with arrays

jonjons

Member
hello
Is there a way to create a struct with a var that contains an array of values ?

Example:
Code:
My_Bannanas:
{
      Bannana1:
            {
                     Bannana1CoordsA:
                                 [
                                    b1X: 10,
                                    b1Y: 15
                                 ]
                      Bannana1CoordsB:
                                 [
                                    b2X: 20,
                                    b2Y: 25
                                 ]
                      Bannana1CoordsC ..etc...
           }
      Bannana2:
            {
                     Bannana2CoordsA:
                                 [
                                    b1X: 30,
                                    b1Y: 35
                                 ]
                      Bannana2CoordsB:
                                 [
                                    b2X: 40,
                                    b2Y: 45
                                 ]
                      Bannana2C ..etc...
           }
}
I know how this is done in JSON, i just cant do the oposite make this in GM and pharse it on to JSON... i dont understand the gm logic, the examples in the blog were all rushed, and dont have any pratical use, except for doing things, that were already ways to do it.

var _xx = 100;
mystruct = {
a : 10,
b : "Hello World",
c : int64(5),
d : _xx + 50,
e : function( a, b )
{
return a + b;
},
f : [ 10, 20, 30, 40, 50 ]
};
 
Array elements don't have names. They should just be values in sequence. Your GM example shows the correct way to do it.
GML:
// Works
testStruct = {
    num: 5,
    array: [1, 2, 3]
}
 

chamaeleon

Member
hello
Is there a way to create a struct with a var that contains an array of values ?

Example:
Code:
...
Perhaps clarify with a valid example.. In the meantime.
GML:
function Foo(a1, a2, b1, b2) constructor {
    FooCoordsA = [a1, a2];
    FooCoordsB = [b1, b2];
}

function Bar(foo1, foo2) constructor {
    Foo1 = foo1;
    Foo2 = foo2;
}
GML:
var bar = new Bar(new Foo(1, 2, 3, 4), new Foo(5, 6, 7, 8));
show_debug_message(json_stringify(bar));
Prettified output
Code:
{
  "Foo2": {
    "FooCoordsB": [
      7,
      8
    ],
    "FooCoordsA": [
      5,
      6
    ]
  },
  "Foo1": {
    "FooCoordsB": [
      3,
      4
    ],
    "FooCoordsA": [
      1,
      2
    ]
  }
}
 

jonjons

Member
thanks
yes i used names in the array
this way should work

GML:
ban1_x = 10;
ban1_y = 11;
ban1_Ang = 12;

ban2_x = 20;
ban2_y = 21;
ban2_Ang = 22;


MY_Bannanas =
{
    Bannana1:
    {
        img_index: 1,
        Bannanas1_Coords :
        [
            ban1_x,
            ban1_y,
            ban1_Ang     
        ]
    }
    Bannana2:
    {
        img_index: 1,
        Bannanas2_Coords :
        [
            ban2_x,
            ban2_y,
            ban2_Ang
        ]
    }
}   

var a1 = MY_Bannanas.Bannana1.Bannanas1_Coords [0];
var a2 = MY_Bannanas.Bannana1.Bannanas1_Coords [1];
var a3 = MY_Bannanas.Bannana1.Bannanas1_Coords [2];

var b1 = MY_Bannanas.Bannana2.Bannanas2_Coords [0];
var b2 = MY_Bannanas.Bannana2.Bannanas2_Coords [1];
var b3 = MY_Bannanas.Bannana2.Bannanas2_Coords [2];

show_message(string(   a1) + "\n" + string(a2) + "\n" + string(a3) + "\n" + string(b1) + "\n" + string(b2) + "\n" + string(b3) );
 
Top