How do I pause or stop a recurring function? (SOLVED)

Not favoritedFavorited Favorited 0 favourites
  • 14 posts
From the Asset Store
Template for dungeon/maze generation, using wave function collapse
  • So I noticed a loop that happens because the recurring function doesn't end. How would I make the recurring loop essentially freeze so that this loop doesn't happen?

  • What recurring function? Did you mean a recursive function, which calls itself? Or do you call the function in a loop?

    Please post a screenshot of your code.

  • So I noticed a loop that happens because the recurring function doesn't end. How would I make the recurring loop essentially freeze so that this loop doesn't happen?

    If it's an actual loop, you can use a break, or if it's functions going into each other, you can use an index varible to only run the other functions a certain amount of times. You could also use a "Wait for signal"

  • What recurring function? Did you mean a recursive function, which calls itself? Or do you call the function in a loop?

    Please post a screenshot of your code.

    This was about the (SOLVED) sprite instance spawning I had asked about earlier, in which a regular timer called the function.

    construct.net/en/forum/construct-3/how-do-i-8/spawn-sprite-instances-185929

    However, I noticed that upon the shape being built, the moleks would loop spawning and then destroying themselves immediately after, which I think is being caused by this little snippet here:

    How would I make it so that that doesn't happen, instead pausing the spawning for a while and then resuming until the final shape is built? (which would be the third stage)

  • I still don't understand your question. But if you want to pause the function, then just don't call it. Check for some condition, say, use a variable - spawningIsPaused. Set it to true when you need to pause spawning.

    If NOT spawningIsPaused -> Call SpawnNewMolecule

    Then set it to false to resume spawning.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • I still don't understand your question. But if you want to pause the function, then just don't call it. Check for some condition, say, use a variable - spawningIsPaused. Set it to true when you need to pause spawning.

    If NOT spawningIsPaused -> Call SpawnNewMolecule

    Then set it to false to resume spawning.

    This works only somewhat.

    Not entirely sure why, but I call the pause to happen in certain states and it doesn't function as intended. I think it may have to do with the fact that the Background sprite is the one with the timer and the instance variable of Pause, and I'm calling on booleans from other sprites to Pause, like so:

    Moleks still appear even after challenge is on, but only showing from one corner. I'd rather it didn't spawn at all, if not from the Chemic itself, which I've already done separately.

    If not, maybe I'm overlooking something crucial, because I also have a sequence of events happening here via function:

    * On function 'Cutscene'

    -> Background: Set Pause to True

    -> Chemic: Set 8Direction Disabled

    -> Plan: Set animation frame to 0

    -> Plan: Stop animation

    -> System: Wait 1.0 seconds (use time scale: True)

    -> Chemic: Subtract 1 from child_count

    -> Molek: Set attached to False

    -> Molek: Set Bullet speed to 500

    -> Molek: Set Bullet angle of motion to angle(0,0,Self.dx,Self.dy) degrees

    -> Molek: Set Bullet acceleration to 0

    -> Molek: Start animation from beginning

    -> Molek: Set collisions Disabled

    -> Chemic: Subtract 1 from child_count

    -> Molek: Set attached to False

    -> Molek: Set Bullet speed to 500

    -> Molek: Set Bullet angle of motion to angle(0,0,Self.dx,Self.dy) degrees

    -> Molek: Set Bullet acceleration to 0

    -> Molek: Start animation from beginning

    -> Molek: Set collisions Disabled

    -> Chemic: Subtract 1 from child_count

    -> Molek: Set attached to False

    -> Molek: Set Bullet speed to 500

    -> Molek: Set Bullet angle of motion to angle(0,0,Self.dx,Self.dy) degrees

    -> Molek: Set Bullet acceleration to 0

    -> Molek: Start animation from beginning

    -> Molek: Set collisions Disabled

    -> Chemic: Subtract 1 from child_count

    -> Molek: Set attached to False

    -> Molek: Set Bullet speed to 500

    -> Molek: Set Bullet angle of motion to angle(0,0,Self.dx,Self.dy) degrees

    -> Molek: Set Bullet acceleration to 0

    -> Molek: Start animation from beginning

    -> Molek: Set collisions Disabled

    -> Audio: Play "Cutscene" not looping from Sounds at 0 dB (stereo pan 0, tag "")

    -> System: Wait 1.0 seconds (use time scale: True)

    -> Chemic: MoveTo: Move to (540, 1290) (Direct)

    -> System: Wait 3 seconds (use time scale: True)

    -> Audio: Play "Start" not looping from Sounds at -10 dB (stereo pan 0, tag "")

    -> Background: Spawn Build on layer 6 (image point 0, create hierarchy: False, template: "")

    -> Build: Set position to (180.909, 747.765109)

    -> Build: Fade: start fade

    -> Plan: Start animation from beginning

    -> System: Wait 5.0 seconds (use time scale: True)

    -> System: Save game to slot "mysave"

    -> Chemic: Set 8Direction Enabled

    -> Chemic: Set Cutscene to False

    -> System: Wait 1.0 seconds (use time scale: True)

    -> Audio: Play "World1" looping from Music at -10 dB (stereo pan 0, tag "")

    -> Background: Set Pause to False

    As you can see, the Pause boolean is set to false at the very end, and yet the molek shows right when the "Build" text appears and not later.

  • I think it may have to do with the fact that the Background sprite is the one with the timer and the instance variable of Pause, and I'm calling on booleans from other sprites to Pause, like so

    It doesn't matter where the Pause variable is, as long as you're correctly checking it and stop the function from running when Pause=true.

    Please post screenshots of your code.

  • This is what I have so far:

  • The second event doesn't make sense. It's running on every tick, constantly restarting the timer (so the timer will never trigger), and it's calling the spawn function on every tick!

    I still don't understand how and when the molecules should spawn in your game, but here is how the timer is supposed to be used:

  • I still don't understand how and when the molecules should spawn in your game, [...]

    Ah, I see. So when the last molek is collected, OR after a certain time passes without it being collected, when Chemic (the main sprite) isn't in miss mode, challenge mode, cutscene mode, or the shape in the center being built, as here for an example:

    That's when I want the moleks to spawn, one at a time. I did the second line so that very first statement can be applied, but wasn't aware it ran every tick.

    I have booleans and variables for all of these options.

  • The easiest way I can think of is Having a spawn state Boolean attached to the game/player state.

    If PlayerState = CutScene

    Set Var CanSpawn false

    Then add a condition on the call function like

    If CanSpawn = true

    Call function.

    If that makes sense. That would be the easiest way to do it.

    Then if you want it to spawn one on a timer, You can attach a timer to each molecule,

    When spawn, start timer, after X seconds set CanSpawn to true. Spawn the molcule then set CanSpawn to False....

  • The easiest way I can think of is Having a spawn state Boolean attached to the game/player state.

    If PlayerState = CutScene

    Set Var CanSpawn false

    Then add a condition on the call function like

    If CanSpawn = true

    Call function.

    If that makes sense. That would be the easiest way to do it.

    Then if you want it to spawn one on a timer, You can attach a timer to each molecule,

    When spawn, start timer, after X seconds set CanSpawn to true. Spawn the molcule then set CanSpawn to False....

    That works some.

    What makes it tricky is the spawn after the last molek gets attached. The attached state is a boolean as well, for the molek sprite. So another molek should spawn immediately after and reset the timer each time the last molek gets attached... or just simply wait x amount of seconds to pass from said timer without attach for another molek to spawn. (this second part seemed easy enough, but the first eludes me)

    Mind you, I had it working before, but when the center shape was built, the spawn-destroy cycle would continue in repeat until the next shape was called for, then act as normal.

  • So another molek should spawn immediately after and reset the timer each time the last molek gets attached... or just simply wait x amount of seconds to pass from said timer without attach for another molek to spawn.

    Hopefully this helps you understand the logic so you can move on with your project:

    dropbox.com/scl/fi/qz2oeyasgghsfkueek72i/MoleksSpawning.c3p

  • Hopefully this helps you understand the logic so you can move on with your project:

    It's been a massive help. With some personalizations, it works. Thanks! :)

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