Saving And Loading setting my variables to 0

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

Hello. I am having some sort of mysterious issue, where, when using the UpgradeSave function and then afterwards using the UpgradeLoad function, the following variables reset to 0, despite not being set as such initially and despite there not being any code i know of to cause them to become 0:

var HotterBoilersCost
var PSIMonitoringCost
var FasterTurbinesCost
var SmallerShaftsCost

I have done some testing and found that by taking out the saving and loading function when calling the upgradebuttons, the variables/values work as they should, but unfortunately that’s all I know, and I do need to save them. This isnt just one localised function either, it seems that both save functions, if called at ready, reset all value to 0 for no apparent reason. Any help would be appreciated, I apologise I cant really localise the problem, I wouldn’t put all 300 lines of code if I knew roughly what the issue is, but I am completely unaware. I have had to cut out the Load() function, for the sake of the character limit.

extends Control

#Save/Load

var energypath = “user://energy.save”
var sizepath = “user://sizelevel.save”
var powerpath = “user://powerlevel.save”
var speedpath = “user://speedlevel.save”
var boileramountpath = “user://boileramount.save”
var condensoramountpath = “user://condensoramount.save”
var turbineamountpath = “user://turbineamount.save”
var generatoramountpath = “user://generatoramount.save”

var boilercostpath = “user://boilercost.save”
var condensorcostpath = “user://condensorcost.save”
var turbinecostpath = “user://turbinecost.save”
var generatorcostpath = “user://generator.save”

var sizelevelcostpath = “user://sizelevelcost.save”
var powerlevelcostpath = “user://powerlevelcost.save”
var speedlevelcostpath = “user://speedlevelcot.save”

#factory variables

var energy = 0
var energydisplayfactor = 0
var energydisplay

var sizelevel = 0
var powerlevel = 0
var speedlevel = 0

var BoilerAmount = 0
var CondensorAmount = 0
var TurbineAmount = 0
var GeneratorAmount = 0

var BoilerCost = 0
var CondensorCost = 0
var TurbineCost = 0
var GeneratorCost = 0

var sizelevelcost = 0
var powerlevelcost = 0
var speedlevelcost = 0

#UpgradeVariables

var HotterBoilersUpgradeLevel = 0
var PSIMonitoringUpgradelevel = 0
var FasterTurbinesUpgradeLevel = 0
var SmallerShaftsUpgradeLevel = 0

var HotterBoilersUpgradeMaxLevel = 5
var PSIMonitoringUpgradeMaxlevel = 5
var FasterTurbinesUpgradeMaxLevel = 5
var SmallerShaftsUpgradeMaxLevel = 5

var HotterBoilersCost = 800
var PSIMonitoringCost = 1700
var FasterTurbinesCost = 3500
var SmallerShaftsCost = 6300

var HotterBoilersMultiplier = 2.2
var PSIMonitoringMultiplier = 2.3
var FasterTurbinesMultiplier = 2.4
var SmallerShaftsMultiplier = 2.9

#Upgrade Save/Load

var hotterboilerslevelpath = “user://hotterboilerslevel.save”
var psimonitoringlevelpath = “user://psimonitoring.save”
var fasterturbineslevelpath = “user://fasterturbineslevel.save”
var smallershaftslevelpath = “user://smallershaftslevel.save”

var hotterboilerscostpath = “user://hotterboilerslevel.save”
var psimonitoringcostpath = “user://psimonitoring.save”
var fasterturbinescostpath = “user://fasterturbineslevel.save”
var smallershaftscostpath = “user://smallershaftslevel.save”

func _ready():

Load()

func _physics_process(delta):

if energy >= 1000:
	
	energydisplayfactor = 1000
	
elif energy >= 1000000:
	
	energydisplayfactor = 1000000
	
	
print(HotterBoilersCost)

