Title
o

orangy

01/31/2017, 8:04 PM
Yep, scripting games would be nice fit for coroutines, e.g. quest scenario, non-path movements of monsters/NPC, etc. But in non-scriptable engines (like libgdx) it’s even more useful. E.g. consider a platform moving left and right. You will normally have to encode state machine by hand. With coroutines you can do it in a simple loop (pseudocode):
class Platform(left: Float, right: Float, vertical : Float, speed : Float) : Actor() {
…
fun suspend behaviour() {
    while (true) {
      for (x in left..right step speed) {
          moveTo(x, vertical) // frame-synced suspend function
      }
      for (x in right downTo left step speed) {
          moveTo(x, vertical)
      }
   }
}
👍 4
:kotlin: 1
o

okkero

01/31/2017, 8:58 PM
This is amazing :D