How do I stop a function from executing multiple times in a single frame?

Not favoritedFavorited Favorited 0 favourites
  • 6 posts
From the Asset Store
three fully animated characters ideal for your RPG side scroller projects
  • For context, I am making a metroidvania-style game and the specific issue is with my Knockback function that triggers when the player slashes at an enemy or interactable object. If there are multiple enemies or objects that the slash sprite collides with on the same frame, the knockback function is called a bunch of times and launches you into orbit.

    Most solutions to this seem to involve adding an additional condition to calling the function, but I call the Knockback funtion all over the place so it would take a really long to edit every single call. I'm trying to find a solution that only involves editing the function itself if possible.

    Thanks for reading, and please let me know if you have any ideas.

  • One approach might be to store the tickcount expression in a global/static variable. When the knockback function is called, if the current tickcount is greater than the stored tickcount, apply knockback and update the stored tickcount. If the function is called and the stored tickcount equals the current tickcount, then you can ignore it as it was already called this frame.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • You could set a variable inside the function when you knockback the player. Then just check to see if that variable is set in the function to prevent repeats. Then you’d just reset the variable later. Either at the end of the frame or after some time later.

    Or some variation of that.

    Another idea could be to accumulate the impulse into variables then a one point in your events clamp the value and apply it.

  • One approach might be to store the tickcount expression in a global/static variable. When the knockback function is called, if the current tickcount is greater than the stored tickcount, apply knockback and update the stored tickcount. If the function is called and the stored tickcount equals the current tickcount, then you can ignore it as it was already called this frame.

    Thank you for responding, but that didn't seem to work for me. The function is still called multiple times on the same frame. Did I implement it wrong, or is there another way I should do this?

    I also tried ROJOhound's solution but it didn't appear to change anything either.

  • Start a knockback cooldown timer inside your function. Add a condition to the function itself to not run if the timer is running. The timer duration can be as low as a single frame.

  • Start a knockback cooldown timer inside your function. Add a condition to the function itself to not run if the timer is running. The timer duration can be as low as a single frame.

    Thanks for the response, and sorry for the late reply. I'm assuming you mean using a cooldown variable and lowering it every tick if it's over 0 and raising it in the function actions. If so, I already tried that too.

    I think that these solutions involving checking veriables in conditions and editing them in actions aren't working because the function is being called multiple times at the exact same time, so there isn't really an order of the calls to stop every call after the first because there is no first. But that's just my hunch, I still don't really know what the issue is exactly or how to fix it.

Jump to:
Active Users
There are 0 visitors browsing this topic (0 users and 0 guests)