How to load and change my variables?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By gamermakerchen

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) 

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

GameVisitor | 2018-09-20 15:48

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)

gamermakerchen | 2018-09-20 16:19

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

Archtects | 2018-09-21 14:33

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?

gamermakerchen | 2018-09-22 14:17

I think the problem is because you are creating
a new local variables in the _on_Button_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

BxStudent | 2018-09-23 06:54

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?

gamermakerchen | 2018-09-23 16:29

i hope this answer your question

BxStudent | 2018-09-24 02:20

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

gamermakerchen | 2018-09-24 14:03