if energy >= 0:
	
	energydisplay = float(HotterBoilersCost) / energydisplayfactor
	$UI/Node/BoilerProductionUpgradeCostLabel.text = "" + str(energydisplay) + "K"
	energydisplay = float(PSIMonitoringCost) / energydisplayfactor
	$UI/CondensorProductionUpgradeButton/CondensorProductionUpgradeCostLabel.text = "" + str(energydisplay) + "K"
	energydisplay = float(FasterTurbinesCost) / energydisplayfactor
	$UI/TurbineProductionUpgradeButton/TurbineProductionUpgradeCostLabel.text =  "" + str(energydisplay) + "K"
	energydisplay = float(SmallerShaftsCost) / energydisplayfactor
	$UI/GeneratorProductionUpgradeLabel/GeneratorProductionLabelCost.text = "" + str(energydisplay) + "K"
	energydisplay = float(energy) / energydisplayfactor
	$EnergyAmountLabel.text = "" + str(energydisplay) + "K"
	
	
elif energy >= 1000000:
	
	energydisplay = float(HotterBoilersCost) / energydisplayfactor
	$UI/Node/BoilerProductionUpgradeCostLabel.text = "" + str(energydisplay) + "M"
	energydisplay = float(PSIMonitoringCost) / energydisplayfactor
	$UI/CondensorProductionUpgradeButton/CondensorProductionUpgradeCostLabel.text = "" + str(energydisplay) + "M"
	energydisplay = float(FasterTurbinesCost) / energydisplayfactor
	$UI/TurbineProductionUpgradeButton/TurbineProductionUpgradeCostLabel.text =  "" + str(energydisplay) + "M"
	energydisplay = float(SmallerShaftsCost) / energydisplayfactor
	$UI/GeneratorProductionUpgradeLabel/GeneratorProductionLabelCost.text = "" + str(energydisplay) + "M"
	energydisplay = float(energy) / energydisplayfactor
	$EnergyAmountLabel.text = "" + str(energydisplay) + "M"

func UpgradeSave():

var hotterboilerslevelfile = File.new()
hotterboilerslevelfile.open(hotterboilerslevelpath, hotterboilerslevelfile.WRITE)
hotterboilerslevelfile.store_64(HotterBoilersUpgradeLevel)
hotterboilerslevelfile.close()

var psimonitoringlevelfile = File.new()
psimonitoringlevelfile.open(psimonitoringlevelpath, psimonitoringlevelfile.WRITE)
psimonitoringlevelfile.store_64(PSIMonitoringUpgradelevel)
psimonitoringlevelfile.close()

var fasterturbineslevelfile = File.new()
fasterturbineslevelfile.open(fasterturbineslevelpath, fasterturbineslevelfile.WRITE)
fasterturbineslevelfile.store_64(FasterTurbinesUpgradeLevel)
fasterturbineslevelfile.close()

var smallershaftslevelfile = File.new()
smallershaftslevelfile.open(smallershaftslevelpath, smallershaftslevelfile.WRITE)
smallershaftslevelfile.store_64(SmallerShaftsUpgradeLevel)
smallershaftslevelfile.close()

var hotterboilerscostfile = File.new()
hotterboilerscostfile.open(hotterboilerscostpath, hotterboilerscostfile.WRITE)
hotterboilerscostfile.store_64(HotterBoilersCost)
hotterboilerscostfile.close()

var psimonitoringcostfile = File.new()
psimonitoringcostfile.open(psimonitoringcostpath, psimonitoringcostfile.WRITE)
psimonitoringcostfile.store_64(PSIMonitoringCost)
psimonitoringcostfile.close()

var fasterturbinescostfile = File.new()
fasterturbinescostfile.open(fasterturbinescostpath, fasterturbinescostfile.WRITE)
fasterturbinescostfile.store_64(FasterTurbinesCost)
fasterturbinescostfile.close()

var smallershaftscostfile = File.new()
smallershaftscostfile.open(smallershaftscostpath, smallershaftscostfile.WRITE)
smallershaftscostfile.store_64(SmallerShaftsCost)
smallershaftscostfile.close()

func UpgradeLoad():

var hotterboilerslevelfile = File.new()
hotterboilerslevelfile.open(hotterboilerslevelpath, hotterboilerslevelfile.WRITE)
HotterBoilersUpgradeLevel = hotterboilerslevelfile.get_64()
hotterboilerslevelfile.close()

