How to detect an object?
How to detect an object?
How would one detect whether there is an object/an item on top of a node? I'm trying to make an ABM that spawns an item on top of a certain type of node, but only when there is no item currently on top of it.
Re: How to detect an object?
Use get_node() if you want to detect a node, or get_objects_inside_radius() for anything mobile.
Re: How to detect an object?
Thanks for the response. I gave this a shot, however I can't seem to get it working still.Astrobe wrote:Use get_node() if you want to detect a node, or get_objects_inside_radius() for anything mobile.
Code: Select all
minetest.register_abm({
nodenames = {"weirdworld:mud"},
neighbors = {"default:water_source", "default:water_flowing", "default:river_water_source", "default:river_water_flowing"},
interval = 5,
chance = 1, --1 in 75
action = function(pos, node, active_object_count, active_object_count_wider)
local above_node = minetest.get_node({x = pos.x, y = pos.y + 1, z = pos.z})
local above_objects = minetest.get_objects_inside_radius({x = pos.x, y = pos.y + 1, z = pos.z}, 0)
if (minetest.registered_nodes[above_node.name].buildable_to and above_objects == nil) then
minetest.add_item({x = pos.x, y = pos.y + 1, z = pos.z}, "weirdworld:worm")
else
return
end
end
})
- Krock
- Developer
- Posts: 4650
- Joined: Thu Oct 03, 2013 07:48
- GitHub: SmallJoker
- Location: Switzerland
- Contact:
Re: How to detect an object?
First consider to increase the radius to 1m to prevent from rounding errors and to ensure the object range checks are not simply skipped in the engine code. Also check against the returned amount of objects. When there are none, get_objects_inside_radius will return an empty table, not nil!num3rical wrote:Code: Select all
local above_objects = minetest.get_objects_inside_radius({x = pos.x, y = pos.y + 1, z = pos.z}, 0) if (minetest.registered_nodes[above_node.name].buildable_to and above_objects == nil) then
Code: Select all
local above_objects = minetest.get_objects_inside_radius({x = pos.x, y = pos.y + 1, z = pos.z}, 0.5)
if (minetest.registered_nodes[above_node.name].buildable_to and #above_objects == 0)
Look, I programmed a bug for you. >> Mod Search Engine << - Mods by Krock - DuckDuckGo mod search bang: !mtmod <keyword here>
Re: How to detect an object?
Works perfectly, thank you very much! Guess I need to read up a bit more on tables in Lua :PKrock wrote:First consider to increase the radius to 1m to prevent from rounding errors and to ensure the object range checks are not simply skipped in the engine code. Also check against the returned amount of objects. When there are none, get_objects_inside_radius will return an empty table, not nil!num3rical wrote:Code: Select all
local above_objects = minetest.get_objects_inside_radius({x = pos.x, y = pos.y + 1, z = pos.z}, 0) if (minetest.registered_nodes[above_node.name].buildable_to and above_objects == nil) then
Also notice that you're using the index [above_node.name], which may return a nil value when the node above is unknown (disabled/removed mod).Code: Select all
local above_objects = minetest.get_objects_inside_radius({x = pos.x, y = pos.y + 1, z = pos.z}, 0.5) if (minetest.registered_nodes[above_node.name].buildable_to and #above_objects == 0)
Who is online
Users browsing this forum: No registered users and 12 guests