How do i get promises on built-in functions for scripting

  • basically I'm calling a construct function in a script with runtime.callfunction()

    I need the actions of that function to finish before executing the rest of the script

    some actions in that function are asynchronous and have the little timer logo thing

  • Use a "Wait for previous action to complete"

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • You could try using 'await runtime.callfunction()' (I hope that runtime.callfunction() was made to support async behavior.)

  • it was not

  • Is that just in a scripting block in the event sheet? Can you give more context? When I try it in a scripting block, I don't get that parsing error marking. Ashley has noted in the past that script blocks are default inside async functions, so you can use await in event scripting.

    Ah, if you are doing it in just a separate C3 JS script file, you need to make the function calling the below function async also, for example:

    async function readJSONFile() {

    const jsonData = await runtime.callFunction("readJsonFile")

    console.log('jsonData:', jsonData)

    }

  • I put this, then a "wait for previous actions" action,

    than nother code block, and the second code block would finish before the first one, triggering a racing condition sometimes just bugging out my whole thing

    that'S why im trying to understand uh, promises or await or something

  • I think this will not happen if you use sub events for the following code blocks. At least I vaguely remember that I had this similar problem. But I'm not sure.

  • I saw this a while ago and thought that it wasn't supported, but it turns out it is. It's just not immediately obvious.

    Here is a small project showing how to do it.

    dropbox.com/s/rpjdia0aj3y87o7/AwaitCallFunction.c3p

    The project has 4 buttons showing what is the execution flow when using different combinations of asynchronous functions, await and wait for previous actions to complete.

    For this example I use a function which only uses the Wait action, just to demonstrate that something is being waited for.

    Button 1

    The first button will do all the things. The script block uses

    await runtime.callFunction
    

    It works because the Asynchronous checkbox was ticked.

    You can see there is a short wait between the text changing. An alert is shown afterwards because there is a wait for previous actions to complete after the script block, which prevents execution of the event sheet from continuing before any previous asynchronous work is complete.

    Button 2

    The second button does the same as the first, but it doesn't use wait for previous actions to complete. So the alert is shown immediately after the script block. After closing the alert box, the short wait between the text changing is still present though, this is because

    await runtime.callFunction
    

    is still stopping execution in the scope of the script block.

    Button 3

    This one just shows what happens when you don't wait for anything. The alert is shown instantly and the text doesn't wait to change. This one is just to show what you don't want to do when working with asynchronous functions.

    Button 4

    This is the same as the first, but calls a function declared in a script.

    eleanorjmorel

    I think that covers everything. Not really sure what you need to do, hopefully one of those is what you are looking for.

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