Using await in event sheet to call external JavaScript functions

0 favourites
  • 3 posts
From the Asset Store
With this template you will learn how to use the GooglePlay Games native plugin
  • Hi, I have an external js script with some async funcstions and I want to call them in event sheets one after another and make sure each one finishes working before the next one starts. What is the best way to do so? Using await or wait for previous actions to complete? Here is my code in the external script:

    class YandexSDK {
     constructor () {
     this.ready = new Promise(resolve => this.init = resolve);
     }
     setSDK (ysdk) {
     this.ysdk = ysdk;
     } 
     showFullscreen () {
     this.ready.then(() => this.ysdk.adv.showFullscreenAdv({
    	callbacks:{ 
    	 onClose: () => {
     console.log('Ad was closed');
     }}}))
     }
     load_player () {
     this.ready.then(() => this.ysdk.getPlayer().then(_player => {
     player = _player;
    		console.log('Player Loaded '+ String(player));
    		return player;
     }).catch(err => {
     console.log('Error loading player');
     }))
     }
     
    };
    
    async function load_player_score(runtime) {
     if (player != null) {
    	loaded_stats = await player.getStats();
    	console.log("loaded stats: "+loaded_stats.score_save);
    	runtime.globalVars.score = loaded_stats.score_save
    	console.log("updated score ")
    	}}
    
    

    And here is the event sheet, now I am using await but I also tried waiting for previous action - but it doesn't work as I am expecting. Could you please advice?

    Tagged:

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Can you repeat the problem in a new project and upload the file? Or write to me on discord.

  • I realise this thread is about 3 weeks old now, but I note that the methods of the YandexSDK class do not return anything, so they cannot be awaited. Async methods need to either return a Promise, or use the async keyword. Otherwise what happens is awaiting them will complete immediately, but the work is really still continuing in the background.

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