Skip to content

Instantly share code, notes, and snippets.

@octacian
Created September 3, 2017 14:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save octacian/5d2a6f6535038bfa9873fe9ccf3dcd4a to your computer and use it in GitHub Desktop.
Save octacian/5d2a6f6535038bfa9873fe9ccf3dcd4a to your computer and use it in GitHub Desktop.
Run a function for every node adjacent to the position provided.
local function for_adjacent_nodes(pos, func, ...)
-- Check that the required parameters (base position and function to execute) are valit
if type(pos) == "table" and func then
-- Define adjacent node positions
local nodes = {
{x=pos.x + 1, y=pos.y, z=pos.z},
{x=pos.x - 1, y=pos.y, z=pos.z},
{x=pos.x, y=pos.y + 1, z=pos.z},
{x=pos.x, y=pos.y - 1, z=pos.z},
{x=pos.x, y=pos.y, z=pos.z + 1},
{x=pos.x, y=pos.y, z=pos.z - 1},
}
-- Loop through positions
for _, p in pairs(nodes) do
-- Execute "func" with any optional arguments provided to for_adjacent_nodes
func(...)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment