#saving and loading arrays
28 messages · Page 1 of 1 (latest)
Use a Resource.
whats that?
Nodes and resources: Up to this tutorial, we focused on the Node class in Godot as that's the one you use to code behavior and most of the engine's features rely on it. There is another datatype th...
how do i use that with an array tho? when i tried loading it i got an error
I found the simplest way to save an array was with FileAccess: ```
FileAccess.open(path, FileAccess.READ_WRITE).store_var(your_variable)
To load it again, just do ```
FileAccess.open(path, FileAccess.READ_WRITE).get_var()
when i do this, i get Cannot call method 'store_var' on a null value.
could you show code? and did you try out to see why it is a null value, since hat means it is an empty valu?
so just print out the namesArray when saving and see what it prints out
both arrays have data and it prints them
even if you call print in the saveScoresNames function?
yeah
huh weird , can you show where you declare it in that class
first of alll when saving you just want to write, when loading you just want to read, and go like var file = FileAccses.open(res..., ...Read)´
than just go , file.getVar
same in the save one your opening the same file twice, and yu are probably overwriting so it is just savin one array i would guess
in the end ov every methode you also want to file.close
probably tells you that the line scores array get var in loading is null?
so like this?
no
namesArray = file.get_Var()
scoresArray=file.get_Var()
for load
and do not forget to do the closing line for the files in both methodes
important with this methode of saving is that, you always gotta load in the same order as saving, couse it is baisiccly saving your input in line and by reading it with get var you are reading line by line
worked out?
The save path has to be different for both of them, otherwise one will simply overwrite the other. If you put a breakpoint on the first line in the save method, are the arrays null? (You can look either in the bottom panel or by hovering over the names). Also, if you are trying to store a single score for a single name, I suggest using a Dictionary instead:```swift
var scores = {
"test1": 250,
"test2": 100,
"test3": 20,
"test4": 0,
}
to get a score out of the array by name
scores[name]
print(scores["test1"]) # prints '250'
print(scores["test3"]) # prints '20'