T O P

  • By -

AutoModerator

**How to: Tech Support** To make sure you can be assisted quickly and without friction, it is vital to learn how to asks for help the right way. **Search for your question** Put the keywords of your problem into the search functions of this subreddit and the official forum. Considering the amount of people using the engine every day, there might already be a solution thread for you to look into first. **Include Details** Helpers need to know as much as possible about your problem. Try answering the following questions: - What are you trying to do? (show your node setup/code) - What is the expected result? - What is happening instead? (include any error messages) - What have you tried so far? **Respond to Helpers** Helpers often ask follow-up questions to better understand the problem. Ignoring them or responding "not relevant" is not the way to go. Even if it might seem unrelated to you, there is a high chance any answer will provide more context for the people that are trying to help you. **Have patience** Please don't expect people to immediately jump to your rescue. Community members spend their freetime on this sub, so it may take some time until someone comes around to answering your request for help. Good luck squashing those bugs! Further "reading": https://www.youtube.com/watch?v=HBJg1v53QVA *I am a bot, and this action was performed automatically. Please [contact the moderators of this subreddit](/message/compose/?to=/r/godot) if you have any questions or concerns.*


Xe_OS

Not too sure because I’m no longer used to the $ syntax in gdscript, but the way I would’ve handled this would be Main having its own script within which it would hold a reference to the label and the GameManager would send a signal to the Main script for it to update the score label


Alemit000

Yup! My approach is to handle UI updates in a CanvasLayer script but the idea is the same.


Xe_OS

Yeah that would probably be a better organisation indeed


CakeOnBake

https://preview.redd.it/h0fy2gywzo9d1.png?width=1920&format=png&auto=webp&s=2f88d28de0b115beb05c3606741f78ae2711e21b Forgot to upload image. oops


Alemit000

Why are you getting 2 error messages? One says the GameManager is at root level, the other says the GameManager is the child of Main. It seems like you added GameManager to Autoload making it a singleton? In this case you should decide if you want to instantiate the node manually (by adding it yourself in the editor as a child of Main as you did) or if the game should instantiate it on its own (making it Autoload, which it seems to be already). The way it is right now, you have 2 GameManagers at the same time on different levels of hierarchy. Keeping it a singleton and deleting that manually created node seems like the better choice here since it's a root-level manager. Now to your problem: the more robust way of changing text in that label is attaching a script to your CanvasLayer and having it update the UI. Whenever score changes you can emit a signal from GameManager that your UI updating script will react to. That way you'll separate the logic of simply incrementing the Score variable and updating the Label text. Here's how that will look: ``` ## GameManager extends Node signal score_changed(new_value) ## A signal with 1 argument that contains the new value of score var score: int = 0 ## It's good practice to always explicitly assign a value to a newly created variable var difficulty: float = 1 var is_game_over: bool = false ## We don't need to get the path to the Label anymore func increase_score(): score += 1 difficulty += 0.03 score_changed.emit(score) ## Here, instead of directly changing Label text, we simply emit a signal saying that the score changed, with the new score value as its argument. ## GameManager doesn't know who will accept that signal, it doesn't need to. ``` ``` ## CanvasLayer @onready var score_text: Label = $"/Panel/ScoreText" ## Get path to Label. ## It's a direct descendant to CanvasLayer, so it's pretty straightforward. ## Using "../" (= getting path to a sibling) is regarded bad practice func _ready(): GameManager.connect("score_changed", _on_score_changed) ## Here we connect the GameManager's signal to a function in this script. ## Since that signal carries 1 argument (new value of score), the function also has to accept 1 argument. ## Also note that we can access GameManager globally by simply typing its name, that's how singletons work func _on_score_changed(new_score): score_text.text = "Score: " + str(new_score) ## Update Label's text when a signal comes through ```


CakeOnBake

It worked! I'm still trying to get the hang of signals.. Thanks! unrelated but the text just doesn't appear at all, not even the panel.


CakeOnBake

Never mind fixed it


Alemit000

Great! Signals are an amazing tool that allows you to make good code where each system is isolated and works on its own but can react to events happening around it. It's important to learn how to use them well and structure your project as a collection of small parts where everyone is responsible for their specific tasks.


fixedmyglasses

The label is not directly under the calling node, so it is not ready when you call onready. You’ll want to look up tree ready order and probably rethink the tree organization here. 


Nkzar

Right click the label node and choose Make Scene Unqiue, then use `%ScoreText`.