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

Legacy GM [SOLVED] Destroying other object

H

HalfSquat

Guest
I have code in the step event in my player object that's supposed to destroy the other object when the player touches it, but for some reason the game keeps trying to destroy the player object itself. I have no idea why this is happening.

Code:
if (place_meeting(x, y, obj_spiritBlock))
{
    if dashGun = true || rocketGun = true || powerGun = true || multiGun = true   
    {
        instance_destroy(other)
    }
}
 

TsukaYuriko

☄️
Forum Staff
Moderator
You are using other out of context. The only times it's valid are inside of a Collision event or a with statement. place_meeting in an if statement is neither of those.

Use instance_place instead of place_meeting, store the returned instance ID and, with all other conditions fulfilled, destroy that one.
 

Fredrik

Member
Correct me if I'm wrong (?)

Code:
if (place_meeting(x, y, obj_spiritBlock))
{
    if dashGun = true || rocketGun = true || powerGun = true || multiGun = true   
    {
        with (other) instance_destroy();
    }
}
 

Slyddar

Member
Correct me if I'm wrong (?)
Alas you are wrong. The 'other' keyword cannot be used with place_meeting, as place_meeting does not return the id of the colliding instance. You need to use instance_place to get the id back, then you can just use that id to refer to the colliding instance.
 
Last edited:

curato

Member
if you are going to code the collisions manually then you need to get that instance yourself with collision_rectangle or something to that effect.

Off topic: I don't know that I would use with together with other it is just one instance id it would be better form to destroy it explicitly but using it as a parameter if that is all you want to do.
 
H

HalfSquat

Guest
You are using other out of context. The only times it's valid are inside of a Collision event or a with statement. place_meeting in an if statement is neither of those.

Use instance_place instead of place_meeting, store the returned instance ID and, with all other conditions fulfilled, destroy that one.
Forgive me, I don't know how to store an instance ID or how to write that using code. This is what I changed it to, but I know it's not correct.
Code:
if (instance_place(x, y, obj_spiritBlock))
{
    if dashGun = true || rocketGun = true || powerGun = true || multiGun = true  
    {
        instance_destroy(other)
    }
}
 

Slyddar

Member
Forgive me, I don't know how to store an instance ID or how to write that using code.
One of the best hints I can give in Gamemaker is to middle click on functions. Middle click on instance_place, and it will show you how to use it. You need to store the id of the colliding instance in a variable, then use with(variable) to access that instance, and in there you can destroy it.
 

TheouAegis

Member
Forgive me, I don't know how to store an instance ID or how to write that using code. This is what I changed it to, but I know it's not correct.
Code:
if (instance_place(x, y, obj_spiritBlock))
{
    if dashGun = true || rocketGun = true || powerGun = true || multiGun = true
    {
        instance_destroy(other)
    }
}
In older versions of GameMaker:
Code:
if dashGun || rocketGun || powerGun || multiGun {
    with instance_place(x,y,obj_spiritBlock) instance_destroy();
In GMS2:
Code:
if dashGun || rocketGun || powerGun || multiGun
    instance_destroy(instance_place(x,y,obj_spiritBlock));
 

TsukaYuriko

☄️
Forum Staff
Moderator
Whenever you don't know how to use a function, the manual usually offers a detailed description and usage example. Either use the search function of the manual or middle-click the function you're unsure about as mentioned before, and you've opened a Pandora's Box of knowledge readily available at your fingertips.

For example, the manual page of instance_place contains a usage example that can literally be applied 1:1 to the situation presented in this topic. ;)
 
H

HalfSquat

Guest
In older versions of GameMaker:
Code:
if dashGun || rocketGun || powerGun || multiGun {
    with instance_place(x,y,obj_spiritBlock) instance_destroy();
In GMS2:
Code:
if dashGun || rocketGun || powerGun || multiGun
    instance_destroy(instance_place(x,y,obj_spiritBlock));
Thanks for making that clear for me! It works now.
 
Top