[Tạo 2D Platformer Game với Godot] Part 27: Unlocking Levels

In this section, I will show you how to unlock the next level after completing the previous level.

It’s not that you like to click on any level to play that level.

Unlock levels


For this part, I will have the following algorithm:

+ I will add a variable in PlayerData to store the data of which map has been unlocked, with the structure as below:

var Unlocked_map = {
    "Map1": false,

+ Then in KhuVucDiChuyen after the Player has gone there ie won the game, then I will set the Map I’m playing in the Unlocked_map variable to true

func _on_KhuVucDiChuyen_body_entered(body):
        PlayerData.Unlocked_map[get_parent().name] = true

+ Then in the Level scene I will check if I click on that Level and that Level if it’s Map1, it will play Map1 because this is the first map I need to play.

+ Then, if it’s not Map1, that means you’ve clicked on Level 2 or higher and then I check if the previous Map that you clicked on has been completed, I will switch to playing that Map.

For example: If I haven’t played Map1, then Unlocked_map[“Map1”] is = false, I click on Level 2 and then Level2 checks if Unlocked_map[“Map1”] is = false then you have not completed it and cannot play level2 and vice versa.


extends Node

var diemso = {
    "Map1": 0,
    "Map3": 0,
var Unlocked_map = {
    "Map1": false,

I added a dictionary containing the data of unlocked maps.


extends Area2D

export (String,FILE) var LevelKeTiep
onready var endlevelmenu = get_parent().get_node("EndLevelMenu")

func _on_KhuVucDiChuyen_body_entered(body):
    if body.name == "Player":
        get_tree().paused = true
        PlayerData.Unlocked_map[get_parent().name] = true

Moving area, I add 1 more line at the end.

And if you notice in the previous section, I have a few lines of code to add scores, but to add the score to PlayerData, it has to press 1 of 2 buttons. So, what if the player just went in and the menu won the game out? Definitely have to start over again.

+ So I will put a small exercise here is to transfer the line of code to add scores to PlayerData at EndLevelMap through KhuVucDiChuyen.

Level Scene

At the Scene level, I just added the code to the Hinhtron_pressed() function:

func _on_HinhTron_pressed():
    var level_truocdo = int(Level) - 1
    if MapName != "Map1":
        if PlayerData.Unlocked_map["Map"+ str(level_truocdo)] == true :

I create one more variable level_truocdo and I add int() at Level because the Level variable is being declared as String if you don’t add int, it will be String – Integer and cause an error.


[Tạo 2D Platformer Game với Godot]  Part 27: Unlocking Levels

Now if you run the game and click on the level you will see that it doesn’t move to the level 2 scene anymore, but has to complete the level 1.

Because of this, I don’t know what kind of picture I should get for you to see, so I’ll read it @@.


In this section, I have shown you how to add level unlock feature. In the next part, the sound will be added.

Leave a Reply