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?
Lua Coroutines in games?
Re: Lua Coroutines in games?
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.
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.
- severedskullz
- Prole
- Posts: 36
- Joined: Thu May 30, 2013 1:55 am
Re: Lua Coroutines in games?
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.Inny wrote:Coroutines are about describing sequential events using code, rather than using data structures.
EDIT: Nevermind. Misread and confused myself.
Re: Lua Coroutines in games?
I'll elaborate for the sake of the wisdom of the ancients.severedskullz wrote:I understood everything but this. Could you elaborate please?Inny wrote:Coroutines are about describing sequential events using code, rather than using data structures.
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
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
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 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
Re: Lua Coroutines in games?
It's not about a performance, but ease of use:severedskullz wrote:Anyone have any pointers on how to use them, using them efficiently, and using them at the right times?
http://love2d.org/forums/viewtopic.php?f=5&t=14166
My lovely code lives at GitHub: http://github.com/miko/Love2d-samples
Who is online
Users browsing this forum: No registered users and 3 guests