|
|
|
 |
Reply From: |
RazorSh4rk |
https://forum.godotengine.org/70637/read-and-save-arrays-solved
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()
CatRass | 2020-06-01 01:05
It works perfectly fine for me, you probably have some error in your stock price generation.
RazorSh4rk | 2020-06-01 13:56
No errors are coming up, any way I could troubleshoot?
CatRass | 2020-06-01 22:49
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.
whiteshampoo | 2020-06-02 15:00
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?
CatRass | 2020-06-02 23:02
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.
whiteshampoo | 2020-06-03 05:31
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?
CatRass | 2020-06-03 05:42
lets discuss this on reddit…
I don’t think its very reasonable to do this here.
whiteshampoo | 2020-06-03 10:41
Sure, should I PM you?
CatRass | 2020-06-03 10:43
Thanks ALOT it worked for me…
RRocky123 | 2020-10-25 07:54