GMS 2 (Solved+) How can I refer to a variable/array/etc using an asset name?

G

Geoff Moore

Guest
I'm creating an array of variables to track progress for each area in my game like so:

Code:
area_001_state=[0,0,0]
area_002_state=[0,1,0]
area_003_state=[1,0,1]
...
The first array position for each area could indicate whether the lights are on to start with, for example. Then when I want to change a value, I'm doing:

Code:
if room=area_001 {area_001_state[0]=1;}
else if room=area_002 {area_002_state[0]=1;}
else if room=area_003 {area_003_state[0]=1;}
...
Since I'm using the same naming convention for my arrays and rooms, can I shorten the second code block to a single line that will modify the first value of whichever array shares the room name with "_state" added at the end?

(Non-working) pseudocode would be:
room_get_name(room)+"_state"+[0]=1;

Any advice on how to do this would be appreciated, or let me know if there's a better approach in general. Thanks for reading!
 

Homunculus

Member
Arrays can be nested and / or added to data structures, you should take advantage of that. For example, you could use the room index or name to have a direct access to the room data using a ds_map:

Code:
//create and add the default arrays
area_state = ds_map_create();
area_state[? area_001] = [0, 0, 0];
area_state[? area_002] = [0, 1, 0];
area_state[? area_003] = [1, 0, 1];
Code:
//get and change the state of a variable in the current room
var _state = area_state[? room];
_state[@ 0] = 1;
Please note that due to how arrays work in game maker, you need to use the @ accessor in the state arrays, otherwise you will modify a copy of the array and not the original data stored in the map (more info here and here).

You should also consider using an enum to access the area information in the array, this makes your code much more readable and maintainable. Example:
Code:
var _state = area_state[? room];
_state[@ AREA_STATES.lights_on] = 1;
 
G

Geoff Moore

Guest
Oh, nice! This looks like just what I need, thank you @Catan!

Edit: Yep, works perfectly for my needs. You've piqued my interest with the enum thing, it would be nice to use lights_on for example. I couldn't find much info about enums in Game Maker, I'm guessing they're fairly underused. How would I need to set the map up to be able to make the call in the last snippet you posted?
 
Last edited by a moderator:

Homunculus

Member
There isn’t much to know about enums, they are essentially constants, you are just replacing the hardcoded number with a named variable having the same value.

All you need is explained in the official documentation, if after reading that you still have questions about their usage just ask
 
G

Geoff Moore

Guest
Ah, gotcha - sorry, it's too hot/late : ) I think rather than an overall AREA_STATES enum what I'd want is one enum for each of the 'columns' in my ds map, right? So if the first value corresponds to lights off/on, I'd have:

Code:
enum light_states {lights_off, lights_on}
So that 0 would be off and 1 would be on in the first column, and then define another enum for each column? If that's right, I think I've fully understood the concepts. This is going to be so helpful for keeping things organised, thank you again!

Edit: Marking this as solved, my system's set up great now. I'm going to be using enums for pretty much everything going forward, like you said seeing things in plain English makes it way easier to understand what's going on and spot problems. Cheers!
 
Last edited by a moderator:
Top