How do I use Await in scripts

0 favourites
  • 14 posts
  • I am calling a function that is in a script file using a script. That all works.

    The function is an async function.

    My question is this:

    Does anyone have example of how to make sure that function call is completed before executing particular code? I've always just used the built in AJAX on completed event, but obviously I can't do that here but looking for the same fucntionality.

    Thank you,

  • Not sure if the await keyword is exactly what you want. Could you perhaps illustrate a bit more how you intend to use said function ?

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Not sure if the await keyword is exactly what you want. Could you perhaps illustrate a bit more how you intend to use said function ?

    I'm calling this function in a script

    // Example POST method implementation:
    async function postData(url = '', data = {}) {
     // Default options are marked with *
     const response = await fetch(url, {
     method: 'POST', // *GET, POST, PUT, DELETE, etc.
     mode: 'cors', // no-cors, *cors, same-origin
     cache: 'no-cache', // *default, no-cache, reload, force-cache, only-if-cached
     credentials: 'same-origin', // include, *same-origin, omit
     headers: {
     'Content-Type': 'application/json'
     // 'Content-Type': 'application/x-www-form-urlencoded',
     },
     redirect: 'follow', // manual, *follow, error
     referrerPolicy: 'no-referrer', // no-referrer, *no-referrer-when-downgrade, origin, origin-when-cross-origin, same-origin, strict-origin, strict-origin-when-cross-origin, unsafe-url
     body: JSON.stringify(data) // body data type must match "Content-Type" header
     });
     return response.json(); // parses JSON response into native JavaScript objects
    }
    

    However, I have code I want to execute after I know that has completed.

    The same as using AJAX to POST and tagging it. Then using that tag to check if it completed with the Ajax on completed action.

  • I have a tutorial that does this:

    construct.net/en/tutorials/add-playfab-cloud-storage-2479

    Briefly, you add await to the call, and then a 'Wait for previous actions to complete' as the next event.

  • I have a tutorial that does this:

    https://www.construct.net/en/tutorials/add-playfab-cloud-storage-2479

    Briefly, you add await to the call, and then a 'Wait for previous actions to complete' as the next event.

    You the man blackhornet

    Any tutorials on how to import a script and use function from it?

    I'm importing moment.js to display some unix timestamp code in a specific way.

    Anyway, thanks for the help.

  • Then you should probably simply use the .then() function.

    postData(...).then(callbacks)

    See documentation here : developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/then

  • The tutorial does that. The API file comes from PlayFab.

  • Then you should probably simply use the .then() function.

    postData(...).then(callbacks)

    See documentation here : developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/then

    I am, I guess I didn't show my call.

    postData(runtime.globalVars.apiHSURL, {"address":runtime.globalVars.address} ).then(data => {

    console.log(data); // JSON data parsed by `data.json()` call

    });

    So will using the wait for previous action to complete work in this case?

  • The tutorial does that. The API file comes from PlayFab.

    I'm struggling with how you get access to the functions in the js file. I imported my js file and when I try to access the functions they don't seem to exist at runtime. Feel like I missed a step.

  • how do you need to format the date? I use the C3 Date plugin, and a function that takes a timestamp and a format string similar to what I use in PHP... no scripting or 3rd party addons required.

    https://www.rieperts.com/games/forum/FormatDate.c3p

  • how do you need to format the date? I use the C3 Date plugin, and a function that takes a timestamp and a format string similar to what I use in PHP... no scripting or 3rd party addons required.

    https://www.rieperts.com/games/forum/FormatDate.c3p

    Using the moment library I can format into now like "A few seconds ago" or "3 days ago"

    Does the plugin do that?

  • ok, took some reading and testing, but it can be done - it just isn't as obvious as I expected.

    back in C2 I used to manually add or subtract a days worth of milliseconds (24x60x60x1000)

    but with the C3 Date plugin you can use the .ChangeDate (or .ChangeSeconds, etc...)

    the part that confused me for a bit was that you can't just say -3 for 3 days ago.

    (Today = Date.Now in following examples)

    if you say PastDate = Date.ChangeDate(Today, 0) the 0 means the last day of the previous month. So -3 means three days prior to the last day of last month, not three days ago from today.

    so, to get "3 days ago" you have to say PastDate = Date.ChangeDate(Today, Date.GetDate(Today) -3)

    or for two weeks from now you say NewDate = Date.ChangeDate(Today, Date.GetDate(Today) +14)

  • another thing that confused me in the past was the Date.GetDay is day of the week (0-6)

    Date.GetDate is the day of the current month (1-31)

    so make sure you use GetDate, not GetDay

  • another thing that confused me in the past was the Date.GetDay is day of the week (0-6)

    Date.GetDate is the day of the current month (1-31)

    so make sure you use GetDate, not GetDay

    Got it working thanks, it's actually pretty easy. The Date plugin uses Unix Epoch Time anyway, so it was just a matter of using some of built in expression.

    Like Date.ToDateString(timeStamp)

    I ended up not going with the 3 days ago, etc. Wasn't really needed and I think an actual date works better for my needs.

    Thanks for your help.

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