Knowing when a function has finished.

0 favourites
  • 11 posts
From the Asset Store
Template for dungeon/maze generation, using wave function collapse
  • I have an issue where I want to change a variable only when a number of functions have completed. They're quite slow (about half a second to process them all). When they're done, I want to reset a variable, but if I put the variable reset after the functions, it seems to reset immediately, while the functions then dawdle along. I was under the impression that the final instruction wouldn't complete until the previous ones had completed, in order. Are functions threaded or something?

  • Functions are called by you and execute synchronously, so if you call three functions, one after another, it would be your next action that signifies they are done. You can't put something "after" the function declarations, that just gets called like every other event.

    Maybe post what you are trying to do.

  • I've forgotten how to post a screenshot, but in short it's like this...

    Call Function A

    Call Function B

    Call Function C

    Set Variable to 0

    All in the same event.

    A later event, then tries to respond only when the variable is set to zero (i.e 'after' the functions have completed), but it responds immediately even though the functions still appear to be processing. I basically need to set the variable only when the functions are done with their processing.

  • What are your functions doing? Each will return "immediately". If you have Waits, etc, the function will still return immediately. Waits do not block.

  • Well, my game is turn-based. On my PC, the functions complete fairly quickly, but on my phone, they can be very slow. So what I wanted to do was to disable any buttons, put up a 'Please wait' message, run the processing functions, then remove the message and re-enable the buttons.

    So I guess my misunderstanding is in thinking that if the function takes a while (say 1 second) to complete, the whole game locks up while it's processing (actually, I've noticed that sprites stop moving etc in tests when I do this). But what you're saying is, even if the function takes a second to complete, the rest of the program continues to process? So how does the function process, is it processing in the background on a thread?

    To solve my problem, how do I know when the function has completed processing, so that I can remove my message and restart the next turn?

    I have capx to demonstrate but not sure how to post it.

  • Try dropbox.

  • Functions run just the same as if all of that code was right where the call is made, they are not in the background. Something else must be going on.

  • Ok, hopefully this Dropbox link will work. Hopefully it demonstrates where I'm going wrong. There are no functions, but what it does is, when you click the pink square, it 'should' change colour to blue (frame 1), then, when the loop has ended, change back to pink (frame 0). You can see that there's a pause due to the loop on line 3, but you can't visibly see the square change colour, as it's instantly changed back on line 4, while line 3 is apparently still processing.

    So my confusion here is, how can I get line 4 to execute AFTER line 3 has finished? It's not the functions that are confusing me, rather, how do I know when a group of instructions are finished, so that I can then do something else (e.g remove the 'Please wait' message as I said above).

    https://dl.dropboxusercontent.com/u/112519761/func.capx

  • Not sure if this will help, but if you have a long operation, you need to force it to run on the next tick at least, so that the current tick can finish and any changes can be viewed. Otherwise, as you've seen, C2 does not actually finish updating things until it reaches the end of the event sheet. That's when the GUI is actually refreshed.

  • Ah thanks, this looks like what I was looking for. This should work for my game, with a few alterations, thanks for your time

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • This solves half the problem at least. Putting the timer in allows me to position the 'Please Wait' message correctly. Problem now is, when do I remove it? It needs to be removed when the functions have completed (also buttons deactivated and activated again). I'm thinking right now of setting some booleans to say the functions have started, then have them reset to false on the last line of the function (presumably that's the 'end' of the function?). Then I can test when they're all false and reset everything. I'll see how it works out.

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