Skip to content

Commit 9eda62d

Browse files
committedMar 11, 2014
Add timer() function/event (node timer based) to luacontroller
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
1 parent a59f53d commit 9eda62d

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed
 

‎mesecons_luacontroller/init.lua

+18-7
Original file line numberDiff line numberDiff line change
@@ -219,13 +219,19 @@ local getinterrupt = function(pos)
219219
return interrupt
220220
end
221221

222-
local getdigiline_send = function (pos)
223-
local digiline_send = function (channel, msg)
224-
if digiline then
225-
digiline:receptor_send(pos, digiline.rules.default, channel, msg)
226-
end
222+
local handle_timer = function(pos, elapsed)
223+
local err = lc_update(pos, {type="timer"})
224+
if err then print(err) end
225+
return false
226+
end
227+
228+
local gettimer = function(pos)
229+
local timer = function (time)
230+
if type(time) ~= "number" then return end
231+
local nodetimer = minetest.get_node_timer(pos)
232+
nodetimer:start(time)
227233
end
228-
return digiline_send
234+
return timer
229235
end
230236

231237
local create_environment = function(pos, mem, event)
@@ -239,7 +245,11 @@ local create_environment = function(pos, mem, event)
239245
pin = merge_portstates(vports, rports),
240246
port = vports,
241247
interrupt = getinterrupt(pos),
242-
digiline_send = getdigiline_send(pos),
248+
timer = gettimer(pos),
249+
digiline_msgs = {},
250+
digiline_send = function(channel, msg)
251+
table.insert(lc_digiline_msgs, {["channel"]=channel, ["msg"]=msg})
252+
end,
243253
mem = mem,
244254
tostring = tostring,
245255
tonumber = tonumber,
@@ -527,6 +537,7 @@ minetest.register_node(nodename, {
527537
if err then print(err) end
528538
reset_meta(pos, fields.code, err)
529539
end,
540+
on_timer = handle_timer,
530541
sounds = default.node_sound_stone_defaults(),
531542
mesecons = mesecons,
532543
digiline = digiline,

0 commit comments

Comments
 (0)
Please sign in to comment.