Flagging the end of a recursive function

0 favourites
  • 9 posts
From the Asset Store
Flags
$5 USD
An educational memory game about countries’ names and their flags!
  • I'm using a recursive function to sort an array in a specific way but I can't work out a good way to flag when the function is complete.

    The recursive function itself works fine, I just can't action something when it ends.

    What I want is for the the function to be initially called by another event. Then when the function has fully completed sorting the array I want it to call a different function.

    I've tried a few different methods but can't nail it. Help would be greatly appreciated.

    1drv.ms/u/s!AkmrWgxeuxlKhJwBQwo7XM7mMAoluQ

  • It should be enough to just call a second function in the “on space” event. Then when the first function finishes the second will be run.

  • In this case it would just work like ROJO said.

    But there are async functions for this purpose as well, set the function to asyncronous in the function dialog (not the call dialog).

    Then you can use wait for previous action after the function call.

    you can read about it on the documentation.

    https://www.construct.net/en/make-games/manuals/construct-3/project-primitives/events/functions

  • It should be enough to just call a second function in the “on space” event. Then when the first function finishes the second will be run.

    While this works, if the function took more than one tick to complete would it break as the next function would be called prior to the completion of the first, which it is dependent on?

    An Asynchronous function with a "wait for previous events to finish action" would likely also be unreliable as the function is recursive, so technically it finishes many times before actually being finished.

  • ah yea, my bad.

    Wouldn't it be the case if the loop ran without the sub event ever being true the other function should be called.

  • In the screenshot the function was called normally so it can’t take multiple frames to complete. It would run till it’s done.

    If it was run asynchronously I guess it could take multiple frames.

    The array could be considered sorted if the sub event isn’t run once in an entire loop. You could use a variable for that: set it to 0 before the loop, set it to 1 in that sub event, then check if the var is still 0 after the loop. But then all the previous recursions would need to finish all their loops of doing nothing before it was truly done.

    Another idea is to pass a depth parameter to the function When you first call the function use 0. Then each recursed call would be depth+1. Finally add another event to the function to check if depth=0, and if it is the whole function is done.

    Personally I’d write the sort in a non recursive way. Like sorting one column at a time. If for no other reason but to be more efficient. Your function will sort but it will be looping over the array a lot.

  • In the screenshot the function was called normally so it can’t take multiple frames to complete. It would run till it’s done.

    Of course, that makes sense and means it should work. Thanks, my tired brain never even considered that it is actually restricted to running within that tick.

    The array could be considered sorted if the sub event isn’t run once in an entire loop. You could use a variable for that: set it to 0 before the loop, set it to 1 in that sub event, then check if the var is still 0 after the loop. But then all the previous recursions would need to finish all their loops of doing nothing before it was truly done.

    I tried this method and it consistently failed. Likely due to how recursive functions stack.

    Another idea is to pass a depth parameter to the function When you first call the function use 0. Then each recursed call would be depth+1. Finally add another event to the function to check if depth=0, and if it is the whole function is done.

    I had also tried this exact method but it failed with inconsistent results, often stopping the function before the sorting was complete. Couldn't work out why.

    Personally I’d write the sort in a non recursive way. Like sorting one column at a time. If for no other reason but to be more efficient. Your function will sort but it will be looping over the array a lot.

    I agree, it's not the most efficient but I can't think of a way to sort all the non zero values in a single column to the bottom without recursion.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Here's a way to do it in a non-recursive way which loops over the array once. Basically it does it column at a time, bottom to top. It keeps track of the lowest 0 slot and if a cell is non zero it moves the value down to that spot.

    dropbox.com/s/4nip6ho8j2c8h0c/column_fall.capx

  • Thanks ROJO that's much neater.

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