0 votes

For my game, which is a kind of stock/crypto trading simulator, the values for the players wallet and coin amount are stored in variables. I want to implement a feature that saves these to values, and can load them. I've watched and tried a few recent tutorials and none of them have worked. How would I go about saving my variables?

in Engine by (48 points)

1 Answer

0 votes
by (264 points)

I'm having a problem with one of my variables loading. It saves the variable but it doesn't load. I'm not sure why it's not working. All relevant code bits are below:

Variables: (MINIMUM_PRICE and price are important here):

#Constants
const MINIMUM_PRICE = 5.0 # no stock will be priced cheaper than this
const FACTOR = 2.0 # the highest possible price will be 2x your balance
const PROGRESS = 1.0 # adds to noise_progress

#Variables
var balance : int = 10
var coin : int = 0
var price : float = MINIMUM_PRICE
var noise : OpenSimplexNoise
var noise_progress : float = 0.0

Price generation:

func generate_stock_price():
    price = noise_modify 
    price += noise.get_noise_1d(noise_progress) * noise_modify 
    noise_progress += PROGRESS

    if coin >= 1:
        noise_modify  = (balance +10 * (FACTOR / 2.0))
    price = noise_modify 
    price += noise.get_noise_1d(noise_progress) * noise_modify 
    noise_progress += PROGRESS

    pricelabel.update_price(price)
    graph.add_value(price)
    graph.create_graph()

And the saving code:

Saving

func load_level():
    var save_file = File.new()
    if not save_file.file_exists("user://savefile.save"):
        return
    save_file.open("user://savefile.save", File.READ)
    balance = int(save_file.get_line())
    coin = int(save_file.get_line())
    price = int(save_file.get_line())
    print("Loaded")
    save_file.close()

func save_level():
    var save_file = File.new()
    save_file.open("user://savefile.save", File.WRITE)
    save_file.store_line(str(balance))
    save_file.store_line(str(coin))
    save_file.store_line(str(price))
    print("Saved!")
    save_file.close()

It works perfectly fine for me, you probably have some error in your stock price generation.

No errors are coming up, any way I could troubleshoot?

were do you call load_level? (whole script please)

btw, if you want to debug your save-files easy, you can use "res://" instead of "user://". Then the save-files will be shown in your project-files. You are using "lines", so the save-file can be read as a text-file! Just try to open it, and see if you have the desired values. If these are correct, then saving works, and loading is the problem.

Saving works since the file has the correct value, the problem is with loading.
Script is below:

extends Node2D
#Constants
const MINIMUM_PRICE = 5.0 # no stock will be priced cheaper than this
const FACTOR = 2.0 # the highest possible price will be 2x your balance
const PROGRESS = 1.0 # adds to noise_progress

#Variables
var balance : int = 10
var coin : int = 0
var price : float = MINIMUM_PRICE
var noise : OpenSimplexNoise
var noise_progress : float = 0.0

#Onready Variables
onready var pricelabel : Label = $PriceLabel 
onready var walletlabel : Label = $Wallet
onready var coinlabel : Label = $CoinLabel
onready var graph : Line2D = $Graph

var noise_modify : float = (balance * (FACTOR / 2.0))

func _ready():
    load_level()
    randomize()

    noise = OpenSimplexNoise.new()
    noise.seed = randi()

    # --- tweak this to your needs ---
    noise.octaves = 4
    noise.period = 20.0
    noise.persistence = 0.8
    # --------------------------------

func _on_PriceGenerator_timeout():
    generate_stock_price()

func generate_stock_price():
    price = noise_modify 
    price += noise.get_noise_1d(noise_progress) * noise_modify 
    noise_progress += PROGRESS

    if coin >= 1:
        noise_modify  = (balance +10 * (FACTOR / 2.0))
    price = noise_modify 
    price += noise.get_noise_1d(noise_progress) * noise_modify 
    noise_progress += PROGRESS

    pricelabel.update_price(price)
    graph.add_value(price)
    graph.create_graph()

func _on_Timer_timeout():
    generate_stock_price()

func _on_Button_pressed():
    get_tree().change_scene("res://Title Screen/title_screen.tscn")

#Buying
func _on_Buy_pressed():
    if balance < price:
        pass
    else:
        balance = balance - price
        coin = coin + 1

#Selling
func _on_Sell_pressed():
    if coin > 0:
        balance = balance + coin*price
        coin = coin-1
    else:
        pass

#Wallet Updating
func _on_WalletTimer_timeout():
    walletlabel.update_wallet(balance)
    coinlabel.update_coin(coin)
    if balance < 0.01:
        pass

#Saving
func load_level():
    var save_file = File.new()
    if not save_file.file_exists("user://savefile.save"):
        return
    save_file.open("user://savefile.save", File.READ)
    balance = int(save_file.get_line())
    coin = int(save_file.get_line())
    price = int(save_file.get_line())
    print("Loaded")
    save_file.close()

func save_level():
    var save_file = File.new()
    save_file.open("user://savefile.save", File.WRITE)
    save_file.store_line(str(balance))
    save_file.store_line(str(coin))
    save_file.store_line(str(price))
    print("Saved!")
    save_file.close()

func _on_Save_pressed():
    save_level()

func _on_Load_pressed():
    load_level()

I think the problem might be with the way price is generated itself?

looks good to me. Are you sure this does not work?
What makes you think it doesn't work?

If you want the graph to be loaded and saved, then you have to save the points from your graph, too. Ich you do this, you should also save/load the seed and the progress.

The save file contains the exchange rate, which matches the one when you save, but when you load your save the exchange rate doesn't change to the one in the save file, hence the loading of the exchange rate doesn't work. would I have to save the graph too?

lets discuss this on reddit...
I don't think its very reasonable to do this here.

Sure, should I PM you?

Thanks ALOT it worked for me........

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.