0 votes

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.

in Engine by (32 points)

1 Answer

+1 vote

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.

by (416 points)

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

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():

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


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

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

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


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"

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

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.