Last active
May 6, 2021 19:56
-
-
Save henriiquecampos/8d98f0660da499967d41c32988cd3612 to your computer and use it in GitHub Desktop.
A very, versy simple way to save and load games in Godot Engine
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extends Node | |
func save_game(): | |
var scene = PackedScene.new() | |
scene.pack(self) | |
ResourceSaver.save("user://save_01.tscn", scene) | |
func load_game(): | |
var scene = load("user://save_01.tscn") | |
get_tree().change_scene_to(scene) |
You don't want to put this on every node. Instead, you can turn it into a reusable node that you put around the root of your main game scene:
func save(branch, save_id):
var scene = PackedScene.new()
scene.pack(branch)
ResourceSaver.save("user://save_%s.tscn" % save_id, scene)
func load(id):
var scene = load("user://save_%s.tscn" % id)
# change scene or return the loaded scene
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is meant to be attached to the top-most
Node
of a scene, i.e. the root.Ohh, by the way. If your player node isn't part of the PackedScene this node is in, i.e. if you didn't added it in this node's hierarchy through the Editor, making it part of the
.tscn
file corresponding to this scene, you have to manually set this node as the owner of the player before line 6.Something like:
Enjoy! 🎉