Rather than typing code over and over and making longer files, wasting more disk space, and increasing your chances of making mistakes, use functions which let you create a myriad of similar functions but still let you change things up by passing different variables.

Desert_life mod

Example Code:
Function:
function new_mod.wonderful_replace(nodename, pos, undernode)
    local under_pos = ({x=pos.x, y=pos.y-1, z=pos.z})
    local under_name = minetest.get_node_or_nil(under_pos)
    if under_name.name == undernode then
        minetest.set_node(under_pos, {name = nodename})
    end
end
ABMs:
minetest.register_abm({
    nodenames = {'default:rail'},
    interval = 5,
    chance = 5,
    action = function(pos)
        print 'running diamondblock abm'
        new_mod.wonderful_replace('default:diamondblock', pos, 'default:dirt_with_grass')
    end
})

minetest.register_abm({
    nodenames = {'group:fence'},
    interval = 5,
    chance = 5,
    action = function(pos)
        local node = minetest.get_node(pos)
        if node.name == 'default:fence_pine_wood' then
            new_mod.wonderful_replace('default:bronzeblock', pos, 'default:mossycobble')
        end
        if node.name == 'default:fence_wood' then
            new_mod.wonderful_replace('default:meselamp', pos, 'default:diamondblock')
        end
    end
})

Read More:
Lua Functions

Add new comment