Reputation: 1
I am trying to add two different functions to a button node in godot. I would like it to show a message when left clicked and make the sprite it's attached to disappear when it's right clicked. Is there a way to do this in gdscript?
Upvotes: 0
Views: 1839
Reputation: 400
I am not sure if you can differentiate between a left click and a right click using the Button class. However, there are multiple easy ways to let the sprite react the way you want.
I am not sure if you just added the Button as an area which reacts after a click. If so, you could also add an Area2D (or 3d) and a collision shape to your sprite. With the collision shape, you can link the "input_event"-signal of the Area2D Node to the Sprite script (or whatever script you use in this scene). The easiest way to link the Signal is via the Signal ribbon on the lower left side of the editor.
The editor automatically creates a new function and you can code whatever behaviour you want like:
func _on_Area2D_input_event( viewport, event, shape_idx ):
if event.is_action('left_click'):
print("Left click message")
elif event.is_action('right_click'):
self.hide() # hides the node which owns the script...
Before you can use the is_action function you have to define 'left_click' and 'right_click' to the Input map under preferences of the editor. In general, it is always a good idea to use the Input map instead of hard coding all keys and buttons.
I hope that helps.
Best regards and happy coding.
Upvotes: 1