GameMaker Referencing arrays within arrays

David Chen

Member
Say I had an array in a loop:

for (ii=0;ii<5;ii++){
msg[ii]=array1;
}

where global.msg would be a 1D array like: array1= ["hello", "world","more text!"]
How would you reference specifically array1[0] in the loops?
You can't do msg[ii][0] as this syntax doesn't work in GMS:2 but this is what I'm trying to get at. Is there an alternative?
 

David Chen

Member
Extract the internal array first:
Code:
var internal_array = external_array[0];
var value = internal_array[0];
You can't do this if it's all inside a loop though


Code:
for (i=0;i<5;i++) {
var internal_array = external_array[0];
var value = internal_array[i];
}
[/QUOTE]

that doesn't work^
 
Uhh, yes it does?
Code:
for (var i=0;i<5;i++) {
  var internal_array = external_array[i];
  var value = internal_array[0];
}
If you want to loop through the outer array and grab slot 0 from each array stored in the outer array, the above code is how you would do it. If you wanted to loop through the internal array stored at slot 0 in the outer array, you would do what you posted above this message. I'm not sure why you think a loop changes this? What specifically doesn't work and what specifically are you trying to do?
 
A

Annoyed Grunt

Guest
You can't do this if it's all inside a loop though


Code:
for (i=0;i<5;i++) {
var internal_array = external_array[0];
var value = internal_array[i];
}
that doesn't work^
Why wouldn't it work? The problem here is that you can't chain accessors, so you just have to access the arrays separately.

Code:
msg[ii][0]
Becomes

Code:
var inner_array = msg[ii],
value = inner_array[0];
 

David Chen

Member
i
Why wouldn't it work? The problem here is that you can't chain accessors, so you just have to access the arrays separately.

Code:
msg[ii][0]
Becomes

Code:
var inner_array = msg[ii],
value = inner_array[0];
i'm using:
Code:
///Step
for(i=0;i<t;i++) {
    var internal_array=current_msg[?i];
    show_debug_message(internal_array);
    var msg=internal_array[n[i]];
i'm getting

___________________________________________
############################################################################################
FATAL ERROR in
action number 1
of Step Event0
for object obj_textbox_m:

trying to index a variable which is not an array
at gml_Object_obj_textbox_m_Step_0 (line 4) - var msg=internal_array[n];
############################################################################################
--------------------------------------------------------------------------------------------
stack frame is
gml_Object_obj_textbox_m_Step_0 (line 4)


the debug message is returning an array:
{ { It's an Imbued Heart! }, }

accessing array chains separately doesn't work inside loops??

tl;dr the problem is how to get something like msg[0][1] to work
 
Last edited:

FrostyCat

Redemption Seeker
It's absolutely annoying how you seem to be intentionally misreading everyone's instructions on purpose.

When you do chained accessors like msg[0][1] in languages like Javascript, first it retrieves msg[0], then subindexes [1] off the result of that. Once you know how the expression is evaluated, it's easy to emulate it in a language the doesn't chain the same way. This is the reason why you don't just take syntax for granted, as a programmer you have to know the entire process of how an expression is evaluated.

Now for all of your failed attempts:
You can't do this if it's all inside a loop though


Code:
for (i=0;i<5;i++) {
var internal_array = external_array[0];
var value = internal_array[i];
}
that doesn't work^
This doesn't work because your code is accessing external_array[0][i] instead of external_array[i][0].
i


i'm using:
Code:
///Step
for(i=0;i<t;i++) {
    var internal_array=current_msg[?i];
    show_debug_message(internal_array);
    var msg=internal_array[n[i]];
i'm getting

___________________________________________
############################################################################################
FATAL ERROR in
action number 1
of Step Event0
for object obj_textbox_m:

trying to index a variable which is not an array
at gml_Object_obj_textbox_m_Step_0 (line 4) - var msg=internal_array[n];
############################################################################################
--------------------------------------------------------------------------------------------
stack frame is
gml_Object_obj_textbox_m_Step_0 (line 4)


the debug message is returning an array:
{ { It's an Imbued Heart! }, }

accessing array chains separately doesn't work inside loops??
Your code in this post is complete nonsense and shows no effort in reading our replies. You are treating current_msg like a map instead of an array by using the map accessor, and you are accessing n[i] which has not been declared. If your intention is to access internal_array[0][0], then it would have been this:
Code:
var msg_array = internal_array[0];
var msg = msg_array[0];
tl;dr the problem is how to get something like msg[0][1] to work
Like everyone else has said before:
Code:
var inner_array = msg[0];
value = inner_array[1];
And if you still don't get it, use this workaround.

Also, real chained accessors are slated for the end of the year, so this solution wouldn't be needed for long.
 

chamaeleon

Member
i


i'm using:
Code:
///Step
for(i=0;i<t;i++) {
    var internal_array=current_msg[?i];
    show_debug_message(internal_array);
    var msg=internal_array[n[i]];
i'm getting

___________________________________________
############################################################################################
FATAL ERROR in
action number 1
of Step Event0
for object obj_textbox_m:

trying to index a variable which is not an array
at gml_Object_obj_textbox_m_Step_0 (line 4) - var msg=internal_array[n];
############################################################################################
--------------------------------------------------------------------------------------------
stack frame is
gml_Object_obj_textbox_m_Step_0 (line 4)


the debug message is returning an array:
{ { It's an Imbued Heart! }, }

accessing array chains separately doesn't work inside loops??

tl;dr the problem is how to get something like msg[0][1] to work
Is n an array, given your code snippet
Code:
internal_array[n[i]]
Doesn't seem to make sense given i is already used to get internal_array.
 
Top