I think the problem here is that you don't have a value to tell you when the mouse is in area or not. You have to code the physics a bit to make them do something. A bool variable really helps with Area2D to help determine when something is in or out. Also, this input method from the docs worked fine for me. Try this out after connecting those mouse signals in the editor. This code assumes your Global singleton script is called Global.
- Your scene tree is correct. I attached this code to the Node2d object (what you named TestNode).
- The mouseentered and mouseexited signals have been connected in the Inspector from the Area2D object. (The script is on the root node, Node2D). Should you save this node as a scene to instance later, this will allow you to drag and drop easily without having to reconnect signals all the time.
- Make sure your singleton script (Global) is configured to load in AutoLoad in settings.
You will also want to build some logic in there to prevent double triggering, I imagine? In case you need that, this code works for that nicely. (I LOVE ARRAYS if you can't tell). Good luck!
var _can_trigger = false
func _on_Area2D_mouse_entered():
_can_trigger = true
func _on_Area2D_mouse_exited():
_can_trigger = false
func _input(event):
if event is InputEventMouseButton and _can_trigger == true:
if event.button_index == BUTTON_LEFT and event.pressed:
if Global._myArray.has(self):
pass
else:
Global._myArray.append(self)
print(Global._myArray)