Knowing when a function has finished.

Get help using Construct 2

Post » Sat May 09, 2015 8:17 pm

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?
B
25
S
5
G
3
Posts: 446
Reputation: 5,633

Post » Sat May 09, 2015 8:36 pm

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.
ImageImageImage
B
71
S
21
G
220
Posts: 3,648
Reputation: 123,725

Post » Sat May 09, 2015 9:55 pm

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.
B
25
S
5
G
3
Posts: 446
Reputation: 5,633

Post » Sat May 09, 2015 10:13 pm

What are your functions doing? Each will return "immediately". If you have Waits, etc, the function will still return immediately. Waits do not block.
ImageImageImage
B
71
S
21
G
220
Posts: 3,648
Reputation: 123,725

Post » Sat May 09, 2015 11:31 pm

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.
B
25
S
5
G
3
Posts: 446
Reputation: 5,633

Post » Sat May 09, 2015 11:57 pm

Try dropbox.
ImageImageImage
B
71
S
21
G
220
Posts: 3,648
Reputation: 123,725

Post » Sun May 10, 2015 12:00 am

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.
ImageImageImage
B
71
S
21
G
220
Posts: 3,648
Reputation: 123,725

Post » Sun May 10, 2015 12:23 am

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
B
25
S
5
G
3
Posts: 446
Reputation: 5,633

Post » Sun May 10, 2015 1:03 am

www.blackhornettechnologies.com/Constru ... c_BHT.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.
ImageImageImage
B
71
S
21
G
220
Posts: 3,648
Reputation: 123,725

Post » Sun May 10, 2015 1:16 am

Ah thanks, this looks like what I was looking for. This should work for my game, with a few alterations, thanks for your time :)
B
25
S
5
G
3
Posts: 446
Reputation: 5,633

Next

Return to How do I....?

Who is online

Users browsing this forum: zsoldiers and 7 guests