+1 vote

So I have made a 2D platformer game in which currently I have almost 4 stages and in each level the player can increase his score by collecting diamonds. The score gets increased with every diamond he collects and he can see his score on the top left corner of screen. But I want to make sure that when the level ends the player gets a screen where he sees his total score of that level and then continue to next level because right now the score becomes 0 when player advances to next level and the process begins again.

I use this in the player script :

func _on_Area2D_body_entered(body: Node) -> void:
score+=1
var scoretext="Score : "+String(score)
scorelabel.clear()
scorelabel.add_text(scoretext)

where I get scorelabel using this :

onready var scorelabel = get_parent().get_node("Score/ScoreDisplay")`
in Projects by (359 points)

I would use a "AutoLoad" script as a global class for the score variable:

https://docs.godotengine.org/en/stable/getting_started/step_by_step/singletons_autoload.html

1 Answer

0 votes

You can use Godot version of the Singleton pattern. Basically, you make a script available everywhere in the project and game, allowing you to store the score "globally", it will persist in all scenes and scripts

https://docs.godotengine.org/en/stable/getting_started/step_by_step/singletons_autoload.html

by (52 points)
edited by
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.

Categories