0

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?

Rex Nihilo
  • 604
  • 2
  • 7
  • 16

1 Answers1

1

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.

magenulcus
  • 400
  • 4
  • 10
  • I think this idea would work better than what I was doing, but the 'event.is_action' throws an error saying it can't find the identifier "event". I wonder if I have a weird version of Godot because I have seen this in forums but it hasn't ever worked for me :/ – Jacob Porter Jul 13 '17 at 00:06
  • It is difficult to nail down the issue without seeing the code. Could you post some snippets? The event "identifier" is an argument of the _on_Area2D_input_event function. How did you create the function? Did you use the Godot editor or did you just type the function in the script? In addition, if you think you have a bad Godot version just download a new one (the current stable is 2.1.3) and try again. – magenulcus Jul 13 '17 at 19:58