Created
September 3, 2017 14:37
-
-
Save octacian/5d2a6f6535038bfa9873fe9ccf3dcd4a to your computer and use it in GitHub Desktop.
Run a function for every node adjacent to the position provided.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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