In this section, I will show you how to add scores to the LevelMap so that it can update the number of stars after the game is over.
| Join the channel Telegram of the AnonyViet 👉 Link 👈 |
Scores
Solution
About how to handle the problem of this part, I will explain as follows:
+ First I will need a script and that script can be accessed anywhere, this script will be responsible for storing specific data here is the score of each map.
+ I will calculate the score achieved by 1 map in the script of EndLevelMenu, this score if you want to calculate it will need: (number of coins collected / total number of coins in the map) * 100.
+ To get the total number of coins, I will add the coin scene to a group called dongxu and I have a function get_tree().get_nodes_in_group(“dongxu) which will allow me to get all the coins available on the scene and This function will return array.
+ Then in LevelMap some children Level1,2,.. I will assign the variable SoDiem did in the previous part equal to the score in the script containing the data.
Autoload scripts
This is a feature of Godot that allows you to turn a script into an autoload script, meaning it always runs no matter what scene you are in and can be accessed anywhere.
![[Tạo 2D Platformer Game với Godot] Part 26: Adding scores at the end of the game 22 [Tạo 2D Platformer Game với Godot] Part 25: Add scores at the end of the game](https://anonyviet.com/wp-content/uploads/2021/11/23-11-2021-11-50-21-300x269.png)
My friend made me a new script called PlayerData in Scenes
![[Tạo 2D Platformer Game với Godot] Part 26: Adding scores at the end of the game 23 [Tạo 2D Platformer Game với Godot] Part 26: Adding scores at the end of the game 19](https://anonyviet.com/wp-content/uploads/2021/11/23-11-2021-11-50-39-300x74.png)
Then go to Project -> project settings -> Autoload
![]()
Click on the folder icon.
![[Tạo 2D Platformer Game với Godot] Part 26: Adding scores at the end of the game 25 [Tạo 2D Platformer Game với Godot] Part 26: Adding scores at the end of the game 21](https://anonyviet.com/wp-content/uploads/2021/11/23-11-2021-11-51-07-300x184.png)
Find the script just created
Click on add and you will see it displayed in the box below.
Add group scene coin
Through the coin scene
On the right side next to signals, click on the group and type the group name -> click add to add the node to the group
Map
![[Tạo 2D Platformer Game với Godot] Part 26: Adding scores at the end of the game 33 [Tạo 2D Platformer Game với Godot] Part 26: Adding scores at the end of the game 29](https://anonyviet.com/wp-content/uploads/2021/11/23-11-2021-11-58-10-300x130.png)
In the Map1 scene, I will add 1 Node2D to contain DongXu nodes to keep it neat.
Then I edited it so it’s easy to collect
How much you adjust the coin is up to you, not necessarily like me.
Player
In the Player’s script, you just need to add yourself a variable that is:
onready var max_dongxu = get_tree().get_nodes_in_group("dongxu")
This variable I use to determine the total number of coins in the map.
PlayerData
In the Playerdata autoload script that I just created above, I added a Dictonary variable named score and it is responsible for carrying the score of each map.
extends Node
var diemso = {
"Map1": 0,
"Map2":0,
"Map3:": 0,
}
extends CanvasLayer
onready var level = get_parent().get_node("KhuVucDiChuyen")
onready var map = get_parent().name
onready var player = get_parent().get_node("Player")
var diemso
func _on_NutLevelKeTiep_pressed():
get_tree().paused = false
get_tree().change_scene(level.LevelKeTiep)
diemso = int(( float(player.dongxu) /player.max_dongxu) * 100)
if diemso > PlayerData.diemso[map]:
PlayerData.diemso[map] = diemso
pass # Replace with function body.
func _on_NutChoiLai_pressed():
get_tree().paused = false
get_tree().reload_current_scene()
print("choi lai")
pass # Replace with function body.
func _on_NutVeMenu_pressed():
get_tree().paused = false
get_tree().change_scene("res://Scences/Map/LevelMap.tscn")
diemso = int(( float(player.dongxu) /player.max_dongxu.size()) * 100)
if diemso > PlayerData.diemso[map]:
PlayerData.diemso[map] = diemso
pass # Replace with function body.
Here, I declare 3 more variables:
+ map: for me to identify the name of the map I am playing.
+ player: so that I can get the number of coins collected and the total number of coins in the map.
+ diemso: calculate the score you achieved.
func _on_NutLevelKeTiep_pressed():
get_tree().paused = false
get_tree().change_scene(level.LevelKeTiep)
diemso = int(( float(player.dongxu) /player.max_dongxu) * 100)
if diemso > PlayerData.diemso[map]:
PlayerData.diemso[map] = diemso
diemso = int(( float(player.dongxu) /player.max_dongxu) * 100): is the formula I calculate the score.
if diemso > PlayerData.diemso[map]: I check if the diemso of this level is greater than the existing score, it will add this score and vice versa.
I will do the same with the NutVeMenu function because when you show the EndLevelMenu, you have already completed it, so exiting or continuing to play will count and if you press play again, it will not.
Level Script
![[Tạo 2D Platformer Game với Godot] Part 26: Adding scores at the end of the game 34 [Tạo 2D Platformer Game với Godot] Part 26: Add scores at the end of the game 30](https://anonyviet.com/wp-content/uploads/2021/11/24-11-2021-12-08-18-300x80.png)
You passed yourself the Level scene in the LevelMap.
And in the _process function, I add 1 line to update the score.
SoDiem = PlayerData.diemso[MapName]
I will have the full code as below:
extends Control
export (String) var MapName
export (String,FILE) var Scene
export (String) var Level
onready var ngoisaovang = preload("res://Assets/LevelMap/3 UI/Star1.png")
var SoDiem = 0
func _ready():
$SoLevel.text = Level
func _process(delta):
SoDiem = PlayerData.diemso[MapName]
if SoDiem >= 1 and SoDiem <= 30:
$NgoiSao1.texture = ngoisaovang
elif SoDiem >= 30 and SoDiem <= 95:
$NgoiSao1.texture = ngoisaovang
$NgoiSao2.texture = ngoisaovang
elif SoDiem >= 95:
$NgoiSao1.texture = ngoisaovang
$NgoiSao2.texture = ngoisaovang
$NgoiSao3.texture = ngoisaovang
func _on_HinhTron_pressed():
get_tree().change_scene(Scene)
pass # Replace with function body.
Result
![[Tạo 2D Platformer Game với Godot] Part 26: Add scores at the end of the game 35 [Tạo 2D Platformer Game với Godot] Part 26: Adding scores at the end of the game 31](https://anonyviet.com/wp-content/uploads/2021/11/24-11-2021-12-11-14.png)
So after collecting all the coins I added and returning to the Level Menu, I will get 3 stars.
Note
In that Menu, you edit the code where the Start button is, the change_scene is the scene Level Map.
![]()
Summary
So I have finished guiding you how to add the number of stars achieved, in the next part I will guide you how to lock the level again and unlock the next level.
![[Tạo 2D Platformer Game với Godot] Part 26: Adding scores at the end of the game 28 [Tạo 2D Platformer Game với Godot] Part 26: Adding scores at the end of the game 24](https://anonyviet.com/wp-content/uploads/2021/11/23-11-2021-11-54-30-300x96.png)
![[Tạo 2D Platformer Game với Godot] Part 26: Adding scores at the end of the game 29 [Tạo 2D Platformer Game với Godot] Part 26: Adding scores at the end of the game 25](https://anonyviet.com/wp-content/uploads/2021/11/23-11-2021-11-54-41.png)








