How can i save data and progress when i switch from a scene to another?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Kiren
:warning: Old Version Published before Godot 3 was released.

Hi, i made 2 scenes with buttons to switch from one to another. On the first scene there are nodes showing hp information on bars (i’m using TextureProgress).
I noticed that when i switch to the second scene and then i go back to the first one, everything is set to default status, every “progress” is not stored.

I captured a video of my project to let you visually get what i mean:

Project Video

I’m here to ask if any of you can show me a way to keep the data and progress while switching from a scene to another. If you can show me a sample code it will be great.
Thank you in advance.

:bust_in_silhouette: Reply From: SingingApple

This is pretty easy to do with the power of Godot.
Create a global.gd script an save it as an autoloaded singleton. (If you don’t know how to do it, click here. The link should lead you to the Godot Documentation with the necessary information).
Then you can create three variables in the global.gd script.
In the scene where you change the progress of each bar. You can set the current value of each progress bar to the three variables you created in global.gd script (This script can be accessed from anywhere within the game tree).
On reloading the scene you can set the three variables in global.gd script to the three progress bars. This will hopefully retain the value of the progress bars just as you wished.
Hope this helps.

Hi, thank you for your answer. I read the doc on the link you posted. I created a global.gd script and i added it in the Autoload tab of the Project settings. The script looks like this:

extends Node

var bar1
var bar2
var bar3

func _ready():
    # Called every time the node is added to the scene.
    # Initialization here
    pass

I can’t manage though to get the second part work. In particular to read the value of the first bar again and then to set the value to the bar1 variable.
On the script attached to the button that switch to the first scene i tried something like this:

extends Button

#onready var bar1 = get_node("/root/global")

func _pressed():
    get_tree().change_scene("res://Game.tscn")

    bar1 = get_node("../TextureProgress1").get_value()
    get_node("../TextureProgress1").set_value(bar1)

    #global.bar1(value1)


func _ready():
    # Called every time the node is added to the scene.
    # Initialization here
    pass

I really don’t know what am i doing wrong and i’m a bit stuck at the moment. Do you have any suggestion? Thank you

Kiren | 2018-01-23 19:42

Your code looks suspicious to me.
Try something like this on the button that switches the scenes:

extends Button

var val_bar1 
func _pressed():
    val_bar1 = get_node("../TextureProgress1").get_value()
    GLOBAL.bar1 = val_bar1 #I am asuming your script is stored as GLOBAL in the Autoload section

    get_tree().change_scene("res://Game.tscn")

SingingApple | 2018-01-24 12:35

Hey, thank you for your answer.

Yes i have a global.gb script called global, flagged as singleton in Autoload tab of the Project Settings.

Anyway i got two buttons: one that switch to scene 2 and one to switch back to scene 1. The button in the second scene (that switch to scene 1) is the only thing present in the scene 2, while in the first scene there are every bars and buttons.

I tried your code just now and, the line that contains the code “GLOBAL.bar1 = val_bar1” it returns an error: “Unexpected indent”

Kiren | 2018-01-24 12:51

This might help you: python - Unexpected indent - Stack Overflow

SingingApple | 2018-01-30 11:57

Hello, do you have an example of how I would save a custom created character in a global.gd?

Amateur.game.dev. | 2021-01-14 14:08