D
Drenathor
Guest
Hey guys,
If someone has a resource they can point me towards that will help me understand the way GML handles arrays that would be amazing! Unfortunately right now the more I try to use them the more confused I get.
Here's some code that I'm running as tests along with the output and my notes, questions and comments:
Anyways, as you can see different methods of creating arrays and setting values result in different data-set structures and different results / errors when attempting to retrieve them.
Right now my assumptions are that you can't use variable[0,3] structures to retrieve data unless you used that format to store data or you end up with errors. Furthermore if you store data this way you can't pull out the 1d "parent" arrays or you get nonsense data and or errors.
However, if you use the variable = [[0,2],[4,6]] method of storing data you have to retrieve it in multiple steps or you end up with errors using something like variable[1,1].
And I still don't understand all the "extra" brackets in that code output
Anyways, thanks for taking the time to look through this mess!
-John
If someone has a resource they can point me towards that will help me understand the way GML handles arrays that would be amazing! Unfortunately right now the more I try to use them the more confused I get.
Here's some code that I'm running as tests along with the output and my notes, questions and comments:
Code:
// test 1
test1 = [[0,1,2,3],[4,5,6,7]];
//show_debug_message( test1 ); // {{{{0,1,2,3},},{{4,5,6,7},}},} // what's with all the extra brackets?!
//show_debug_message( test1[0] ); // {{0,1,2,3},}
//show_debug_message( test1[1,0] ); // !error // why is this an error?
tmp = test1[1];
//show_debug_message( tmp ); // {{4,5,6,7},}
//show_debug_message( tmp[0] ); // 4 // why does this work but calling it directly doesn't?
// test 2
test2[0,0] = 0;
test2[0,1] = 1;
test2[0,2] = 2;
test2[0,3] = 3;
test2[1,0] = 4;
test2[1,1] = 5;
test2[1,2] = 6;
test2[1,3] = 7;
//show_debug_message( test2 ); // {{0,1,2,3},{4,5,6,7},} // Why didn't test1 format this way?
//show_debug_message( test2[0] ); // 0 // Where is all the data?
//show_debug_message( test2[1,0] ); // 4 // Not sure how it's getting the data if test2[1] doesn't...
tmp = test2[1];
//show_debug_message( tmp ); // 1 // where is all my data?
//show_message( tmp[0] ); // error // why is this an error?
// test 3
tmp = [0,1,2,3];
test3[0] = tmp;
tmp = [4,5,6,7];
test3[1] = tmp;
//show_debug_message( test3 ); // {{{{0,1,2,3},},{{4,5,6,7},}},} // wish it would format this similar to test2
//show_debug_message( test3[0] ); // {{0,1,2,3},}
//show_debug_message( test3[1,0] ); // error // again, don't understand the error
//tmp = test3[1]; show_debug_message( tmp[0] ); // 4 // when doing it this way works
// test 4
tmp[0] = 0;
tmp[1] = 1;
tmp[2] = 2;
tmp[3] = 3;
test4[0] = tmp;
tmp[0] = 4;
tmp[1] = 5;
tmp[2] = 6;
tmp[3] = 7;
test4[1] = tmp;
//show_debug_message( test4 ); // {{{{4,5,6,7},},{{4,5,6,7},}},} // Apparently doing it this way as opposed to test3's method uses pointers?! Why?
//show_debug_message( test4[0] ); // {{ 4,5,6,7 },}
//show_debug_message( test4[1,0] ); // error
// test 5
/**
* I've created a script to essentially do the same thing as above as a workaround to the pointer issue...
* my_script contains the following code:
*
* ---------------------------------
* var t = [];
* var j = argument0 * 4;
*
* t[0] = 0 + j;
* t[1] = 1 + j;
* t[2] = 2 + j;
* t[3] = 3 + j;
*
* test5[argument0] = t;
* ----------------------------------
*
* As you can see it simply does the same thing as test 4 but populates via a script instead of inline.
* A horribly convoluted solution if I do say so myself ;)
*/
test5 = [];
my_script(0);
my_script(1);
//show_debug_message( test5 ); // {{{{0,1,2,3},},{{4,5,6,7},}},} // surely there's an easier way to do this right?
Right now my assumptions are that you can't use variable[0,3] structures to retrieve data unless you used that format to store data or you end up with errors. Furthermore if you store data this way you can't pull out the 1d "parent" arrays or you get nonsense data and or errors.
However, if you use the variable = [[0,2],[4,6]] method of storing data you have to retrieve it in multiple steps or you end up with errors using something like variable[1,1].
And I still don't understand all the "extra" brackets in that code output
Anyways, thanks for taking the time to look through this mess!
-John