• 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!

Using Array as ds_map key

Sammi3

Member
Hello, I'm having an issue when using an array as a ds_map key. When I set an array as a key and immediately try to get the value, it works. But when I use ds_map_find_* functions, while the array is returned, it is not usable as a key. For example:

Code:
previousNodes[?state] = action;
show_debug_message(previousNodes[?state]);
Where state is an array. This works and returns the variable stored in action.

While:
Code:
key = ds_map_find_first(previousNodes);
show_debug_message(previousNodes[?key]);
returns undefined.

Is there any way around this because I need the variable state to be an array and the map needs the array as a reference to the variable stored.
 
H

Homunculus

Guest
Why would you even want to do this? Anyways, you should probably use a ds_list for that, and use the index as key.
 

Sammi3

Member
Why would you even want to do this? Anyways, you should probably use a ds_list for that, and use the index as key.
Using a GOAP for AI where an array represents either the current or a theoretical state of the world. They used the same method in F.E.A.R. But yeah, was trying to avoid lists to avoid memory leaks but it's looking more likely.
 
H

Homunculus

Guest
Yeah forgetting about data structures is a pain. I have no idea why the GMS2 debugger (which is great) has no way to track data structures. That's something I wanted since basically forever, and was definitely expecting to see this in GMS2...
 

Sammi3

Member
I think what I'm going to do is convert the array into a string then hold it as a ds_map key because trying to do this with a ds_list brings it's own set of problems. Any advice in doing this? I could parse through the array and just copy whatever is there as a string. As long as I have a reference to what data is where and the data type, it shouldn't be too hard.

EDIT:
I'll try use string(array) first and see if that works.
 
Last edited:

Tsa05

Member
I have no idea why the GMS2 debugger (which is great) has no way to track data structures. That's something I wanted since basically forever, and was definitely expecting to see this in GMS2
Ummmm.... Good news?
It's in GMS 1.4. And GMS 2.

In GameMaker, data structures are "remembered" by index value, which is just a number. But, right-click any data structure name in the debugger, and you'll see options to display it as a list, map, etc, depending on what types are available.
 

Sammi3

Member
So using the string of an array as the key works which is perfect. Now I have two options:

1. Use string(array) but then when I want to parse the string back into an array, I'm going to have to remove the brackets and spaces in order to just recognize the commas as delimiters
2. Create my own array_to_string function so that I can circumvent having to remove the brackets and spaces.

I would only use option 1 if there is a way to use string_replace/string_replace_all in order to remove the brackets and spaces but it seems that you have to replace whatever string with a substring which at best can be a space. Is there away around this? So that string_replace also deletes the character and removes the excess space?

EDIT:
Excuse my idiocy, forgot somehow that I could use "" as a substring instead of " ". Silly mistake.
 
Last edited:

Dmi7ry

Member
So using the string of an array as the key works which is perfect. Now I have two options:
1. Use string(array) but then when I want to parse the string back into an array, I'm going to have to remove the brackets and spaces in order to just recognize the commas as delimiters
2. Create my own array_to_string function so that I can circumvent having to remove the brackets and spaces.
I think you're going wrong way. Why do you think that you should use the array's content as ds_map key?
 

Sammi3

Member
I think you're going wrong way. Why do you think that you should use the array's content as ds_map key?
Well because the array that is used (and there are multiple arrays) have an action related to them. That action is what "solves" the array. In the beginning of the script, we have a goal array. At the end of the script, we look at the goal array and look at the action that satisfied it. Then we find the array that would be formed once that action is done and look at the ds_map again to see the action that satisfied it. We continue until we get to the last array. Then we know the order in which to do the actions in order to satisfy this goal array.

How else would I know which action solves which goal array?
 

Dmi7ry

Member
Well because the array that is used (and there are multiple arrays) have an action related to them. That action is what "solves" the array. In the beginning of the script, we have a goal array. At the end of the script, we look at the goal array and look at the action that satisfied it. Then we find the array that would be formed once that action is done and look at the ds_map again to see the action that satisfied it. We continue until we get to the last array. Then we know the order in which to do the actions in order to satisfy this goal array.

How else would I know which action solves which goal array?
I mean that use pointer to array will be better. GMS can't get pointer to array, but there are some workarounds.
For example, place all arrays into ds_list and then just pass the index.
 

Sammi3

Member
I mean that use pointer to array will be better. GMS can't get pointer to array, but there are some workarounds.
For example, place all arrays into ds_list and then just pass the index.
I think you've demonstrated the problem of when someone's been working on something for too long that they're too close to the problem in order to see things clearly. Weird how I never even considered this. I even at one point had a ds_list that contained ds_lists in place of the arrays (this was so that I could clear any memory leaks). Thank you.
 
Top