GML Collision with specific instances of an object

FullCup

Member
I'd like to put an instance of an object into a "collidable group" in some cases. For example:

If the player collides with a "water coin" INSIDE the water, he CAN collect it (just an example).

My first thought was to change the parent just before the collision check and then return to the original.

GML:
   //Set the collidables
   with (obj_water_coin) {if (in_water) object_set_parent(obj_collidable);}

   //Collision
   if place_meeting(x,y,obj_collidable) {...

   //Normal state
   with (obj_water_coin) {object_set_parent(main_parent);}
But... I can't change the parent, there's another way?
 

samspade

Member
I don't think that object_set_parent is a real function. Is it?

That said, even if you could do this, you probably wouldn't want to. A better solution would be to do the same thing but just have a variable that is toggled to true or false and then in a collision with obj_water_coin, instead of immediately collecting it, you check against that variable. And you don't even need to do that because you have the variable in_water. You could also flip the check around (so below is an example of an instance checking for the water coin).

GML:
//Collision
with (obj_water_coin) {
    if (place_meeting(x, y, other)) && (in_water) {
        ....   
    }
}
You could probably also toggle the mask on and off, but that is a solution which could have other consequences if you are using the mask for anything else. You could also have the water coin have a hitmask that follows it which is created/destroyed that you actually check for collisions with.
 

FullCup

Member
object_set_parent doesn't exist :v, was just a "function example" too.

The mask sysyem is a good ideia! And there's no problem if it needs the mask 'on' in other events, because the game runs line by line, so the other events will be after or before the collision check.

GML:
//...Here and before the code runs based in mask 'on'

    //*Change mask to 'off'

        //Here the code runs based in mask 'off'.
    
    //*Change mask to 'on'
    
//Here and after the code runs based in mask 'on'...
the 'with (obj) {place_meeting(.... ' is good too, i used it in other code of a moving platform collision, the problem is because all the platforms (or coins) will check the collisions instead of one check of the player, but it wouldn't be a problem if it were that way. Thank you for replying!
 

FullCup

Member
The
I don't think that object_set_parent is a real function. Is it?

That said, even if you could do this, you probably wouldn't want to. A better solution would be to do the same thing but just have a variable that is toggled to true or false and then in a collision with obj_water_coin, instead of immediately collecting it, you check against that variable. And you don't even need to do that because you have the variable in_water. You could also flip the check around (so below is an example of an instance checking for the water coin).

GML:
//Collision
with (obj_water_coin) {
    if (place_meeting(x, y, other)) && (in_water) {
        ....  
    }
}
You could probably also toggle the mask on and off, but that is a solution which could have other consequences if you are using the mask for anything else. You could also have the water coin have a hitmask that follows it which is created/destroyed that you actually check for collisions with.
Hey! By the way, i watch your videos. They helped a lot! Good job.
 
Top