• Hello [name]! Thanks for joining the GMC. Before making any posts in the Tech Support forum, can we suggest you read the forum rules? These are simple guidelines that we ask you to follow so that you can get the best help possible for your issue.

Windows How to determine instance ID of closest object collided with.

creagan

Member
I am using the command: collision_line_list(x - 100, y, x + 1000, y, <object>, false, true, <ds list>, true) to retrieve the objects that collide with a line.

It works well and shows me the instance ID of the nearest object hit. Unfortunately, the instance ID in a ds list is not the same as the instance ID's specified elsewhere. For example, the ds list will say the ID of the closest object is 100003 and the instance ID for that same object elsewhere will be 3.

Any way to match the ds list instance IDs with the instance IDs specified elsewhere?
 
Instance IDs will never be 3 or some such a low number. You might be thinking of object IDs, which could be that low but are an entirely different thing to instance IDs and you shouldn't be using object IDs when trying to talk about a specific instance in your game. The values in the ds_list are correct and since you are the last argument in the collision function as true, they will be sorted by distance correctly.
 

creagan

Member
Instance IDs will never be 3 or some such a low number. You might be thinking of object IDs, which could be that low but are an entirely different thing to instance IDs and you shouldn't be using object IDs when trying to talk about a specific instance in your game. The values in the ds_list are correct and since you are the last argument in the collision function as true, they will be sorted by distance correctly.
OK, that's helpful, but the goal of all this is to get that particular instance's x & y position and manipulate that instance in other ways later. I can't seem to find a way to do that simple thing, even after going over the manual for the past 2 hours. I'm a newbie, so I may be missing something obvious.
 

chamaeleon

Member
OK, that's helpful, but the goal of all this is to get that particular instance's x & y position and manipulate that instance in other ways later. I can't seem to find a way to do that simple thing, even after going over the manual for the past 2 hours. I'm a newbie, so I may be missing something obvious.
Whatever collision_line_list() stores in the list are instances, which you can use to check variable values like position, or modify them if you wish.
GML:
var my_list = ds_list_create();
if (collision_line_list(<arguments>) > 0) {
    var the_closest_instance = my_list[| 0]; // assuming list was requested to be sorted by distance
    the_closest_instance.y += 100;
    the_closest_instance.sprite_index = my_other_sprite;
}
ds_list_destroy(my_list);
 

creagan

Member
Whatever collision_line_list() stores in the list are instances, which you can use to check variable values like position, or modify them if you wish.
GML:
var my_list = ds_list_create();
if (collision_line_list(<arguments>) > 0) {
    var the_closest_instance = my_list[| 0]; // assuming list was requested to be sorted by distance
    the_closest_instance.y += 100;
    the_closest_instance.sprite_index = my_other_sprite;
}
ds_list_destroy(my_list);
Oh wow. Thanks. Too simple.
 
Top