#Finding scene "root" from collision
1 messages · Page 1 of 1 (latest)
How far away from root is your body in your scene tree?
In most projects I've seen, CharacterBody2d or CharacterBody3d is the root of the scene, or a direct child.
Otherwise, this has worked for me (for a state machine, not for bodies, so good luck)
in root node:
@onready var child_body := $Whateveryourchildnode'snameis
func _ready() -> void:
child_body.init(self)
in child body:
var my_daddy
func init(parent) -> void:
my_daddy = parent
Then, when your raycast hits the body:
func _on_body_entered(body : CharacterBody2d(or whatever)): -> void:
body.mydaddy.take_damage() #or whatever functions you want the parent to run.
Basically just pass a reference of the root node to its children, so that they can reference it directly. Although, I'm having a hard time understanding under what situation you'd need to do this with bodies. Like, you have a robot with destructible parts and each part needs to be able to communicate with the parent node, but you could just do that with signals.