Yep, scripting games would be nice fit for corouti...
# coroutines
o
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):
Copy code
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
K 1
o
This is amazing :D