Skip to content

Commit

Permalink
Add timer() function/event (node timer based) to luacontroller
Browse files Browse the repository at this point in the history
This adds a timer(<seconds>) function, which causes an event of type
"timer" to be fired after that many seconds has elapsed.

Because it's node timer based, it works properly across server restarts
and block unloading. Thus, simplest example, a blinky plant replacement
with a 10 second period:

if event.type == "program" then
  timer(10)
elseif event.type == "timer" then
  port.a = not port.a
  timer(10)
end
  • Loading branch information
CiaranG committed Mar 11, 2014
1 parent a59f53d commit 9eda62d
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions mesecons_luacontroller/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -219,13 +219,19 @@ local getinterrupt = function(pos)
return interrupt
end

local getdigiline_send = function (pos)
local digiline_send = function (channel, msg)
if digiline then
digiline:receptor_send(pos, digiline.rules.default, channel, msg)
end
local handle_timer = function(pos, elapsed)
local err = lc_update(pos, {type="timer"})
if err then print(err) end
return false
end

local gettimer = function(pos)
local timer = function (time)
if type(time) ~= "number" then return end
local nodetimer = minetest.get_node_timer(pos)
nodetimer:start(time)
end
return digiline_send
return timer
end

local create_environment = function(pos, mem, event)
Expand All @@ -239,7 +245,11 @@ local create_environment = function(pos, mem, event)
pin = merge_portstates(vports, rports),
port = vports,
interrupt = getinterrupt(pos),
digiline_send = getdigiline_send(pos),
timer = gettimer(pos),
digiline_msgs = {},
digiline_send = function(channel, msg)
table.insert(lc_digiline_msgs, {["channel"]=channel, ["msg"]=msg})
end,
mem = mem,
tostring = tostring,
tonumber = tonumber,
Expand Down Expand Up @@ -527,6 +537,7 @@ minetest.register_node(nodename, {
if err then print(err) end
reset_meta(pos, fields.code, err)
end,
on_timer = handle_timer,
sounds = default.node_sound_stone_defaults(),
mesecons = mesecons,
digiline = digiline,
Expand Down

0 comments on commit 9eda62d

Please sign in to comment.