Code Monkey home page Code Monkey logo

defunico's Introduction

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)

defunico's People

Contributors

u16kuma avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

defunico's Issues

Incorrect removing coroutine instance in method on_message_coroutine

`
function self.on_message_coroutine(message_id, message, sender)
self.message_pack = { message_id = message_id, message = message, sender = sender }
-- for i, co_ins in ipairs(self.unico_list) do
-- if co_ins:message_move_next() == "dead" then
-- -- remove unico
-- table.remove(self.unico_list, i)
-- end
-- end

	for i = #self.unico_list, 1, -1 do
		if self.unico_list[i]:message_move_next() == "dead" then
			-- remove unico
			table.remove(self.unico_list, i)
		end
	end
end

`

I comment the old code.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.