I have been coding a form of platformer, and am having a problem with a label. I want the label to show when you fall off the map, and hide when youre just playing. I have had trouble getting the label to hide. I have checked my other scripts and there is no reference to the label, the variable controlling it (Global.global) is set to zero and works properly. The label is a child node of the player, which is set to visible, although from what ive read it seems that it should only affect it if the parent node was set to hidden, or invisible.

extends Label

func _ready() -> void:
visible = false
hide()
visible = false
modulate.a = 0.0

func _process(float):
if Global.global == 1:
visible = true
if Global.global == 0:
hide()
visible = false
modulate.a = 0.0
print(Global.global)

  • Toxe replied to this.

    That code is unreadable without the actual indentation.

    The </> "Insert code" feature is not reliable.

    Place ~~~ by itself, with no leading or trailing spaces, on lines before and after the code. Then the code will display properly.

    Hey there, I changed this discussion to the help tag. 👍

    BonkBonkDonkey The label is a child node of the player

    Unless your player is derived from Control this is probably your problem. Add a proper UI layer to your program where you show the label.

      Toxe ive tried adding a ui layer, it makes the label stationary, but to be fair, im pretty new and wouldnt know if there was a way around that lol. thanks tho

        BonkBonkDonkey Forget what I said, you can totally attach a Label to a Node2D, I just checked. I guess I confused this with another problem that I had in the past where I wasn't able to use a Label but had to use a Sprite instead. But I don't remember the details anymore. it had something to do with the player rotating and I wasn't able to prevent the Label from rotating as well so I had to use a Sprite and reset the rotation every frame or something like that.

        Anyways, it should work. Can you upload your project here (without the .godot directory) or show more of your code and setup?

        Looking at your code again, your problem might simply be that you never set the alpha of your modulate back to 1.0 when you show the label. Best to remove the modulate and hide() lines at all and only work with the visible property.

        All you have to do is put the label under the player scene as a child in the level scene tree.

        Here is the code needed for the control node:

        extends Node2D
        @onready var test_label: Label = %TestLabel
        
        
        # Called when the node enters the scene tree for the first time.
        func _ready() -> void:
        	test_label.hide()
        
        func _on_area_2d_body_entered(body: Node2D) -> void:
        	test_label.show()

        If that works, please let me know.

        I am almost positive it works, because I tested it in Godot before answering your question.

        I didn't exactly know myself, so I figured out how to do it with a lot of trial and failure.

        You could also use the area2d function to start a timer that reloads the current scene after the player has had enough time to read it.

        If you wanted to do that, the code for that would be the following:

        extends Node2D
        @onready var test_label: Label = %TestLabel
        @onready var dead_timer: Timer = %DeadTimer
        
        
        # Called when the node enters the scene tree for the first time.
        func _ready() -> void:
        	test_label.hide()
        
        func _on_area_2d_body_entered(body: Node2D) -> void:
        	test_label.show()
        	dead_timer.start()
        
        func _on_dead_timer_timeout() -> void:
        	get_tree().change_scene_to_file("res://test_scene_2d.tscn")

        I also tested this, so it should work.

        you will have to replace the @onready variables with the actual nodes you use in your scene. You will have to right click, access as unique name, and then drag the node into the code and hitting Control before that. the _on_area_2d_body_entered function with an actual signal from an actual area 2D node.

        I hope this helps,
        ArDanZ11 out for now.