This is good to know. However, if I switch to extends Resource, then I cannot access get_tree(), which I need to change scenes and save the new scene as current.
Resolving save and load
- Edited
If you are only saving integers, then you should use store_64(). I think store_var() is for classes.
file.store_64(air)
file.get_64(air)
64() is a double- but it would still work. It will simply take up more space. Int size is 32.
I switched all the get_var() to get_64. I can now open the game without constantly restarting, but my character is never reset- and it goes to player_0 by default, which does not exist. (The player characters start at 1).
Thanks for the help so far, tho. While it hasn't fixed it yet, I do appreciate the efforts as I am terribly lost.
Ah, what the heck.
This seems to work fine for me (based on your earlier post). Of course, I can't speak to any issues with scenes.
extends Node2D
# I haven't mastered arrays and dictionaries yet. My code sort of does the same thing still after rearranging the variables.
var file
var air: float
var bullets: int
var tanks: int
var lives: int
var health: float
var player: int
var level
var var_names := {
player = 1, # player must come first.
air = 1,
bullets = 10,
tanks = 1,
lives = 0,
health = 1,
level = 1,
}
func save_data():
file = File.new()
file.open("user://save_file.file", File.WRITE)
for n in var_names.keys():
file.store_var(get(n))
file.close()
func load_data():
file = File.new()
if file.file_exists("user://save_file.file"):
file.open("user://save_file.file", File.READ)
for n in var_names.keys():
set(n, file.get_var())
file.close()
else:
for n in var_names.keys():
set(n, var_names[n])
save_data()
func start_game():
save_data()
# get_tree().change_scene("res://Maze" + str(level + 1) +".tscn")
print('change maze %d ' % [level + 1])
load_data()
func _ready():
load_data()
func reset():
file = File.new()
file.open("user://save_file.file", File.READ)
player = file.get_var() # The first variable will be player.
file.close()
for n in var_names.keys():
set(n, var_names[n])
save_data()
# get_tree().change_scene("res://Maze" + str(level) +".tscn")
print('change maze %d' % [level])
func print_data():
for n in var_names.keys():
print('%s: %s' % [n, str(get(n))])
So... why is it acting all weird on my end?
As Michael Corleone said to Moe Greene, "you're unlucky."
- Edited
- Best Answerset by Nerdzmasterz
Actually, I made a mistake there. Try this instead.
extends Node2D
# I haven't mastered arrays and dictionaries yet. My code sort of does the same thing still after rearranging the variables.
const FILE_NAME = "user://save_file.file"
var file
var air: float
var bullets: int
var tanks: int
var lives: int
var health: float
var player: int
var level
var var_names := {
player = 1, # player must come first.
air = 1,
bullets = 10,
tanks = 1,
lives = 0,
health = 1,
level = 1,
}
func save_data():
file = File.new()
file.open(FILE_NAME, File.WRITE)
for n in var_names.keys():
file.store_var(get(n))
file.close()
func load_data():
file = File.new()
if file.file_exists(FILE_NAME):
file.open(FILE_NAME, File.READ)
for n in var_names.keys():
set(n, file.get_var())
file.close()
else:
for n in var_names.keys():
set(n, var_names[n])
save_data()
func start_game():
save_data()
# get_tree().change_scene("res://Maze" + str(level + 1) +".tscn")
print('change maze %d ' % [level + 1])
load_data()
func _ready():
load_data()
func reset():
# reset the variables, then load player over them.
for n in var_names.keys():
set(n, var_names[n])
file = File.new()
file.open(FILE_NAME, File.READ)
player = file.get_var() # The first variable will be player.
file.close()
save_data()
# get_tree().change_scene("res://Maze" + str(level) +".tscn")
print('change maze %d' % [level])
func print_data():
for n in var_names.keys():
print('%s: %s' % [n, str(get(n))])
Oh my gosh, you saved me there. I'm going to study this code. Thank you!
Very quick, though. Suppose I need to change a variable, or add one? Ie, I want to add a max_bullets variable to represent the amount of bullets the player had by the time they started the new level. At this stage, all the enemies will respawn and not the ammunition. Or, I want to add a keys variable to show the player they have a way to unlock a door?
+ var max_bullets:int
var var_names := {
player = 1, # player must come first.
air = 1,
bullets = 10,
tanks = 1,
lives = 0,
health = 1,
level = 1,
+ max_bullets = 1000,
}
I'm going to have to change the section on saving in my tut now. It took me a bit, but I found a way to only save data at given times-which is a little different than what we see here. This method is useful to avoid continuing a level with extra/missing keys and bullets, damage dealt, oxygen missing in your air tank, and whatever from the last time you played that level. It should only save after you go to a new level, so you can restart each time you try that level again.
Again, thank you so much for the help!