u16kuma / defunico

DefUniCo is a coroutine library for Defold game engine.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

DefUniCo

DefUniCo is a coroutine library for Defold game engine.
The library is useful when you want to write coroutine like Unity3D in Defold.

Installation

You can use DefUniCo in your own project by adding this project as a Defold library dependency.
Open your game.project file and in the dependencies field under project add:

https://github.com/u16kuma/defunico/archive/master.zip

Or point to the ZIP file of a specific DefUniCo release.

Usage

DefUniCo is very easy to use.
All you need to do is to write template code.

test.script

local defunico = require("defunico.defunico")
function init(self)
    defunico.init(self)
end
function update(self, dt)
    self.update_coroutine(dt)
end

Let's try write coroutine!

function init(self)
    defunico.init(self)

    -- Please write coroutine code under the defunico.init
    self.start_coroutine(function(self)
        -- wait one second
        wait_seconds(1)

        print("One second has passed.")
    end)
end

Coroutines can be stopped by adding this code.

local co = self.start_coroutine(function() ... end)
self.stop_coroutine(co)

Coroutine wait some wait_* function.

self.start_coroutine(function(self)

    -- wait one second
    wait_seconds(1)

    -- wait one second
    wait_msec(1000)
    
    -- wait one second (60 fps)
    wait_frame(60)

    -- wait one second (60 fps)
    local frame = 0
    wait_until(function() 
        frame = frame + 1
        return frame >= 60
    end)

    -- wait one second (60 fps)
    frame = 0
    wait_while(function()
        frame = frame + 1
        return frame <= 60
    end)

    -- need to write self.on_input_coroutine to on_input
    local input_pack = self.wait_until_input(function(action_id, action)
        return action_id == hash("touch")
    end)

    -- need to write self.on_message_coroutine to on_message
    local msg_pack = self.wait_until_message(function(message_id, message, sender)
        return message_id == hash("update")
    end)

end)

About

DefUniCo is a coroutine library for Defold game engine.

License:MIT License


Languages

Language:Lua 100.0%