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

Want to destroy specific instance

jaaldis

Member
Hey - So I've basically got a shop room which has three podiums. When I enter the room, an item instance (oShopItems) is randomly generated onto each podium. There are three similar chunks of code to generate each item, which look like this:

GML:
if room = rStuffPlaceInterior {
    var shop1 = collision_rectangle(280, 200, 318, 220, oShopItems, true, true)
    if shop1 = noone && global.storeClosed = false && (!shopItems) {
    var Obj1 = instance_create_depth(300, 211, 100, oShopItems);
    with (Obj1)
            {
            DrawHereX = 300
            DrawHereY = 211
            DrawObjectType = DrawWallObjs
            }
        shopItems = true;
}}

if room = rStuffPlaceInterior {
    var shop2 = collision_rectangle(240, 247, 286, 268, oShopItems, true, true)
    if shop2 = noone && global.storeClosed = false && (!shopItems1) {
    var Obj2 = instance_create_depth(262, 255, 100, oShopItems);
        shopItems1 = true;
            with (Obj2)
            {
            DrawHereX = 262
            DrawHereY = 255
            DrawObjectType = DrawLargeObjs
        }
}}

if room = rStuffPlaceInterior {
    var shop3 = collision_rectangle(315, 247, 336, 266, oShopItems, true, true)
    if shop3 = noone && global.storeClosed = false && (!shopItems2) {
    var Obj3 = instance_create_depth(325, 255, 100, oShopItems);
        shopItems2 = true;
            with (Obj3)
            {
            DrawHereX = 325
            DrawHereY = 255
            DrawObjectType = DrawSmallObjs
        }
    }
}
Now, what I want to do is have a trigger object in front of each podium (oBuyObject) which, when I press enter, will destroy the instance that corresponds with the podium its nearest to. I've tried using instance_nearest, tried using a collision_rectangle and collision_point variables but every time it just destroys all three instances. Any help would be much appreciated!
 

Tyg

Member
I dont see the destroy code, you may be destroying the object
use instance_destroy(self) on the object to be destroyed
only the instance affected should be destroyed
or have the instance pass its id
 
Last edited:

Nidoking

Member
Have you tried not declaring the instances of oShopItems as var and using those?

But my guess is that you're doing something wrong with the instance_destroy, which you haven't shown us, so I can't tell you what it is you did wrong.
 

Reprom

Member
What are you using as a trigger?

If it is event Keyboard Down, then you need to change it to Keyboard Press. The first one will be triggered every time the key is down, while the second one will trigger only once.
If you use a function keyboard_check, then change its to keyboard_check_pressed. The same thing applies in this case.
 

jaaldis

Member
Sorry I should have posted the "destroy" code - so this is in the "step" event for oBuyObject:

GML:
var inst = collision_point(300, 211, oShopItems, true, true)
if (inst != noone) && place_meeting(x,y,oPlayer) && keyboard_check(vk_enter) {
    with (inst) instance_destroy()
}
But it's destroying all three items...
 

gnysek

Member
It's destroing one item per step in above case, not three at once.
It's because you've used keyboard_check, so it destroys one object every step AS LONG AS KEY IS KEPT DOWN, and collision happens. You rather want keyboard_check_pressed, as @Reprom stated, so it will be executed only on keyboard state changed from not pressed to pressed. "pressed" and "released" events always happens for one frame only.
 

jaaldis

Member
It's destroing one item per step in above case, not three at once.
It's because you've used keyboard_check, so it destroys one object every step AS LONG AS KEY IS KEPT DOWN, and collision happens. You rather want keyboard_check_pressed, as @Reprom stated, so it will be executed only on keyboard state changed from not pressed to pressed. "pressed" and "released" events always happens for one frame only.
Hmm, I do get what you're saying but even when I change it to keyboard_check_pressed it still deletes all three.
 

Nidoking

Member
I notice that you've got a lot of extra stuff that you're keeping track of that you probably don't need. Each oShopItems seems to have additional x and y draw coordinates stored, and you've got multiple booleans that seem redundant to me. I strongly suspect that you've got something tangled up in the parts you haven't shown, but this is the kind of problem where I have no idea where to advise you to look. I'd do a global search for oShopItems and see whether you're ever using that improperly.

Do you really have the location of the oShopItems hardcoded into the oBuyObject? How could you possibly refer to other oShopItems then? This looks like it shouldn't be able to destroy any of the other oShopItems, ever. All of these hardcoded coordinates should be relative values to something.

How certain are you that all three instances are destroyed, rather than simply not being drawn anymore?

Did you define a Destroy event for the oShopItems? What does that do?
 
Top