#Finding scene "root" from collision

1 messages · Page 1 of 1 (latest)

sacred haven
#

What's the Godot way to do scene / actor split akin to Unreal? When I raycast I hit a body, but I'd like to somehow get a reference to the parent of that body that's the root of my "actor" scene. I can traverse the parents until I get an Actor node, but that seems like I'm trying to solve the wrong problem.

oblique jay
#

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.