I think I'm having timing issues in my event flow

  • I've really been struggling with my code this weekend and I'm not sure why. Even fairly simple things are not working and I'm starting to suspect that I'm encountering timing issues in the event flow. Like some events are taking so long that following events are not being called at all. Has anyone else encountered this?

    For example, when players push a button in my game I call two different functions. The first checks through a giant array to look for valid words. That works great. The second function does a variety of things and it isn't working. To debug it I started disabling events until I was left with a single one that simply changes text in a visible string. Nothing happens, it's like the second function call is being skipped.

    Here's what my code basically looks like:

    [the player clicks on a button][ul]	[li]Function call "score_words" (this works great!)[/li]
    	[li]Function call "final_score" (this doesn't work at all)[/li]
    [/ul]
    Function On "score_words"
    [ul]
    	[li]For each X element (there are 13,000+ values in my array)[/li]
    [/ul]---- array_all_words Current Value = array_player.at(0) --> set score_1 to array_all_words(arr_all_words.CurX,1)
    
    Function On "final_score" ---> txt_debug Set text to "HERE!"
    [/code:2tyz0h2w]
    
    My debug message never gets updated to "HERE!" I've tried adding a Wait(0) between the two function calls but that doesn't seem to help.
    
    Any other suggestions? (Thanks!)
  • It sounds like a race condition.

    Could you call "final_score" from "score_words" after is done? Or use a (boolean) variable and when "score words= is done, set it to true. In an extra event when the variable is true call "final score". Then when that function is done set the variable to false again.

  • Thanks for being so helpful, Asmodean!

    What you proposed is basically what I wound up doing. I wasn't sure how to tell when my "For each element" loop was finished so I am simply checking to see if array_all_words Current value = "agria" (Which is the last word in my array.) Is there a better way to do that?

    Ugh. My code is now full of all sorts of crazy kludges that I didn't actually need. I wonder if there is anything Scirra could do to make it more obvious when this "race" situation is happening? It's not intuitive at ALL.

  • john Cutter yea i had an identical issue with my game when loading big arrays.

    i figured that probably because the first function dosent finish before the tick cycle the second function just gets forgotten. im not sure if right but as Asmodean said it was easily solved with a Boolean true on completion and just use that as the trigger for the debug message or the next function.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • There should not a race condition. That's not how events work. Can you isolate your code and post it? There's probably a simpler explanation.

    I can provide an example similar to to what you are doing, and all events are run in order - nothing is ever skipped.

  • blackhornet

    I tried isolating the events into a very small file and I'm still seeing the problem.

    My program:

    Here's a picture of my events sheet:

    And here's a picture of my functions sheet:

    When I click the button my first function is called but the code never reaches the second one. (The text changes to "Button Clicked" and not "Function 2".)

    NOTE: If I reverse the order and call "Change Text" first, the text changes to "Function 2".

    Here's a link to my .c3p if you want to play around with it:

    https://nofile.io/f/uhcGvKUwwuR/Race+Test.c3p

  • The problem is your json file. Did you edit it manually? The length is set to 13570. It should be 13569.

    If you run with the Console up, you'll see an exception is thrown. That's why the later code is not run - it is crashing.

  • [quote:2cl6reav]The problem is your json file. Did you edit it manually? The length is set to 13570. It should be 13569.

    If you run with the Console up, you'll see an exception is thrown. That's why the later code is not run - it is crashing.

    Yikes! Yes, I generate the .json file from an Excel spreadsheet. Looks like I need to fix my VBA code. Thanks, blackhornet!

    When you say "run with the console up" do you mean the debug console? I've never noticed any error messages...

  • The browser console.

    runtime.js:413 Uncaught TypeError: Cannot read property '0' of undefined

    at pluginProto.Instance.Cnds.Contains (runtime.js:413)

    at Condition.run_object (eveng.js:1430)

    at EventBlock.run (eveng.js:884)

    at EventBlock.run_subevents (eveng.js:1012)

    at EventBlock.run_actions_and_subevents (eveng.js:951)

    at EventBlock.run (eveng.js:904)

    at Runtime.executeSingleTrigger (preview.js:4999)

    at Runtime.triggerOnSheetForTypeName (preview.js:4918)

    at Runtime.triggerOnSheet (preview.js:4850)

    at Runtime.trigger (preview.js:4821)

    Cnds.Contains runtime.js:413

    Condition.run_object eveng.js:1430

    EventBlock.run eveng.js:884

    EventBlock.run_subevents eveng.js:1012

    EventBlock.run_actions_and_subevents eveng.js:951

    EventBlock.run eveng.js:904

    Runtime.executeSingleTrigger preview.js:4999

    Runtime.triggerOnSheetForTypeName preview.js:4918

    Runtime.triggerOnSheet preview.js:4850

    Runtime.trigger preview.js:4821

    Acts.CallFunction runtime.js:180

    Action.run_object eveng.js:1868

    EventBlock.run_actions_and_subevents eveng.js:947

    EventBlock.run eveng.js:904

    Runtime.executeSingleTrigger preview.js:4999

    Runtime.triggerOnSheetForTypeName preview.js:4918

    Runtime.triggerOnSheet preview.js:4850

    Runtime.trigger preview.js:4827

    (anonymous) runtime.js:98

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