Mouse click handling to get the clicked 3d object, almost working..

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Godotmus

Hi,
Godot 3.1 (Windows10)
I have simple 3d object that I instantiate in main game many many times to get grid like structure. Goal is simply to return 3d object from mouse click so I can get clicked grid cell info.
Googling godot 3d view mouse click handling gives very confusing and old answers but I manage to get it almost working.

My clickable ghost 3d object structure:
-Spatial
–StaticBody
—Collision shape
(just a box)

Static body has a script:

 func _input_event(camera, event, click_position, click_normal, shape_idx):
	if event is InputEventMouseButton:
		print("Mouse Click/Unclick at: ", event.position, " shape:", shape_idx)

Works kind of ok and click is pixel perfect in 3d world but always return 0 as shape index… does that represent just that my ghost object has one collision shape and its index is 0?
How to get info which ghost object instance from 25 or so I actually clicked?

Thanks

:bust_in_silhouette: Reply From: wombatstampede

Ok, your script runs into the StaticBody.
So self is actually the node = StaticBody which is clicked.
So you work with all methods of a StaticBody.

I.e. you can call print("Clicked: "+name) (same as print("Clicked: "+self.name) to print the nodes name to the output window.

Now it depends what you want to do with that node. For example, you’ve got some script in the parent Spatial which should do something with that node. That script has a function func node_was_clicked(node):. Then you could call $"..".node_was_clicked(self) (identical to: get_parent().node_was_clicked(self) or self.get_parent().node_was_clicked(self))

Thanks, it was easier than I thought. StaticBody has the event handler and it can use Spatial (parent) function or variable (or even send signal to main script… edit: actually no, signals are bit hard to connect with instantiated nodes…)

Couple of question still, I tried to move event handler to Spatial parent and it did not work…

(in staticbody)
func _input_event(camera, event, click_position, click_normal, shape_idx):
if (event is InputEventMouseButton and event.pressed and event.button_index == BUTTON_LEFT):
	print("cell: " + str(self.get_parent().cell))

(in Spatial parent)
func _input_event(camera, event, click_position, click_normal, shape_idx):
if (event is InputEventMouseButton and event.pressed and event.button_index == BUTTON_LEFT):
	print("cell: " + str(cell))

if I tried plain _input(event) then one click produced events from all instanced “ghost object”, why is that working that way?

And more about editor, editor suggestions did not contain event.pressed and event.button_index or event.get_button_index()
I was wondering why InputEventMouseButton event does not contain these properties or functions… makes newbie godotter life harder :slight_smile: Had to type them “blindly” and they worked…

Godotmus | 2019-04-16 16:35