Skip to content

Instantly share code, notes, and snippets.

@rubenwardy
Last active March 9, 2022 00:44
Show Gist options
  • Save rubenwardy/ca8b39276dd53aad5a7a1996fe0e84c9 to your computer and use it in GitHub Desktop.
Save rubenwardy/ca8b39276dd53aad5a7a1996fe0e84c9 to your computer and use it in GitHub Desktop.
-- Run a series of functions as a pipeline - each returning the next's arguments.
--
-- The first function will be ran immediately
--
-- @param interval Time between pipelines
-- @param funcs A list of functions, each one of the form function(...)
-- where ... is returned by the previous function.
-- @param params Optional, a list of arguments to pass to the first function
local function pipeline(interval, funcs, params)
if funcs[1] ~= nil then
params = table.pack(funcs[1](table.unpack(params)))
end
table.remove(funcs, 1)
if #funcs > 0 then
minetest.after(interval, pipeline, interval, funcs, params)
end
end
-- Run a series of functions as a pipeline - each returning the next's arguments.
-- Player is injected as the first argument in functions.
--
-- The first function will be ran immediately
--
-- @param name Player name
-- @param interval Time between pipelines
-- @param funcs A list of functions, each one of the form function(player, ...)
-- where ... is returned by the previous function.
-- @param params Optional, a list of arguments to pass to the first function
local function pipeline_plr(name, interval, funcs, params)
if funcs[1] ~= nil then
local player = minetest.get_player_by_name(name)
if not player then
return
end
table.insert(params, 1, player)
params = table.pack(funcs[1](table.unpack(params)))
end
table.remove(funcs, 1)
if #funcs > 0 then
minetest.after(interval, pipeline_plr, name, interval, funcs, params)
end
end
pipeline(1, {
function()
return 5.3
end,
function(x)
return 2*x
end,
math.round,
nil, -- skip a step, ie: 2xInterval is between above and below functions
function(x)
return x*x, x*x*x
end,
function(x, y)
return "X is: " .. x .. ", y is: " .. y
end,
print
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment