Lua Coroutines in games?

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
User avatar
severedskullz
Prole
Posts: 36
Joined: Thu May 30, 2013 1:55 am

Lua Coroutines in games?

Post by severedskullz »

Hello everyone,

I am somewhat familiar with Lua's co-routines but I cant really figure out how to use them inside of my games to improve performance. Where would it be logical to use co-routines inside the games? Obviously I don't want to slap on a co-routine on each entity to update it, but then again why use a co-routine to update entities when LOVE basically already does this for me in the Update function?

Anyone have any pointers on how to use them, using them efficiently, and using them at the right times?
User avatar
Inny
Party member
Posts: 652
Joined: Fri Jan 30, 2009 3:41 am
Location: New York

Re: Lua Coroutines in games?

Post by Inny »

Coroutines would not get you efficiency if you're already coding in an evented system. What they do is get you back to coding in a threaded way.

To put it another way, when you wrote a program in the 80s, your coded started and ended in your main function, and all updating and drawing was done in the same place (which may or may not have been a good thing), so that when you, for instance, drew your titlescreen, the title started and ended in the title_screen function, and you could reason that everything that happened on the title screen was in that one place. The reason we don't do that anymore is because we tend to want more than one thing to happen at the same time, such evented systems.

Coroutines are about describing sequential events using code, rather than using data structures.
User avatar
severedskullz
Prole
Posts: 36
Joined: Thu May 30, 2013 1:55 am

Re: Lua Coroutines in games?

Post by severedskullz »

Inny wrote:Coroutines are about describing sequential events using code, rather than using data structures.
I understood everything but this. Could you elaborate please? I'm sitting in a 4 hour long Algorithm Analysis course bored out of my mind... so my brain is not all there at the moment.

EDIT: Nevermind. Misread and confused myself.
User avatar
Inny
Party member
Posts: 652
Joined: Fri Jan 30, 2009 3:41 am
Location: New York

Re: Lua Coroutines in games?

Post by Inny »

severedskullz wrote:
Inny wrote:Coroutines are about describing sequential events using code, rather than using data structures.
I understood everything but this. Could you elaborate please?
I'll elaborate for the sake of the wisdom of the ancients.

Love is event-driven. Those events are update, draw, keypressed, keyreleased, and so on (see: callbacks). These are very useful, but require that you manipulate data from them, since you can't change the path of execution in another function (like in callback) without some variable. So, lets say you're showing a cutscene, your draw function may look like this:

Code: Select all

function draw_cutscene_where_important_stuff_happens()
  if draw_kirby then kirby:draw() end
  if draw_peach then peach:draw() end
  if draw_hearts then hearts: draw() end
  if draw_dedede then dedede:draw() end
end
The evented style of updating this cutscene probably needs a clock to decide when to and when not to draw things

Code: Select all

function update_cutscene_where_important_stuff_happens(dt)
  clock = clock + dt
  draw_kirby = (clock > 1000)
  draw_peach = (clock < 4000)
  draw_hearts = (clock > 1500) and (clock < 3000)
  draw_dedede = (clock > 3000) and (clock < 4000)
end
Simple, but I've had to contrive the appearance and disappearance of the characters, and it's not clear what event happens when. In more complex scenes, it may be necessary to keep a table of events and run a clock that'll execute events in order, which can get pretty messy.

The alternate, using coroutines, would look like this

Code: Select all

cutscene_coroutine = coroutine.wrap(function()
  draw_peach = true
  wait(1000)
  draw_kirby = true
  wait(500)
  draw_hearts = true
  wait(1500)
  draw_dedede = true
  draw_hearts = false
  wait(1000)
  draw_peach = false
  draw_dedede = false
end)

function update_cutscene_where_important_stuff_happens(dt)
  cutscene_coroutine(dt)
end
The scene is clearer, since it describes in order and with code, the cutscene events that are supposed to happen.

The only missing code is the wait function, which is this simple (though it's not 100% precise).

Code: Select all

function wait(seconds)
  while seconds > 0 do
    seconds = seconds - coroutine.yield(true)
  end
end
So coroutines gets you back to having a straight thread explaining the steps in what will happen, while allowing for other things to happen as well. It's sometimes described as Cooperative Multithreading.
User avatar
miko
Party member
Posts: 410
Joined: Fri Nov 26, 2010 2:25 pm
Location: PL

Re: Lua Coroutines in games?

Post by miko »

severedskullz wrote:Anyone have any pointers on how to use them, using them efficiently, and using them at the right times?
It's not about a performance, but ease of use:
http://love2d.org/forums/viewtopic.php?f=5&t=14166
My lovely code lives at GitHub: http://github.com/miko/Love2d-samples
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest