0 votes

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 physicsprocess(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 onBoilerProductionUpgradeButton_pressed():
if energy >= HotterBoilersCost and HotterBoilersUpgradeMaxLevel <= 5:

    energy -= HotterBoilersCost
    HotterBoilersCost = HotterBoilersCost * HotterBoilersMultiplier


    UpgradeSave()
    UpgradeLoad()

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

    energy -= PSIMonitoringCost
    PSIMonitoringCost = PSIMonitoringCost * PSIMonitoringMultiplier


    UpgradeSave()
    UpgradeLoad()

func onTurbineProductionUpgradeButton_pressed():

if energy >= FasterTurbinesCost and FasterTurbinesUpgradeMaxLevel <= 5:

    energy -= FasterTurbinesCost
    FasterTurbinesCost = FasterTurbinesCost * FasterTurbinesMultiplier


    UpgradeSave()
    UpgradeLoad()

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

    energy -= SmallerShaftsCost
    SmallerShaftsCost = SmallerShaftsCost * SmallerShaftsMultiplier


    UpgradeSave()
    UpgradeLoad()
Godot version Godot version 3.3.2_stable_win64exe
in Engine by (20 points)

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

1 Answer

0 votes

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

by (16,664 points)
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.