Trouble with understanding node system and how to append it to an array

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Wurzelpilz

I’ve still big trouble to understand the nodesystem of Godot, this is my setup:

enter image description here

So when I enter the Area2D with my mouse, the Test node should be appended to a global array. How do I do that? In my area2D under advanced I set a transform argument… since I can’t chose node or gameObject?! But that of course gives an error, since transform does not contain get_owner() … in Unity it’s transform.parent to get the parent object but it doesn’t work here.


func _on_Area2D_mouse_entered(var transform):
	if Input.is_mouse_button_pressed(1) :
		Globals.i_dont_know_what_to_call_it.append(transform.get_owner())
		print(transform)
:bust_in_silhouette: Reply From: SnapCracklins

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 mouse_entered and mouse_exited 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)

First and foremost, thanks for your help and I love arrays too :). I think my question is a bit too vague explained.This is unfortunately not really the issue I have. I don’t need to append the scriptholder but the parent node of the area2D, the Test node, that’s what I’m struggling with. I don’t know how to grab this node.

… But I think I just give every test node this script instead of trying to do it from a different, single node

Wurzelpilz | 2022-08-18 19:24

Yeah, try it out and report back. I tried it in the way you had originally and had the same issue you had. That variable flipping when the mouse enters the area as a condition for you “Grabbing” the item should work! Good luck.

SnapCracklins | 2022-08-19 01:07