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

GML How to check for a collision with one specific instance just created?

V

viniciuscsg

Guest
After reading the documentation on collision methods I couldn't find a complete solution for this situation: I have an object 1 with a create instance action at its create event. That action creates an instance of an object 2. Then I need to make a collision event (or check for collisions in code) that will trigger only when object 1 collides with the that specific instance which was created in the create event.

If I set up a simple collision event and select object 2 as target it will register collisions of object 1 with any instance of object 2, but that is a problem because there is a bunch of those in the room, since each instance of object 1 spawns its own copy of object 2 ( the game works like that if all objects are static and there is no chance of they touching each other, but wont work as intended if they move and collide).

I suppose I need to check for the instance ID when creating it, and store it in a variable by defining it = to the argument that creates the instance. But then, what would be the syntax to check for collisions with it using any of the collision functions that have an obj as parameter? I cant find that anywhere in the manual. Is it possible to instead simply use the collision event and make it to trigger only when touching that specific instance of object 2 created by the object 1?

Does anyone knows how would I go about doing that? Thank you in advance!
 

NicoFIDI

Member
Code:
with(instance_create(random(room_width),random(room_height),obj_created)) {
    while(place_meeting(x,y,obj_wall)){
        x = random(room_width);
        y = random(room_height);
    }
}
 
V

viniciuscsg

Guest
Wow, thanks! I'll give it a try soon and report back! I was under the impression that I had to use the instance_place function to do this.
 

NightFrost

Member
If you need to know the id of the collided instance for further processing, you would use instance_place. If you just need to check for a collision, you use place_meeting (it returns true/false) because it is faster.
 
Top