Page 1 of 1

Don't trigger "on_pre_build" when out of range

Posted: Tue Jan 26, 2021 5:39 am
by PFQNiet
I found out the hard way:

- Placing an entity that is blocked by collision does not fire the on_pre_build event
- Placing an entity that is out of build range does call the event.

Currently I am working around this by re-implementing the build range check myself:

Code: Select all

local function canReach(player, position, entity)
	-- building reach is based on the entity's north-facing collision box
	local box = entity.collision_box
	local dx = math.max(box.left_top.x + position.x - player.position.x, 0, player.position.x - (box.right_bottom.x + position.x))
	local dy = math.max(box.left_top.y + position.y - player.position.y, 0, player.position.y - (box.right_bottom.y + position.y))
	return math.sqrt(dx*dx + dy*dy) <= player.build_distance
end
I then just discard the event "if not canReach(...)".

So I have a workaround that works (although I haven't checked if that should be a "<" or a "<=" - I don't expect it to matter, it's literally an edge case!) but it would be nice if either the event didn't fire at all (potentially breaks existing mods?) or had the built-in range check exposed in "event.can_reach" (backwards-compatible).

Re: Don't trigger "on_pre_build" when out of range

Posted: Tue Jan 26, 2021 4:23 pm
by PFQNiet
Never mind, I just missed LuaPlayer#can_build_entity() in the docs. That solves my problem neatly.

Re: Don't trigger "on_pre_build" when out of range

Posted: Thu Jan 28, 2021 10:23 pm
by Rseding91