How to make a "you died" text appear when your health hits 0?

Godot Version

4.2.2

Question

I am very new to coding and have been toying with making a 3D souls-like, taking it one step at a time. I have coded a health variable that I can reduce to 0. Now I want to make to make a “you died” text show up for a few seconds after dying like in dark souls. So I’m wondering how I make such text appear. All the tutorials on YT I could find just tell me to make a full new scene with button to respawn, but I just want the text to appear (with maybe some music and effects later) before I make the player respawn. So how do I do that?

Currently I’ve tried hiding and unhiding the text but I can’t find out how to make that work. Neither does making a you died scene with only a label and a get.tree. Thanks.

If you want a super simple “YOU DIED” text appear over everything else, you could use the following setup:

  1. Create a new scene that’s just your “YOU DIED” Label (and perhaps a CenterContainer to assist with placement). Save it as it’s own separate scene.
  2. Add the above scene to your game scene and give it a unique name in the scene tree like YouDiedScene. Test a bit to make sure it appears how you want it to (but right now it’s showing all the time). Once satisfied, set the property visible to false in the inspector. It will be hidden from view but still be in the scene.
  3. Save a reference to the added scene from above. You can use var you_died_scene = find_child("YouDiedScene") if you used the same name.
  4. When you see that the player has reached zero (or less) health, just make it unhide by you_died_scene.visible = true so it appears for the player to see.

There’s more that can be done to change this up, but at an absolute basic level this should work. Having the Label and any containers as a separate scene makes it easy to modify it but also easy to assign it as a variable.

Later, when you want sounds, etc, you can experiment with making death update the entire game as a signal. That will let you have anything that needs to know that the player died just wait for that signal to be emitted by the health code. An audio manager can stop level music and play a death sound. A game manager can reload the last save or exit to menu. And, of course, you can show the message.

1 Like