How to detect an object?

Post Reply
num3rical
New member
Posts: 3
Joined: Fri Jun 15, 2018 13:58
IRC: num3rical
In-game: num3rical

How to detect an object?

by num3rical » Post

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.

Astrobe
Member
Posts: 577
Joined: Sun Apr 01, 2018 10:46

Re: How to detect an object?

by Astrobe » Post

Use get_node() if you want to detect a node, or get_objects_inside_radius() for anything mobile.

num3rical
New member
Posts: 3
Joined: Fri Jun 15, 2018 13:58
IRC: num3rical
In-game: num3rical

Re: How to detect an object?

by num3rical » Post

Astrobe wrote:Use get_node() if you want to detect a node, or get_objects_inside_radius() for anything mobile.
Thanks for the response. I gave this a shot, however I can't seem to get it working still.

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
})
There aren't any errors, it's just that the weirdworld:worms don't spawn at all.

User avatar
Krock
Developer
Posts: 4650
Joined: Thu Oct 03, 2013 07:48
GitHub: SmallJoker
Location: Switzerland
Contact:

Re: How to detect an object?

by Krock » Post

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
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!

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) 
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).
Look, I programmed a bug for you. >> Mod Search Engine << - Mods by Krock - DuckDuckGo mod search bang: !mtmod <keyword here>

num3rical
New member
Posts: 3
Joined: Fri Jun 15, 2018 13:58
IRC: num3rical
In-game: num3rical

Re: How to detect an object?

by num3rical » Post

Krock wrote:
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
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!

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) 
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).
Works perfectly, thank you very much! Guess I need to read up a bit more on tables in Lua :P

Post Reply

Who is online

Users browsing this forum: No registered users and 12 guests