0 votes

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

in Engine by (38 points)

1 Answer

0 votes

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

by (3,203 points)

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.getbuttonindex()
I was wondering why InputEventMouseButton event does not contain these properties or functions... makes newbie godotter life harder :) Had to type them "blindly" and they worked..

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.

Categories