Dictionary shows error when i try to load

Godot Version

4.2.2

Question

Hello! I tried loading a dictionary, and i see why it caused an error. but i dont know how to fix it the code:
func load_skins():
if FileAccess.file_exists(save_path_skins): var file = FileAccess.open(save_path_coins, FileAccess.READ) skins = file.get_var(skins)
heres the error: Invalid type in fun function ‘get_var’ in base ‘FileAccess’. Cannot convert arguement 1 from dictionary to bool.

file.get_var accepts a bool argument, if you are saving “objects” use true, otherwise you can omit the argument or put false

Try

skins = file.get_var()
1 Like

i tried, but it doesnt work. heres the variable:var skins = {"freddy" : false, "mat" : true, "don" : false, "miki":false} and another part of the script where the new error showed up:`func on_select_miki_pressed():
if skins[“miki”] == true:
$“/root/CharSelect”.character = "miki
"
elif skins[“miki”] == false:
if coins >= 50:
coins -= 50
skins[“miki”] = true
save_skins()

or i just use multiple variables. that would work.

Could you show how you are saving this variable? The save_skins function code?

Use it and try:

if skins.has(“miki”):
  if skins[“miki”]:
    $“/root/CharSelect”.character = "miki"
  elif coins >= 50:
    coins -= 50
    skins[“miki”] = true
  save_skins()

I recommend using this library for saving and loading, it’s already been tested and works well, you can use it instead of load_skins and save_skins:

And still check your save_path_skins, maybe it’s wrong too.

Try this:

func load_skins():
     if not FileAccess.file_exists(save_path_skins):
         return
     
     var file = FileAccess.open(save_path_skins, FileAccess.READ)
     var fileContent = file.get_as_text()
     skins = JSON.parse_string(fileContent)
     file.close()

func save_skins():
     var file = FileAccess.open(save_path_skins, FileAccess.WRITE)
     file.store_string(JSON.stringify(skins))
     file.close()

Maybe you forgot convert skins to JSON format for save.

i tried it, and its says: funktion “parse_json()” not found in base self

Sorry, use JSON.stringify(skins) at save_skins function (instead of to_json(skins)) and JSON.parse_json(fileContent) at load_skins function (instead of parse_json(fileContent)).

1 Like

now it says: satic function “parse_json()” not found in base “GDScriptNativeClass”.

im just gonna use variables. but thanks for trying. i just like the longer but smpler way

Sorry, my second mistake :sweat_smile:. Write JSON.parse_string(fileContent) at func load_skins(): instead of JSON.parse_json(fileContent).

i already tried this, and nothing works

Can you insert the contents of the file you are loading from?

Looking into your original post.
Looks like you are checking if save_path_skins file exists, but then you Open an read from save_path_coins.
Have you corrected that? because in that case you are reading froma different file with, most likely, a different format.