How to make a game object clickable? (SOLVED)

Does exist an equivalent API to:
gui.pick_node
for the game objects?
I mean, if I want to know if a game object is clicked, is there a way to?
Or do I need necessarily a GUI object and gui.pick_node?
Thanks, Marco

3 Likes

There is no such function but you can compute the click/boundaries of game objects yourself, taking camera etc into consideration.

2 Likes

And most of the popular camera extensions like RenderCam or Orthographic have helper functions to translate camera position to world position of your mouse position.

Here is a simple rectangle collision check for seeing if a point is withina rectangle that assumes the center of the rectangle object is at its 0,0 point and expands outward evenly.

local function point_within_rectangle_centroid(point_x, point_y, rectangle_x, rectangle_y, rectangle_width, rectangle_height)
	local width_half = rectangle_width / 2
	local height_half = rectangle_height / 2
	if point_x >= (rectangle_x - width_half) and point_x <= (rectangle_x + width_half) then
		if point_y >= (rectangle_y - height_half) and point_y <= (rectangle_y + height_half) then
			return true
		end
	end
	return false
end
5 Likes

Depending on the use case you could also use collision objects to detect mouse/touch interaction with game objects. You can use my cursor.script from Defold-Input if you want to try that kind of solution.

4 Likes

Thanks everybody, as usual my problem is solved.
Marco

5 Likes