How do I retain the player viewport upon queue_free() or change the *world* camera to follow the player (2D)?

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

Pretty simple - maybe. I have a camera (current) that follows the player, but I also have a main viewport that is in a static position to the world and contains some UI elements.

When my player dies (queue_free) I create a popup. However, since the camera is a child of the player, I can’t display this popup in the same position that the player died (because the player viewport is gone when the player is queue_free() ).

So, how do get my camera to stay at the player’s position upon player death (queue_free())?

I assume a better way to go about this is to get the main “world” viewport that is currently static to follow my player instead of having the player’s camera child at all, but all the tutorials I’ve seen only have the camera as a child of the player, and I’ve looked and looked.

Any ideas on how to solve this dilemma?

:bust_in_silhouette: Reply From: scrubswithnosleeves

It is pretty tricky to make a camera follow that isn’t a child. The documentation starts to go into it if you look up camera2D in the docs.

My first question would be, why queue_free() right when he “dies”? Why not just make him invisible and stop taking input? that is probably the best option. Then after the popup you queue_free().

You could also try re-parenting the camera node:

self.remove_child(Camera2D)
get_parent().add_child(Camera2D)
Camera2D.global_position = global_position

I use this code to re-parent particle effect trails on projectiles after they explode. I am only worried about the global_position part in this case because idk if the Camera2D will work with that.

lmk if you need any further help!