# When there are multiple objects of the same type, how do I know which one I am colliding with?

I'm working on a platformer game using Gamemaker Studio 2. , and currently I'm dealing with implementing platforms (those thin tiles that are able to pass through from the bottom, but remains solid when landing on).

To use some more advanced collision detection, I'm using place_meeting() instead of the build-in Collision Event.

It's working good and well for a single platform, but when I add multiple, then only the lowest placed platforms will work properly. I'm already aware that the problem lies on the second if-statement in the code below:

if (place_meeting(x, y+mySpeed[1], o_platform) &&       //If the player collides with a platform
mySpeed[1] > 0  )                                   //if the player is falling
{
if ((y + 29) < o_platform.y)                        //if the player's feets are located higher than a platform
{
while !place_meeting(x, y+sign(mySpeed[1]), o_platform)
{
y += sign(mySpeed[1]);
}
mySpeed[@ 1] = 0;
}
}


The problem is likely because it's reading information from any platform. but not the higher platform I'm colliding with. So I should need a check to make sure the program knows it's the same object I'm colliding with.

If this was the build-in Collision Event, I could use the other statement to get the right instance, but currently I'm using place_meeting in a step method to collide, and other would be too broad to use properly. So how would I be able to archieve this?

Instance_place did the trick.
If you want to use the object ID outside the collision event, then you can use Instance_Place instead of Place_Meeting.

var myObject = instance_place(x, y, object)
if (myObject != noone)
{
//...you can use myObject as the object it's colliding with.
}

• I have some doubt this solution works best if your character is colliding with many platforms at time. May 11, 2018 at 7:17
• Could be, but I don't think that can happen in my case. I've an instance solid that I place over the layer of tiles. so there will only be one platform at a time. But it's good to take a notice of it in case it may go wrong. If you've a different solution, then let me know. May 11, 2018 at 7:42
• In case I need to come back to this for multiple collisions at once, I think this one can help: gamedev.stackexchange.com/questions/122130/… May 11, 2018 at 9:43
• Have an array objects (or a dict of )
• Give your objects a ulong ID field
• Whenever you create an object, assign it an ID incrementally and then index it into the array with that id

Now whenever you collide with an object you just called:

var myCollider = arrayOfObjects[object.ID]

NOTE: It'd be better to do this with a Dictionary if you're in C#, but since I don't know what language you're in I gave you a starting point of using an array. An array in this implementation may well waste a lot of memory if you have lots of destructable objects.

The other way to do this is usually done by a Delegate (or a callback depending on your language.) I would imagine if you're in game-maker that you can probably find something out there on this; but the above will work either way.

• I'm currently using Gamemaker Studio 2, who uses Game Maker Language. It's close to C#, but not the same. Sorry that I didn't mention that before. So far I've already tried 'memorising' the object ID by making it a variable before checking the if-statements. but that didn't worked out. Though I'm not sure if that works the same way as using arrays. I'll try and see if your other options might work. May 8, 2018 at 6:30
• This question was tagged with game-maker at the time of answering. So the engine was already known when answering. Do proper research before answering. Dec 28, 2018 at 7:20