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

GameMaker referencing a ds_list's ID using a string variable?

S

SmashAdams

Guest
So I have an object (obj_weapon_gui)
and in that object's create event, I've created a ds_list named SHOTGUN, and a ds_list named CURRENT_WEAPON.

The code snippet below is written in a script (scr_equip_weapon).
comparison.png
The top code snippet works. but it isn't ideal.
I have over 20 weapons, and I don't want to have to write 20 if statements for each weapon if i can help it.

The bottom code snippet does not work. but it's what I want to do.
i would only need that one line, because the string values for global.equippedWeapon are all the same as the names of my ds_lists.
ex:
- global.equippedWeapon = "SHOTGUN";
- SHOTGUN = ds_list_create();

- global.equippedWeapon = "KNIFE";
- KNIFE = ds_list_create();

So in essence i am trying to reference my ds_lists using a string variable instead of the ds_lists actual ID.
when the 2nd snippet runs, GMS2 throws me this error: "data structure index does not exist"

is there a way to accomplish what I want?
 

Perseus

Not Medusa
Forum Staff
Moderator
A DS list index is a numeric value (starting at 0 and getting incremented for subsequent lists) and the variables `SHOTGUN` and `KNIFE` hold these indexes. To get this index via a string, you could use variable_instance_get.

Code:
var list_ind = variable_instance_get(id, global.equippedWeapon);
 

FrostyCat

Redemption Seeker
A cleaner method would be to nest the lists inside maps.

Start with a map and then add lists to it:
Code:
weapons = ds_map_create();
ds_map_add_list(weapons, "SHOTGUN", ds_list_create());
ds_map_add_list(weapons, "KNIFE", ds_list_create());
Then you can reference the individual lists through the map keys in string form (e.g. weapons[? "SHOTGUN"]).
 
S

SmashAdams

Guest
A cleaner method would be to nest the lists inside maps.

Start with a map and then add lists to it:
Code:
weapons = ds_map_create();
ds_map_add_list(weapons, "SHOTGUN", ds_list_create());
ds_map_add_list(weapons, "KNIFE", ds_list_create());
Then you can reference the individual lists through the map keys in string form (e.g. weapons[? "SHOTGUN"]).
instead of doing:
ds_map_add_list(weapons, "SHOTGUN", ds_list_create());
I did:
ds_map_add_list(WEAPONS,"KNIFE",KNIFE);
ds_map_add_list(WEAPONS,"SHOTGUN",SHOTGUN);

which I guess only stored the ID's of my two lists into two keys in the map, rather than adding the two lists' contents to the map?

using:solution.png
I'm able to grab the values (ID) of: key = global.equippedWeapon inside the map, and use THAT as an argument for ds_list_copy....

i don't understand the end of this code though: ds_map_add_list(weapons, "SHOTGUN", ds_list_create());
my SHOTGUN ds_list has 9 different keys and values stored inside it. how would I put that list and it's contents inside my WEAPONS ds_map?
 

TheouAegis

Member
Frosty's method is ideal because you're not wasting variables, which you do. Your code is basically

KNIFE = ds_list_create();
WEAPONS[? "KNIFE"] = KNIFE;

Frosty's is just

WEAPONS[? "KNIFE"] = ds_list_create();

See? One less variable, so essentially 3 less CPU cycles.

You don't store the contents of the lists in the map, you store the IDs of the lists inside the map. You must access the lists via the map.
 
Top