Skip to content

Instantly share code, notes, and snippets.

@Lerg
Last active December 2, 2022 16:43
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Lerg/888c7df3844ab38cc39ef81d1c786da4 to your computer and use it in GitHub Desktop.
Save Lerg/888c7df3844ab38cc39ef81d1c786da4 to your computer and use it in GitHub Desktop.
Script wrapper for Defold
local hashed = require('libs.hashed')
local _M = {}
local scripts = {}
-- NoMoreGlobalFunctionsInScripts
function _M.new()
local script = {
is_script = true,
messages = {}
}
return script
end
--[[init(self)
final(self)
update(self, dt)
on_message(self, message_id, message, sender)
on_input(self, action_id, action)
on_reload(self)]]
local defold_methods = {'update', 'on_input', 'on_reload'}
function _M.register(script)
_G['init'] = function(self, ...)
local s = {}
for k, v in pairs(script) do
s[k] = v
end
scripts[self] = s
s.instance = self
if s.init then
s:init(...)
end
end
_G['final'] = function(self, ...)
local s = scripts[self]
if s.final then
s:final(...)
end
scripts[self] = nil
end
for i = 1, #defold_methods do
local method = defold_methods[i]
if script[method] then
_G[method] = function(self, ...)
local s = scripts[self]
s[method](s, ...)
end
end
end
local has_messages = false
for method, f in pairs(script) do
if type(f) == 'function' and method:len() > 8 and method:sub(1, 8) == 'message_' then
has_messages = true
local message_id = method:sub(9)
script.messages[hashed[message_id]] = f
end
end
if has_messages then
_G.on_message = function(self, message_id, message, sender)
local s = scripts[self]
local f = s.messages[message_id]
if f then
f(s, message, sender)
return true
end
end
end
end
return _M
@Lerg
Copy link
Author

Lerg commented Nov 7, 2019

Use it like this.

local hashed = require('libs.hashed')
local n28s = require('libs.n28s')

local script = n28s.new()

function script:init()
	print(self.instance)
end

function script:final()
	-- Free resources
	self.anything = nil
end

function script:update(dt)
end

function script:message_some(params)
	-- Automatic message handler.
end

function script:message_other(params)
	
end

n28s.register(script)

@naazeri
Copy link

naazeri commented Nov 3, 2022

thank you ❤

link of hashed library:
https://github.com/Lerg/defold-hashed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment