#saving and loading arrays

28 messages · Page 1 of 1 (latest)

covert ingot
#

i have 2 arrays, one of integers and one of strings, (for example [10, 20, 30] and ["text", "text2", "text"]) i want to be able to save and load them in my game.
how do i do that? pls keep it as simple as possible, im very new lul)

hybrid bough
#

Use a Resource.

covert ingot
#

whats that?

hybrid bough
covert ingot
#

how do i use that with an array tho? when i tried loading it i got an error

strong verge
#

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()

covert ingot
rocky plover
#

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?

rocky plover
#

so just print out the namesArray when saving and see what it prints out

covert ingot
#

both arrays have data and it prints them

rocky plover
#

even if you call print in the saveScoresNames function?

covert ingot
#

yeah

rocky plover
#

huh weird , can you show where you declare it in that class

covert ingot
rocky plover
#

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

rocky plover
rocky plover
#

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?

strong verge
#

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'