GameMaker [Solved] Spawn object only once on collision

I have a hay bale that if you stab, it emits have particles out of the side. The emitter works perfectly fine so I don't think that's the issue. When the emitter is created, it plays two frames and then destroys itself.

I want the stab of the obj_player to only activate the "create emitter" code once. I was able to accomplish this with similar code earlier today when I was trying to get the emitter to work, but I since deleted it since I didn't need it anymore. The problem is that the stab is a few frames long meaning it obj_player keeps contact with the hay bale for a bit so it wants to register a hit a number of times per second. Because of that, it creates more than one emitter. It should be one stab, one emitter creation.

I think the code I have almost works but something is out of order.

Create Event of obj_hayBale
stab = false;
counter = 0;

Step Event of obj_hayBale
Code:
if (obj_player.myState == STATE_PLAYER.stab) or
(obj_player.myState == STATE_PLAYER.walkstab) or
(obj_player.playerFightLockState == STATE_PLAYER_FIGHTLOCK.stab) or
(obj_player.playerFightLockState == STATE_PLAYER_FIGHTLOCK.walkstab) and //if obj_player has his sword out (obj_player has a timer that makes him hold out his stab animation for a quarter of a second)
(place_meeting(x, y, obj_player))
{       
        stab = true;
        if (counter < 1)
        {
            counter++; //ticks up to 1 if collision is initiated. It's supposed to keep the collision from activating 60 times a second.
        }
    
    if (stab == true) and (instance_number(obj_emitter) == 0) and (counter == 1) //if his sword is out, and there's no other emitters, and collision has only happened once then do this:
    {
        instance_create_layer(x, y-25, "Instances", obj_emitter);
        stab = false;
    }
}
counter = 0;
 

Slyddar

Member
There are numerous ways to fix it.
Try something like this.

Create a script called is_stabbing to check if the player is stabbing, which makes checking much easier to write out.
Code:
///is_stabbing()

//return true if player is in a state of stabbing

if (obj_player.myState == STATE_PLAYER.stab) or
(obj_player.myState == STATE_PLAYER.walkstab) or
(obj_player.playerFightLockState == STATE_PLAYER_FIGHTLOCK.stab) or
(obj_player.playerFightLockState == STATE_PLAYER_FIGHTLOCK.walkstab)
return true else return false;
Then you can just have something like this:
Code:
if !stab and is_stabbing() and place_meeting(x, y, obj_player)
{   
        instance_create_layer(x, y-25, "Instances", obj_emitter);
        stab = true;
}

//return to non stabbing state once player has finished stab action
if stab {
    if !is_stabbing() stab = false;
}
 
Top