DSLua Community
Welcome, Guest. Please login or register.
February 05, 2012, 08:35:20 AM
1371 Posts in 262 Topics by 33109 Members
Latest Member: Limewire Download
DSLua Community  |  DSLua - Best scripting language for Nintendo DS  |  Examples/Tutorials  |  Coroutines in Lua « previous next »
Pages: [1]
Author Topic: Coroutines in Lua  (Read 3406 times)
Zhen
Waxing Crescent
**
Posts: 45


WWW
« on: May 19, 2006, 10:54:39 PM »

Hello
I was playing with coroutines in Lua, And using it to implement cooperative multitasking. I got the idea from Game Programming Gems 6.

I post the code and one example so everyone can test it, and play with it too.

I think it's very funny. And coroutines is an powerful tool of Lua.

Also, I post it, because I can't think usefull applications for coroutines in a game. I think that it can be useful for animations, gameplay of sprites,..and so on. I will be pleased for every comment.

Zhen

There is more information in http://en.wikipedia.org/wiki/Coroutines

* scheduler.zip (2.14 KB - downloaded 205 times.)
« Last Edit: May 20, 2006, 08:27:42 AM by Zhen » Logged
waruwaru
Administrator
Waxing Crescent
*****
Posts: 78


« Reply #1 on: May 19, 2006, 11:47:33 PM »

Cool!  I was playing with coroutines example from Lua's online reference a little bit.  It's pretty neat.  I havn't really read up on it to see what it's capable of yet.  The usecase that I was thinking of (not sure if this will work) was one function polling wifi (which is searching for AP/wifi-clients), while another function to update the UI/getting inputs.  Kinda like when you try Download n' Play.  It would be really cool to be able to have something like that. Smiley

Waruwaru
Logged
Zhen
Waxing Crescent
**
Posts: 45


WWW
« Reply #2 on: May 20, 2006, 07:21:34 AM »

One think awesome if this system is that you can create several scheduler and asign diffente task to each. e.g: one scheduler for the polling task that you say. Other scheduler for the gameplay tasks and another for play music, animations,.. etc. And you can create a top level scheduler to exectute all other schedulers in a concurrent way.

A problem that i have now, and I was looking for is when tasks end, sometimes lua crash. You can try it changing the  ( while true do ) sentence with a ( while health >= 0 do ) sentence in the player function.

Zhen
Logged
Zhen
Waxing Crescent
**
Posts: 45


WWW
« Reply #3 on: May 20, 2006, 08:23:21 AM »

I just fixed the end problem,

with a check before execute the actual task.

Code:
function Scheduler:Execute( task )
if coroutine.status( task.co ) == "dead" then
self.tasks[ task.name ] = nil
return
end

self.active = task
--.....
Logged
daltonlaffs
First Quarter
***
Posts: 189


Freaking Insane


WWW
« Reply #4 on: May 20, 2006, 10:58:50 AM »

ooooooooooooh. Nice one. This is an interesting program. I have learned stuff now, and this is cool. Player never dies, though.  Grin
Logged

Code:
-- Bored? Read code line below.
-- Bored? Read code line above.



^^ That works, I SWEAR! ^^
Zhen
Waxing Crescent
**
Posts: 45


WWW
« Reply #5 on: May 21, 2006, 08:20:11 AM »

 Cheesy yes is true, the player remains alive at 1 HP. Well is easy to fix.

The die of entities with coroutines is a difficult task. I was thinking about have two or more task per entity. One task to execute the behaviour and another task to control the messages. One example of behaviour task can be:

Code:
-------------------------------------------------------------------------------
function MoveTo( scheduler, entity, x, y )
    local incx, incy
    if ( x - entity.x ) > 0 then
        incx = 1
    else
        incx = -1
    end

    if ( y - entity.y ) > 0 then
        incy = 1
    else
        incy = -1
    end
   
    while entity.x ~= x or entity.y ~= y do
        if entity.x ~= x then
            entity.x = entity.x + incx
        end
        if entity.y ~= y then
            entity.y = entity.y + incy
        end
        scheduler.Transfer()
    end
end

-------------------------------------------------------------------------------
function Boss()
    local fun = function( scheduler )
        local entity = {}
        entity.x = 0
        entity.y = 0
        while true do
            MoveTo( scheduler, entity, 0, 10 )
            MoveTo( scheduler, entity, 10, 10 )
            --Shot()
            MoveTo( scheduler, entity, 10, 0 )
            MoveTo( scheduler, entity, 0, 0 )
            scheduler:Sleep( 20 )
        end
    end

    return fun
end

The MoveTo function is a generic one, all entities can use it. We can have generic functions like PlayAnimation, MoveTo, gfx effects..etc. The main function of the 'boss' only use the generic functions.

Zhen.
Logged
Pages: [1]
« previous next »
    Jump to: