Turn based battle system - ( wait - sleep - async )for action?

0 favourites
  • 3 posts
From the Asset Store
A well commented RPG game template to learn from or use as a base for your own game!
  • Hello,

    i would like to create a turn based battle system. I already created one with Events, but there i use this events with the "wait" events:

    How can i accomblish this with js? The js function "Sleep()" is not working. Ashley said that you could use this function to wait until something happens next:

    setTimeout(() => {
    			console.log("Code to run after 1 second");
    		}, 1000);
    

    But when i want to chain some waits, i need to nest "setTimeout()s"? Is there a better solution, or an example of a turn based battle function?

    Thank you!

  • Ok, i found a solution using async functions. Now you can use "mySleep(time in ms)" in every async function in your project. The only addition is, that you need a check, that the async function is only called once (see global variable: "g_active"):

    // global variable, could also be stored in an object
    let g_active = true;
    
    function Tick(runtime)
    {
     if(g_active) demoFunction();
    }
    
    function mySleep(ms) {
     return new Promise(resolve => setTimeout(resolve, ms));
    }
    
    async function demoFunction() {
     g_active = false;
     console.log('start');
     await mySleep(2000);
     console.log('two seconds later');
     await mySleep(2000);
     console.log('four seconds later');
     await mySleep(2000);
     g_active = true;
    }
    

    What do you think?

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • It would be great if it was available!

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