How do I wait until a function has completed?

0 favourites
  • Similar to my previous post, but that looks as though it's solved. I've solved part of the problem but I still need to know if there's a way to wait until a function has completed all of it's processing. I want my game to wait and disable the 'Next Turn' button until processing is complete (it's turn-based). Right now, I can disable the button, put in a tiny pause to show the new state of the button, then jump to my functions, then re-enable the button 'after' the functions, but what it does is, it calls the functions which begin processing and then immediately re-enables the button (even if that line is 'after' the functions or at the end of a function), meaning you can keep clicking for more turns while this one is still being processed.

  • var = 1

    some event:

    var = 1

    call function

    on function:

    do stuff

    var = 0

    var = 0

    trigger once:

    set text: "the function has finished"

    (or use wait for signal and signal)

  • ehhhmm..

    Aren't the functions performed in one tick?

    What is it that should be waited for?

  • Normally they are, if you have no wait actions etc in them.

  • My game is turn-based and the functions can take a long time (several seconds on my phone, less than a second on my PC). When you click the 'Next Turn' button, I need to show a 'Please Wait' type message and have the button(s) become disabled. Right now, you can click the button over and over while it's processing the functions and it basically queues more turns.

    From what I can tell, the functions aren't performed in one tick. It appears to call each function immediately but then process them in a background thread or something, since you can still press the button while it's processing the functions.

  • Could you give an example of what your function looks like?

    As far as I know a function is a triggered event that will be handled in the tick it is called, unless you use wait actions inside the function itself.

  • Here's an example of what I mean. If you click on the right-hand box, it should change colour, then you'll see the left-hand box pause while it's processing the loop on line 3 (the timer has been added otherwise you don't even see the frame-change). The problem is, if you click really quickly, it queues up more presses. Adding 'Sprite Animation frame = 0' the line 1 conditions doesn't stop this.

  • Ok, so what you're saying is, if the function is very slow (e.g 1 second) the entire game should lock up for 1 second and nothing else should be processed? That's fine and would make my life easier, but then I don't understand why it's queuing up extra presses of the mouse.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • First of all I don't see any functions, just a loop for 2.000.0000

    this means you are processing 2 million events in one tick.

    if you set the loop to start the next tick (by adding a wait 0 command) it works. ... .capx?dl=0

    It's not saving up extra presses as far as I can see..

    In your example there is a timer function which is restarted every time you click..

  • Yes, I'm aware that it's not actually a function (it is in my main program). I'm just using the word generically (and wrongly, sorry). What I mean is, a block of code which takes a long time to complete. The loop here is just to demonstrate. Are you saying that C2 will literally do nothing else while those 2 million events are processed? If so, I don't see why I can double-click on the sprite.

    I need to update so I'll check your example.

  • Yes, I'm struggling to demonstrate this properly in an example. The reason for the timer is that without it, what it does is change to the next frame then in the same tick, start the loop, meaning that you don't see the frame change (because presumably everything updates at the end of the tick?). So it changes frame then immediately pauses before showing the change, then changes back (all in the same tick, so nothing happens).

    It's hard to see the extra presses, it's not a very good example. If you really hammer at the mouse, you may see it flicker and restart another loop.

  • It's the same in your example. It might be hard to notice, you need to double-click the box and you may see it pause, then change back, then pause again. Try it a few times. Your PC might be faster than mine so it might be unnoticeable.

    I'm trying to work out a good way of demonstrating. But basically, stuff is still being processed while it's in that loop (mouse clicks at least).

  • In theory the program should indeed freeze while the event is repeated 2 Million times.

    I must admit it seems the mouse-clicks are being registered while the eventsheet is executing and as such used the next time the eventsheet is read (which is actually kinda logical when you think of it, when would mouse-clicks be registered otherwise?).

    I'm not sure why anyone would need a loop repeating 2 million times, though..

  • Like this it works (the wait introduces a wait time of 1 frame after freeze until the touch gets unlocked again, means clicks during the freeze will have had no effect)

    I'd still suggest writing the code in a way that doesn't freeze up your game.

  • That seems to work mindfaQ, I'll try implementing that into my game.

    Littlestain, bear in mind that the 2 million loop is only to demonstrate the problem.

    I don't particularly mind locking up the game during the turn process, though I agree that it's not ideal and it's poor programming. What I was trying to do was to have a message to tell the player that it's processing and disable any buttons so that the player can't make alterations during this time. Whether it's frozen or unfrozen during that time is no big deal as far as the game is concerned. But that wasn't working the way I was doing it, though it looks like the correct use of 'wait' will fix it.

    I'm not actually sure how to stop my processing from freezing up the game, on my phone at least. It's got a lot to do and I don't want the player interfering during that time anyway.

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