Run code from one node in another (messages

Godot Version

4.3

Question

I am triying to make code from one node run on another, something like this

Func some input
Run on some node.some code

or something similar, if You wonder i’m triying to do queue free to a colision shape in the same scene as the node that runs the code

Yeah then do it? what`s the problem? You can get any node by dragging it to the script.

i dont know how to do it (and it working), yes i can drag the collision shape to the script and make something like this

func _on_area_2d_body_entered(body: Node2D) -> void:
	if body.is_in_group("car"):
		$"../collisionshape2".queue_free()

it simply does not work like that, then i tried using @onready like this

@onready var collision1 = $"../collisionshape2"

and then

func _on_area_2d_body_entered(body: Node2D) -> void:
	if body.is_in_group("car"):
		collision1.queue_free()

but it does not work too and i cant find the solution because i dont know the name of what i am triying to do to search it on the docs so thats why im here
(sorry if there are any spelling mistakes, english is not my first languaje)

What do you mean by “it does not work”? What do you expect to happen, and what happens instead? (Consider posting a screen recording of what happens if you can.) Do you get any warnings or errors? What does your scene tree look like?

the scene tree looks like this
characther body
-collisionshape2d
-sprite
-area 2d (script shown above and signal that calls the function)
—collisionshape2d
it then is placed in a level with the obstacles (cars) that are classified in the tag car

while running the game, if you crash, a sound plays and after 0.75 seconds it resets the level scene, what happens that if you move up and down quickly touching the car repeatedly, the sound plays repeatly because it detects again collision, so i am triying to remove the collision shape (not the son of the area2d, the one outside)

and what happens, well the collision shape stays (i can see it in debug mode), the log shows absolutely nothing and i cant find where’s the error

i sadly cant provide a screen recording right now

using lambda functions with .bind(target) can easily make any node run added code if connected to any (custom) signal.

if owner has func like

func _ready()->void:
	var test_node:=Node.new()
	test_node.ready.connect(remote_print.bind(test_node, "NODE READY"), 4)
	add_child(test_node)
func remote_print(node:Node, text:String)->void:
	print("DEBUG - string output: "+text)

bind produces a copy of callable with defined args. Signal “ready” runs it. Flag 4 means one shot.
this will make test_node print text “DEBUG - string output: NODE READY”

2 Likes

thank you