var psimonitoringlevelfile = File.new()
psimonitoringlevelfile.open(psimonitoringlevelpath, psimonitoringlevelfile.WRITE)
PSIMonitoringUpgradelevel = psimonitoringlevelfile.get_64()
psimonitoringlevelfile.close()

var fasterturbineslevelfile = File.new()
fasterturbineslevelfile.open(fasterturbineslevelpath, fasterturbineslevelfile.WRITE)
FasterTurbinesUpgradeLevel = fasterturbineslevelfile.get_64()
fasterturbineslevelfile.close()

var smallershaftslevelfile = File.new()
smallershaftslevelfile.open(smallershaftslevelpath, smallershaftslevelfile.WRITE)
SmallerShaftsUpgradeLevel = smallershaftslevelfile.get_64()
smallershaftslevelfile.close()

var hotterboilerscostfile = File.new()
hotterboilerscostfile.open(hotterboilerscostpath, hotterboilerscostfile.WRITE)
HotterBoilersCost = hotterboilerscostfile.get_64()
hotterboilerscostfile.close()

var psimonitoringcostfile = File.new()
psimonitoringcostfile.open(psimonitoringcostpath, psimonitoringcostfile.WRITE)
PSIMonitoringCost = psimonitoringcostfile.get_64()
psimonitoringcostfile.close()

var fasterturbinescostfile = File.new()
fasterturbinescostfile.open(fasterturbinescostpath, fasterturbinescostfile.WRITE)
FasterTurbinesCost = fasterturbinescostfile.get_64()
fasterturbinescostfile.close()

var smallershaftscostfile = File.new()
smallershaftscostfile.open(smallershaftscostpath, smallershaftscostfile.WRITE)
SmallerShaftsCost = smallershaftscostfile.get_64()
smallershaftscostfile.close()

func _on_BoilerProductionUpgradeButton_pressed():
if energy >= HotterBoilersCost and HotterBoilersUpgradeMaxLevel <= 5:

	energy -= HotterBoilersCost
	HotterBoilersCost = HotterBoilersCost * HotterBoilersMultiplier
	
	
	UpgradeSave()
	UpgradeLoad()

func _on_CondensorProductionUpgradeButton_pressed():
if energy >= PSIMonitoringCost and PSIMonitoringUpgradeMaxlevel <= 5:

	energy -= PSIMonitoringCost
	PSIMonitoringCost = PSIMonitoringCost * PSIMonitoringMultiplier
	
	
	UpgradeSave()
	UpgradeLoad()

func _on_TurbineProductionUpgradeButton_pressed():

if energy >= FasterTurbinesCost and FasterTurbinesUpgradeMaxLevel <= 5:
	
	energy -= FasterTurbinesCost
	FasterTurbinesCost = FasterTurbinesCost * FasterTurbinesMultiplier
	
	
	UpgradeSave()
	UpgradeLoad()

func _on_GeneratorProductionUpgradeLabel_pressed():
if energy >= SmallerShaftsCost and SmallerShaftsUpgradeMaxLevel <= 5:

	energy -= SmallerShaftsCost
	SmallerShaftsCost = SmallerShaftsCost * SmallerShaftsMultiplier
	
	
	UpgradeSave()
	UpgradeLoad()

Are you sure the variable types match? I.e. are you trying to save a float into an int variable?

Gluon | 2022-06-21 19:58

:bust_in_silhouette: Reply From: jgodfrey

That’s a lot of “not properly formatted for the forum” code to look through. While there could be other problems lurking in there somewhere, I’d bet the main issue is that your code intended for loading the data (UpgradeLoad() is opening all files in WRITE mode.

As stated in the docs, that’ll create the file if it does not exist and truncate it if it does. So, I assume you’re effectively blanking out your storage file each time you attempt to load its contents.

You probably want to change the open mode to READ.

https://docs.godotengine.org/en/stable/classes/class_file.html?highlight=file#enum-file-modeflags