Orphan a child through code in Godot 4?

Godot Version

4.2.2

Question

Hey! In my game, there is a spike that kills and deletes the player node. But since the camera is a child, I need to orphan the camera before the player dies so the player can see their death. It seems like a simple question, but I’ve looked at the documentation, looked at tutorials, and asked ChatGPT, but I found nothing. I’ve debugged the code and the code does work, but I just need the line. If I could get help, that would be great, and thanks in advance!

1 Like

I would avoid actually deleting the player, hide their sprite, set_physics_process(false), etc.

3 Likes

I would use @gertkeno s solution as well in most cases. But in case you actually want to reuse the camera, I’d consider these steps:

  1. choose a new parent node, new_parent for the camera (e.g. the level, a different player,…)
  2. call camera.reparent(new_parent, true). Having the second argument set to true means that the camera keeps it current position
  3. if you want to switch to a different camera camera2 you can call camera2.make_current()

This does not really orphan the camera (it is still part of the scene tree), but if you make a different camera the current one, the previous camera is not in use anymore.

1 Like

Instead camera in player node you can have RemoteTransform with camera in Remote Path(not child of player), now you can kill player and camera will be untouched
obraz


obraz

1 Like

Thank you all for your answers! I hope you all have an amazing day!

2 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.