Add score at the end of the game

In this part, 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.

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, which is the score of each map.

+ I will calculate the score achieved for 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 which means it always runs no matter what scene you are in and can be accessed anywhere.

[Tạo 2D Platformer Game với Godot]  Part 25: Add scores at the end of the game

My friend gave 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 61

Then go to Project -> project settings -> Autoload

[Tạo 2D Platformer Game với Godot]  Part 26: Adding scores at the end of the game 62

Click on the folder icon.

[Tạo 2D Platformer Game với Godot]  Part 26: Adding scores at the end of the game 63

Find the script just created[Tạo 2D Platformer Game với Godot]  Part 26: Adding scores at the end of the game 64[Tạo 2D Platformer Game với Godot]  Part 26: Adding scores at the end of the game 65Click on add and you will see it displayed in the box below.

Add group scene coin

[Tạo 2D Platformer Game với Godot]  Part 26: Adding scores at the end of the game 66

Through the coin scene

[Tạo 2D Platformer Game với Godot]  Part 26: Adding scores at the end of the game 67

[Tạo 2D Platformer Game với Godot]  Part 26: Adding scores at the end of the game 68[Tạo 2D Platformer Game với Godot]  Part 26: Adding scores at the end of the game 69On 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 70[Tạo 2D Platformer Game với Godot]  Part 26: Adding scores at the end of the game 71

In the Map1 scene, I will add 1 Node2D to contain DongXu nodes to keep it neat.

Then I edit 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 variable of Dictonary 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 as in 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 72

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: Adding scores at the end of the game 73

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.

[Tạo 2D Platformer Game với Godot]  Part 26: Adding scores at the end of the game 74[Tạo 2D Platformer Game với Godot]  Part 26: Adding scores at the end of the game 75[Tạo 2D Platformer Game với Godot]  Part 26: Adding scores at the end of the game 76

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.

Leave a Reply