I tried to code a node, when a player walks on it, it activates. When the player leaves the node, the action is performed.
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
- Code: Select all
minetest.register_abm(
{nodenames = {"maze:closer"},
interval = 1,
chance = 1,
action = function(pos, node, active_object_count, active_object_count_wider)
local objs = minetest.env:get_objects_inside_radius(pos, 3)
for k, obj in pairs(objs) do
local objpos = obj:getpos()
local dist = math.sqrt( ((pos.x - objpos.x) * (pos.x - objpos.x)) + ((pos.y - (objpos.y - 0.5)) * (pos.y - (objpos.y - 0.5))) + ((pos.z - objpos.z) * (pos.z - objpos.z)) )
print (dist.." for "..minetest.pos_to_string(pos).." to player "..minetest.pos_to_string(objpos))
local meta = minetest.env:get_meta(pos)
if dist < 0.7 then -- player walked on node
meta:set_string("trap", "triggered")
elseif dist > 2.1 then -- at least one node away
if meta:get_string("trap") == "triggered" then
meta:set_string("trap", "")
minetest.env:add_node(pos,{name="default:cobble"})
minetest.env:add_node({x = pos.x, y = pos.y + 1, z = pos.z},{name="default:cobble"})
minetest.env:add_node({x = pos.x, y = pos.y + 2, z = pos.z},{name="default:cobble"})
end
end
end
end,
})
What it does:
A player walks over a node (distance < 0.7 -- the diagonal from middle of node to edge) then a certain string is written in the meta-data of the node.
When a player is near that node (distance > 2.1) and the certain string is in the meta-data, some action is performed.
Problem:
The abm isn't called often enough. When the player "runs" over the node, the distance is mostly bigger than 1. When walking "slowly" everythings fine. But I want the node to be triggered in any case.
If I increase the trigger-distance, the node is triggered, even when the player isn't walking over the node but passes it.
Any ideas?