Implementation of Wait

0 favourites
  • 6 posts
  • Just curious how the Wait system action is implemented. I want to do something similar in a C game engine cause it's so useful. Does it use the browser API setTimeout? ES6 coroutines? Or is it implemented in some other way.

  • It probably works like this, at least that’s how I rely on it working.

    Update loop

    — run event sheet

    — run wait events that are done

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • If I have a wait 0 after a wait 0 does it get run after the next tic runs the standard events, or does it get done after this tic's wait 0 stuff? I assume it is next tic.

  • Not sure. We could test that with something like this:

    Star of layout
    — set text to tickcount
    — wait 0 seconds
    — set text to self.text&tickcount
    — wait 0 seconds
    — set text to self.text&tickcount

    Depending on how wait is implemented we could get 000 or 001.

    Edit: I’m inclined to think it would be 000, aka all wait 0 after a wait will be run. The logic may be similar to the following. If I wanted it to delay to next frame it would be more complex I’d imagine. We could always look at the c2 source to see how wait is implemented too.

     waitlist = []
    time = 0
    
    function wait(t){
     waitlist.append({when:time+t, events:followingEvents})
    }
    
    while(gameRunning){
     time += dt
     runEvents()
     for(i=0; i<waitlist.length; ++i){
     if(waitlist[i].when<=time){
     run(waitlist[i].events)
     waitlist.remove(i)
     i-=1
     }
     }
    }
  • winkr7

    Turns out you were right, it runs the next tick. Guess looping backwards over the list works to do that.

    waitlist = []
    time = 0
    
    function wait(t){
     waitlist.append({when:time+t, events:followingEvents})
    }
    
    while(gameRunning){
     time += dt
     run(eventsheet)
     for(i=waitlist.length-1; i>0; --i){
     if(waitlist[i].when<=time){
     run(waitlist[i].events)
     waitlist.remove(i)
     }
     }
    }
  • Thanks ROJOHound

    You are very thorough.

    yours

    winkr7

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