unlock levels in a game

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By THE HELIX

how can i unlock a level ( for example Level 2 is locked). i should be able to unlock Level 2 using the coins i gained in Level 1(for instance let there be 5 coins in level 1) .When Level 2 is unlocked the coins of level 1 should become 0 and the player can now play Level 2.
Could u please help me out with the scripting?

Can you give us some code? for example, how you store coins for each level. Are they singletons? How you access each level? Is there a menu where you click each level and you have to unlock the button to enter the level?

p7f | 2018-12-20 14:39

yes ; there is a menu where you have to click each level and you have to unlock the button to enter the level

THE HELIX | 2018-12-20 15:12

Can anybody please help me out in achieving the unlock format? “PLEASE”.

THE HELIX | 2018-12-21 07:32

You really need to give us some code if you want help, or even to show us the entire peoject, cause there are many variables that can vary depending on how the game is implemented. I would make a singleton that stores coins each level, and on main menu, i would make a function on locked buttons that unlock them if the right ammount of coins in the previous level is stored in that singleton.

p7f | 2018-12-21 11:05

:bust_in_silhouette: Reply From: DEKADO

Sorry for my English, I’m using the google translator.

I have a question about the subject and I have the following Scripts that I have been implementing through an online tutorial.

#Button to select level

extends Node2D
#Nivel
export (int) var level
export (String) var level_to_load
export (bool) var enabled
export (bool) var score_goal_met
#Textura
export (Texture) var block_text
export (Texture) var open_text
export (Texture) var goal_met
export (Texture) var goal_not_met
onready var level_label = $TextureButton/Label
onready var button = $TextureButton
onready var start = $Sprites
func _ready():
if gamedata.level_info.has(level):
enabled = gamedata.level_info[level][“unlocked”]
else:
enabled = false
setup()
music.stop()
func setup():
level_label.text = String(level)
if enabled:
button.texture_normal = open_text
else:
button.texture_normal = block_text
level_label.hide()
if score_goal_met:
start.texture = goal_met
level_label.show()
else:
start.texture = goal_not_met
func _on_TextureButton_pressed():
if enabled:
get_tree().change_scene(level_to_load)

And this Script is the global variables but it has confused me as I mentioned above as I implement this together with my Item “star” so that when collecting 3 the next level “Level 2” is unlocked.

#GAMEDATA GLOBAL VAR AUTOLOAD
extends Node

var level_info = {}
var default_level_info = {
1:{
“unlocked”: true,
“stars_unlocked”: 0
}
}
onready var path = “user://save.dat”

func _ready():
level_info = load_data()

func save_data():
var file = File.new()
var err = file.open(path, File.WRITE)
if err !=OK:
print (“Algo esta mal”)
return
file.store_var(level_info)
file.close()

func load_data():
var file = File.new()
var err = file.open(path, File.READ)
if err !=OK:
return default_level_info
print(“Algo esta mal”)
var read = {}
read = file.get_var()
return read
Can you help me to better understand this?