lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


recently, I spent quite a lot of time studying various internet resources on coroutines, continuations, and other related topics. There's one thing that many authors hinted upon repeatedly, but nobody cared enough to give any particulars. They unanimously wrote that Lua coroutines are often used to implement the behavior of character in computer games.

I have quite a hard time imagining a concrete implementation. Is somebody on this list familiar with this topic enough to give a few pointers, book references, or an example maybe?

Thanks a lot,

- Peter

[In case I'm not clear enough, here's how I imagine the problem. Let's have an animation of a rabbit running towards a carrot. A primitive implementation (in pseudocode) would be like the following:

y = 100; // rabbit's y-coordinate, some constant value
for x = start_position to carrot_position {
  draw_rabbit(x, y);

A better implementation would use a timer:

function update_scene(current_time) {
  draw_rabbit(current_time * speed, y);

setTimer(update_scene, 100ms, timer::repeating);

How would one rewrite these examples with coroutines? I mean, if I replace the sleep() in the former example with a yield, how would I get correct timing? Or is this example completely inadequate?]