Question about saving/loading string variables

Hi!

A quick question here. I am trying to come up with a save/load system for my game. Everything works fine so long as I work with boolean variables. However, I get errors if I save a string variable and try to load.

This is my code:

var save_path = “user://save_game.save”

func save():
var file = FileAccess.open(GlobalVars.save_path, FileAccess.WRITE)
file.store_var(GlobalVars.button_date1_text) #this is a string
file.store_var(GlobalVars.button_date2_text) #this is a string
file.store_var(GlobalVars.button_date3_text) #this is a string
file.close()

func load():
if FileAccess.file_exists(GlobalVars.save_path):
var file = FileAccess.open(GlobalVars.save_path, FileAccess.READ)
GlobalVars.button_date1_text = file.get_var(GlobalVars.button_date1_text)
GlobalVars.button_date2_text = file.get_var(GlobalVars.button_date2_text)
GlobalVars.button_date3_text = file.get_var(GlobalVars.button_date3_text)

And this is the error I get:

Invalid type in function ‘get_var’ in base ‘FileAccess’. Cannot convert argument 1 from String to bool.

(my indents look ok on my screen but for some reason the message does not display them properly)

Thanks!

To keep formatting when posting, surround the text with 3 ticks on either side:

```
func _ready():
var x = 5
print(x)
```

becomes:

func _ready():
	var x = 5
	print(x)

To answer your question though, get_var does not take arguments. Instead (and this has caused me pain before), just call get_var() repeatedly, and it will spit out the vars you saved in the same order they were saved in.

1 Like

Thanks very much for your answer. I’ll try later today.

I think get_var does take arguments (at least the Boolean ones). It seems to do so for me anyway. The problem comes when you try to load other types of variables. Maybe I didn’t describe my problem entirely correctly.

The boolean parameter of the get_var function only activates object deserialization during loading. In the case of saving and restoring the game state using strings, there is no reason to use this parameter.

2 Likes

This seems to work. Thanks!

1 Like