• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Converting Javascript array to GML

P

Punch

Guest
Hi,

I'm going to try to convert an existing Javascript game to GM2, so far it looks pretty straight forward to convert but just wanted to ask anyone with experience about how I can use structs to replace a huge literal array I have. At the moment it looks like this:

var array=[
"7720-7721",40,0,{o:"S",audVers:runS,run:1,sloop:["X",1]},
"7721-7821",45,0,{o:"E",audVers:runM,run:2},
];


- there are actually nearly 7000 lines like that. At the moment the only search I do is for the first string, using array.indexOf("7720-7721") for example, then I access the JSON type object, which sometimes also contains its own arrays.

So I'm wondering what's the best way of converting this sort of structure? If I converted it to this:

array={
v: {"7720-7721", 40, 0} ,o: "S", audVers:runS, run:1, sloop:{"X",1}},
}


Would I have any access problems, and could I retain the structure in a better way? Ideally I'd like it to look as similar as possible to the original Javascript purely for legibility. Speed isn't an issue as I only address this once every mouse click or controller move.

Thanks.
 

chamaeleon

Member
You haven't made it very clear just what kind of changes are acceptable, but here's an attempt (adding brackets around the two lines to make it nested arrays).
GML:
var runS = 10;
var runM = 20;
var a=[
    ["7720-7721",40,0,{o:"S",audVers:runS,run:1,sloop:["X",1]}],
    ["7721-7821",45,0,{o:"E",audVers:runM,run:2}]
];

show_debug_message(a);
show_debug_message(a[1][0]);
show_debug_message(a[1][3].audVers);
show_debug_message(a[0][3].sloop[0]);
Code:
[ [ "7720-7721",40,0,{ audVers : 10, run : 1, o : "S", sloop : [ "X",1 ] } ],[ "7721-7821",45,0,{ audVers : 20, run : 2, o : "E" } ] ]
7721-7821
20
X
 
P

Punch

Guest
Ah thank you - so that's basically identical to the Javascript. I couldn't find that in the docs.
 

chamaeleon

Member
GML:
var runS = 10;
var runM = 20;
var a=[
    ["7720-7721",40,0,{o:"S",audVers:runS,run:1,sloop:["X",1]}],
    ["7721-7821",45,0,{o:"E",audVers:runM,run:2}]
];

var b = {};

for (var i = 0; i < array_length(a); i++) {
    var key = a[i][0];
    var value = [];
    array_copy(value, 0, a[i], 1, array_length(a[i])-1);
    b[$ key] = value;
}

show_debug_message(b);
show_debug_message(b[$ "7720-7721"]);
show_debug_message(b[$ "7720-7721"][2].audVers);
show_debug_message(b[$ "7720-7721"][2].sloop[0]);
Code:
{ 7720-7721 : [ 40,0,{ audVers : 10, run : 1, o : "S", sloop : [ "X",1 ] } ], 7721-7821 : [ 45,0,{ audVers : 20, run : 2, o : "E" } ] }
[ 40,0,{ audVers : 10, run : 1, o : "S", sloop : [ "X",1 ] } ]
10
X
Assumes the key is unique, of course..
 
Top