Godot Version
godot 4.7
Question
i am designing a 2d platformer/metroidvania i have a fully playable character but i am trying to add unlockable abilitys ive had some ideas but no idea how to implimant them.
A simple way to start is to keep a list of unlocked abilities on the player (or in an autoload, so it survives scene changes), have pickups add to it, and check it before running each ability.
# player.gd
var unlocked := {} # e.g. {"double_jump": true}
var used_double_jump := false
func unlock(ability: String) -> void:
unlocked[ability] = true
func has_ability(ability: String) -> bool:
return unlocked.get(ability, false)
func _physics_process(delta):
if is_on_floor():
used_double_jump = false
if Input.is_action_just_pressed("jump"):
if is_on_floor():
velocity.y = JUMP_VELOCITY
elif has_ability("double_jump") and not used_double_jump:
velocity.y = JUMP_VELOCITY
used_double_jump = true
# ...rest of your movement code, then move_and_slide()
The pickup can be an Area2D with an exported ability name:
# ability_pickup.gd
extends Area2D
@export var ability := "double_jump"
func _ready():
body_entered.connect(_on_body_entered)
func _on_body_entered(body):
if body.has_method("unlock"):
body.unlock(ability)
queue_free()
Drop a few of these in your levels and set ability in the Inspector for each one. If unlocks should persist between rooms or save files, move unlocked into an autoload and save it with the rest of your game data. Once you have more than a handful, an enum or a small custom Resource per ability keeps the names from getting typo’d.
Good luck with it! (Posting from the Novastart account, where we build an infinite canvas workspace, but metroidvania unlocks are a fun problem.)
Wouldn’t it be better to make entire game from main scene and add to it? You remove all problems with changing scenes then