Loops aren't threaded but functions are???

0 favourites
From the Asset Store
40 ambient sound loops. A wide selection of looping ambient sounds for different locations.

    Ok, this is seriously confusing. In the startup of my little project I call a function DoStuff. At the end of that function I set the return value to 1. Inside that function I call another function DoLotsofStuff without a return value under the stupid assumption that THAT function will complete, whether it has a return value or not, before it completes DoStuff and returns the 1. Instead, what I'm seeing is that DoMoreStuff fires because the DoStuff returns a 1 long before DoLotsofStuff is done.

    + System: On start of layout
    ----+ System: Functions.DoStuff = 1
    -------> DoMoreStuff
    
    * On function 'DoStuff'
    -> Functions: Call DoLotsofStuff 
    -> Functions: Set return value 1
    

    I'll mention this again. I've been writing code a long time. I have never, ever called a function in any language and have the code skip over it before it finishes even without a return value. But it appears Construct is doing just that. Am I going to have to assign some pointless return value to every function just to force it to completed before the code goes to the next event????

    And I apologize in advance. I've had an extremely aggravating morning writing code. It's taken me 3 hours to figure out why my code was not working as intended and this is the reason why.

    Please tell me this is a bug and I should fill out a bug report.

    And yea, I realize this isn't true threading but it's doing a fine job of faking it.

    Ok, I have no clue what's going on. I just did a test.

    + System: On start of layout
    ----+ System: Functions.Function1 = 1
    -----> Text: Append newline&"Function 1 returned"
    
    * On function 'Function1'
    -> Functions: Call Function2
    -> Functions: Set return value 1
    
    * On function 'Function2'
    ----+ System: For "" from 1 to 50000000
    -----> (no actions)
    
    ----+ System: 0 is Number
    -----> Text: Append newline&"Function 2 finished"
    

    And this works as intended. Function 2 completes before function 1. So I have no idea now why my code, which is essentially the same but a lot more complex, is not doing this.

    I suspect the second it being passed into the first as a callback or being executed with a promise. In either case, the execution will be added to the end of the execution queue, so will be executed later.

    I suspect the second it being passed into the first as a callback or being executed with a promise. In either case, the execution will be added to the end of the execution queue, so will be executed later.

    I suspect the second it being passed into the first as a callback

    I take this bit back.

    Oh, I figured it out and this is even crazier than I thought.

    + System: On start of layout
    ----+ System: Functions.Function1 = 1
    -----> Text: Append newline&"Function1 complete"
    
    * On function 'Function1'
    -> Functions: Call Function2
    -> Functions: Set return value 1
    
    * On function 'Function2'
    -> System: Wait 0 seconds
    ----+ System: For "" from 1 to 5000000
    -----> (no actions)
    
    ----+ System: 0 is Number
    -----> Text: Append newline&"Function2 complete"
    

    Do you see the difference? At the start of function2, I added a wait 0. With the wait 0 enabled, function 1 completes first. With it disabled, function 2 completes first.

    Wow, just.... wow. So, the method that was described to me earlier on one way to update say a progress bar in the middle of a loop by inserting a wait, completely breaks the chain functions should follow.

    Are you a JavaScript programmer usually?

    The strange behavior is actually correct, documented behavior. The wait will be compiled as a setTimeout(0), which will not make any minimum wait time but will put it to the back of the execution queue.

    Are you a JavaScript programmer usually?

    Nope, several other languages but not JS.

    Ok, so, setting the wait to 0.1 does solve the problem but. Having wait 0 and wait 0.1 do two different things???

    Where did this JS language come from any way?

    Hello;

    Sorry this is a little off topic--but how do you get your code snippets to print out so neatly. I always have to take a screen shot. Are you using cut and paste to generate those?

    thanks, and I hope you solve your wait problem.

    yours

    winkr7

    winkr7

    Wrap it in a code tag.

    As a rule of thumb, I steer clear of "Wait 0". Rarely do I really want to postpone actions back to the end of a tick.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads

    Hello;

    Sorry this is a little off topic--but how do you get your code snippets to print out so neatly. I always have to take a screen shot. Are you using cut and paste to generate those?

    thanks, and I hope you solve your wait problem.

    yours

    winkr7

    As a rule of thumb, I steer clear of "Wait 0". Rarely do I really want to postpone actions back to the end of a tick.

    Yea, I see that now. Here's the problem. The list of things I'm learning to avoid (loops, while, etc.) is growing at an exponential rate. By the time my subscription comes around for renewal, I'll be coding on paper with a pencil.

    But why would you avoid loops ? They are one of the fundamental building blocks of coding !

    What if loops were an action rather than in the conditionals, and they also qualified as asynchronous so you could use "Wait for previous actions to complete"?

    Well actually I guess everything does wait until they complete. lol

    As a rule of thumb, I steer clear of "Wait 0". Rarely do I really want to postpone actions back to the end of a tick.

    I use "Wait 0" all the time. Mostly after I create objects and need to change something about them (call a function that re-arranges them for example). It's also sometimes useful with mouse/touch events, drag&drop and other behaviors.

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