• 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 ds_lists and child objects

Hi guys!
I have an issue with ds lists. I got some object obj_shotgun, obj_laser... , childs of "obj_parent_gun". My obj_player as a ds_list named "binded_instances" where i store all the objects "attached" to my player (to differanciate objects, like guns, of my player or the one of a monster), but the ds_list_index function don't work when we call the parent object, and i want to use it to find, at least, one iteration of a child object of obj_parent_gun in my list. Does somebody have a solution?

Code:
// The call that I use
ds_list_find_index(variable_instance_get(obj_player, "binded_instances"), "obj_parent_gun")
 
H

Homunculus

Guest
You'll have to iterate the list one instance at a time yourself, anche check if the object_index matches the one you are looking for. If you have actual instances in the list, you can't simply use ds_list_find_index with the object index or object name.

Why are you using variable_instance_get though? That's a compatibility function, is you are outside of the player object when running this code, just use a with statement or obj_player.binded_instances.
 

samspade

Member
While there are a few good use cases for variable_instance_get/set that are not compatibility related, it doesn't seem to be useful here. Presumably, you simply have a binded_instances list in the obj_player which is either empty (no binded instances) or not empty (at least one binded instance). You could then just reference it like this:

Code:
ds_list_find_index(binded_instances, "obj_parent_gun")
There's a couple other things to note as well - if you posted your code as you are actually using it - you aren't assigning the returned value to anything, so that code could be running correctly it just won't do anything, and you're searching for a string which might be right or wrong, but object ids are not strings.

Again, perhaps you didn't post your code as it exists in your project, and maybe there's some other things going on that aren't obvious, but normally I would expect to see something closer to this:

Code:
//from inside the player object
var _index = ds_list_find_index(binded_instances, obj_parent_gun);

//from outside the player object (assuming only one instance of player object, else you need an instance, not an object id)
var _index = ds_list_find_index(obj_player.binded_instances, obj_parent_gun);
Also note that searching for the object id obj_parent_gun will not find a child of that object. So whether or not it succeeds would also depend on what you add to the list.
 
Top