Skip to content

Instantly share code, notes, and snippets.

@henriiquecampos
Last active May 6, 2021 19:56
Show Gist options
  • Save henriiquecampos/8d98f0660da499967d41c32988cd3612 to your computer and use it in GitHub Desktop.
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
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)
@henriiquecampos
Copy link
Author

henriiquecampos commented Oct 25, 2018

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:

    get_node("path_to_player").owner = self

Enjoy! 🎉

@NathanLovato
Copy link

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