0

I am trying to get a dynamically instanced kinematicBody2D with an area 2D attached to handle mouse entered/exit inputs. I have created my area 2D with correct collision body, and have tested a similar collision body for detecting some area 2d's and this is working happily, however, the mouse detection is not triggering the function as it should.

I am unsure of why it does not appear to be detecting my mouse. I am assuming I have messed with the Masks incorrectly, and it is not on the same level, however looking at some of the documentation this is not suggested to be a problem.

I am unsure of what code to attach because it is not really coded at this point.

Any help would be appreciated.

notme21
  • 47
  • 9

1 Answers1

0

To detect mouse events on an Area or KinematicBody, set input_pickable to true and connect to one or more of the provided signals.

KinematicBody2D and Area2D both inherit from CollisionObject2D, so they can both handle mouse input. This means you don't need to add an Area to your KinematicBody unless the area that detects clicks needs to be different than the area that detects collisions (e.g. only a small part of a larger object is clickable).

Here's how you could detect mouse events on a KinematicBody with some CollisionShape:

func _ready():
    input_pickable = true
    connect("mouse_entered", self, "_on_mouse_entered")
    connect("mouse_entered", self, "_on_mouse_entered")
    connect("input_event", self, "_on_input_event")

func _on_mouse_entered():
    print("mouse entered")

func _on_mouse_exited():
    print("mouse exited")

func _on_input_event(viewport, input_event, shape_idx):
    var mouse_event = input_event as InputEventMouseButton
    if mouse_event:
        prints("Mouse button clicked:", mouse_event.button_index)
rcorre
  • 6,477
  • 3
  • 28
  • 33
  • Hi @rcorre , thanks for the input. If I test this on the scene that the dynamically instanced objects are called from, this works great, however, using the same code, but embedded into another scene this interaction no longer works. – notme21 Nov 24 '19 at 21:13
  • Can you provide a [minimal example](https://stackoverflow.com/help/minimal-reproducible-example)? It will be hard for anyone to guess what the exact problem is without more details. – rcorre Nov 24 '19 at 21:30
  • https://github.com/benpotter480/simplified-godot-mouse-entered-issues is the repository for a simplified version. As you will be able to see the (hopefully) when the dynamically instanced scene is run by itself this works, but when instanced this no longer happens. – notme21 Nov 25 '19 at 16:52
  • @notme21 you have a completely different problem in your example. The `Panel` you added is capturing mouse input. You might want to ask a separate question for that, because it isn't clear why you have that `Panel` there or what you expect it to do. – rcorre Nov 29 '19 at 14:19
  • hi @rcorre, the panel I have used as a place holder for the sprite I have used as the background for this part of my system. It also helps to distinguish the instanced scene and the mains scene. The issue I am having is trying to get the dynamically instanced scene to detect mouse inputs from either the main or instanced scene. I hope this clears it up a bit. Thanks – notme21 Dec 01 '19 at 20:58
  • 1
    That panel is capturing the mouse input, that's why the function in your instanced scene isn't firing. – rcorre Dec 02 '19 at 12:36