Simple mouse click behavior

Instructions on setting up simple mouse click behavior that only works if it’s touching a game objects Collision2D area. This doesn’t include cursor manipulation or anything else, purely a click and have the proper game object react.

  1. Add mouse click input in the Project Settings:

  2. Add a can_click switch to the object you want to be clickable, this will act as a guard from clicking anywhere and having the object react:

  3. In the visual script make the any state link have checking the mouse input and the can_click switch. The out link can be anything, time/re-click, etc, and it doesn’t need to check the switch:

  4. You do need a little code… but it’s very simple and copy paste able. Create a Node, and create a script on that node (you can name it anything) and then paste the code:

extends Node

@onready var switch_settings: SwitchSettings = $"../SwitchSettings"

var GO:GameObject

func _ready() -> void:
    GO = owner as GameObject
    GO.input_pickable = true
    GO.mouse_entered.connect(_on_mouse_entered)
    GO.mouse_exited.connect(_on_mouse_exited)

func _on_mouse_entered() -> void:
    switch_settings.set_value("can_click",true)

func _on_mouse_exited() -> void:
    switch_settings.set_value("can_click",false)
  1. Mouse is detecting inside the game objects Collision2D area, so make sure you set the size correctly:

And that is it! You should have a working clicking system where you can click an object and it can respond based on the visual script logic. In this video I just have it call a Display Text action, but the sky is the limit!

「いいね!」 1