GameMaker ds_list_find_index return -1

Hey everyone,

I got 3 objects:
  • obj_gun
  • obj_player
  • obj_gun_pickup
obj_gun and obj_player have a ds_list called binded_instance, initialized in the Create Event of each with binded_instances = ds_list_create();

There is my Step Event for obj_gun_pickup:
Code:
with (obj_player)
{
   if place_meeting(x, y, other) && key_action
   {
       var that_player = instance_nearest(x, y, obj_player);
       var that_gun = instance_nearest(x, y, obj_parent_gun);
       unbind_instances(that_player, that_gun);
       instance_destroy(that_gun);
       instance_create_layer(x, y, "Gun", obj_semiauto);
       bind_instances(that_player, instance_nearest(x, y, obj_gun));
   }
}
The bind_instances and unbind_instances add (or delete respectively) the arg1 to the binded_instance ds_list of the arg2 and vice versa.
The obj_parent_gun is the parent of obj_gun.

In the Begin Step Event of my obj_gun, i use the show_debug_message (to simplify and debug my code) and I show
Code:
string(ds_list_find_index(binded_instances, obj_player))
And this print "-1" but - and that is the point - in the Variables widow of the debugging mode, I see that there is an instance of obj_gun in my binded_instances ds_list of my instance of obj_player.

I really do not understand, my code and debug "contradict each other".
 
H

Homunculus

Guest
You are adding instances to the ds_list, and you are using ds_list_find_index to look for an object. Unfortunately, this doesn't work. ds_list_find_index will look for the exact value inside the list, you can not use objects and instances interchangeably like you do with, for example, place_meeting or instance_nearest.
 
C

Catastrophe

Guest
Essentially, if you want to do what you are doing, you would need to loop through the ds_list, and check if (i.object_index = obj_player || object_is_ancestor(i.ObjectIndex,obj_player))

Alternatively, make a ds_map of object_index->instance, but that sounds too rigid since you could only ever have one of every instance. But maybe that's fine for your game.
 
Top