0 votes

I made a sample save and load system .
I'm aiming to initial basic state( herohp = 0 ) and load the lastest state of my hero( herohp = 46),but it seem not to work like that .
Can somebody tell me why i only have one variables and output result 2 answers?

output
Game Loaded
herohp: 46
herohp: 0

savegame.txt
[hero]
Lv=0
hp=46
mxhp=100
xp=0
mxxp=100

extends Node

var savegame = File.new()
var file_to_save= "res://savegame.txt" 
var heroLv   = 0
var herohp   = 0
var heromxhp = 0
var heroxp   = 0
var heromxxp = 0


func _on_Button_pressed():
if savegame.file_exists(file_to_save):
var configFile= ConfigFile.new()  
configFile.load(file_to_save) 
var heroLv = configFile.get_value("hero", "Lv") 
var herohp = configFile.get_value("hero", "hp") 
var heromxhp = configFile.get_value("hero", "mxhp") 
var heroxp = configFile.get_value("hero", "xp") 
var heromxxp = configFile.get_value("hero", "mxxp") 

print ("Game Loaded") 
print ("herohp: ", herohp) 


func _on_Button3_pressed():

print ("herohp: ", herohp) 
in Engine by (12 points)

Where are you storing your savegame.txt file ?
Following official docs, better to use user:// prefix

hi save system works fine
my problem is how to change variables ? they should only one variables "herohp" and i cant load-in my value(=46)

You only have 1 variable because your only calling one variable?

print ("herohp: ", herohp) 

You wont get multiple entries with only one?

Secondly id advise removing all the vars = 0
You may as well have a default .txt file that pulls the default lvls that are overwrite by the saved txt file

also maybe put this:

var heroLv = configFile.get_value("hero", "Lv") 
var herohp = configFile.get_value("hero", "hp") 
var heromxhp = configFile.get_value("hero", "mxhp") 
var heroxp = configFile.get_value("hero", "xp") 
var heromxxp = configFile.get_value("hero", "mxxp") 

in the func _ready():

Hi thank for your reply,but you seems not understand my question.
my question is i cant update variables from herohp = 0 to herohp = 46
when i pressed loadbutton ,it print herohp = 46 that's good
but than i pressed printbutton it print herohp = 0 again?

I think the problem is because you are creating
a new local variables in the onButton_pressed() function
So just remove the "var" keyword before the variables names
In that function so u could change the global variables
That are outside that function

oh yes~! It works.Thank you very much.
the next quesion of this is how to hold this state when change to other tscn?
when i change to stage2 and call "print(global.herohp)" , it print herohp= 0 again?
is there any deferred game state or something to deal with this?

i hope this answer your question

Hi BxStudent,thank you so much.i'll start reading it.

Please log in or register to answer this question.

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 Frequently asked questions and 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 [email protected] with your username.