forked from VoxeLibre/VoxeLibre
Update vl_scheduler.register_function to require the function name to be prefixed by the mod registering, add examples of function registration
This commit is contained in:
parent
27fb96afdf
commit
a958fbbf71
|
@ -48,7 +48,16 @@ function mod.add_task(time, name, priority, args)
|
||||||
queue_add_task(priority_queue, task)
|
queue_add_task(priority_queue, task)
|
||||||
end
|
end
|
||||||
|
|
||||||
function mod.register_function(name, func)
|
function mod.register_function(name, func, default_time, default_priority)
|
||||||
|
-- Validate module in name
|
||||||
|
local modname = minetest.get_current_modname()
|
||||||
|
if string.sub(name,1,#modname+1) ~= (modname..":") and string.sub(name,1) ~= ":" then
|
||||||
|
error("Module "..modname.." is trying to register function '"..name.."' that doesn't start with either '"..modname..":' or ':'")
|
||||||
|
end
|
||||||
|
if string.sub(name,1) == ":" then
|
||||||
|
name = string.sub(name,2,#name-1)
|
||||||
|
end
|
||||||
|
|
||||||
local fid = #functions + 1
|
local fid = #functions + 1
|
||||||
functions[fid] = {
|
functions[fid] = {
|
||||||
func = func,
|
func = func,
|
||||||
|
@ -57,16 +66,39 @@ function mod.register_function(name, func)
|
||||||
}
|
}
|
||||||
function_id_from_name[name] = fid
|
function_id_from_name[name] = fid
|
||||||
print("Registering "..name.." as #"..tostring(fid))
|
print("Registering "..name.." as #"..tostring(fid))
|
||||||
|
|
||||||
|
-- Provide a function to easily schedule tasks
|
||||||
|
local dtime = math.floor((default_time or 0) * 20)+1
|
||||||
|
if not default_priority then default_priority = 3 end
|
||||||
|
return function(...)
|
||||||
|
local task = {
|
||||||
|
time = dtime,
|
||||||
|
dtime = dtime,
|
||||||
|
fid = fid,
|
||||||
|
priority = default_priority,
|
||||||
|
args = ...
|
||||||
|
}
|
||||||
|
queue_add_task(priority_queue, task)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
mod.register_function("vl_scheduler:test",function(task)
|
-- Examples of scheduler task:
|
||||||
print("game time="..tostring(minetest.get_gametime()))
|
if false then
|
||||||
|
-- Register a task that runs ever 0.25 seconds
|
||||||
|
mod.register_function("vl_scheduler:test",function(task)
|
||||||
|
print("game time="..tostring(minetest.get_gametime()))
|
||||||
|
|
||||||
-- Reschedule task
|
-- Reschedule task
|
||||||
task.time = 0.25 * 20
|
task.time = 0.25
|
||||||
return task
|
return task
|
||||||
end)
|
end)()
|
||||||
mod.add_task(0, "vl_scheduler:test")
|
|
||||||
|
-- Register a function that runs an action at a fixed time in the future
|
||||||
|
local act = mod.register_function("vl_scheduler:act",function(task,arg1)
|
||||||
|
print(dump(arg1))
|
||||||
|
end, 0.15, 1)
|
||||||
|
act("test")
|
||||||
|
end
|
||||||
|
|
||||||
minetest.register_globalstep(function(dtime)
|
minetest.register_globalstep(function(dtime)
|
||||||
local start_time = minetest_get_us_time()
|
local start_time = minetest_get_us_time()
|
||||||
|
@ -104,6 +136,7 @@ minetest.register_globalstep(function(dtime)
|
||||||
|
|
||||||
-- If the task was returned, reschedule it
|
-- If the task was returned, reschedule it
|
||||||
if ret == task then
|
if ret == task then
|
||||||
|
task.time = math.floor(task.time * 20) + 1
|
||||||
task.next = nil
|
task.next = nil
|
||||||
queue_add_task(priority_queue, task)
|
queue_add_task(priority_queue, task)
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue