there doesn't seem to be a dedicated method for this in the FileAccess docs and File.store_var(dictionary) didn't work.

for context, im making a minecraft like game, and each chunk uses a BlockDictionary to store the blocks location and type in the world:
BlockDict[blockposition] = {"BlockName" : "test" , "VisibleFaces" : [0,0,0,0,0,0]}
so the chunk uses the position of the block to determine its visible faces and name and then spawns it at said location. after all blocks spawn,the chunk runs a function and creates a single collision shape that contains all blocks. in order to save the chunk, naturally i need to save the BlockDict of that chunk. now i tried using the RescourceSaver to do this by creating a ChunkData Class as a rescource, but calling CD = ResourceLoader.load(Savepath).duplicate(true) for every chunk significantly reduced chunk generation speed. so i then gave the world a WorldData class and stored the CD of each chunk in that and then saved the WorldData instead. this helped the game by only calling load and save once, BUUT, when i walked around a bit and had more chunks generated, Calling ResourceSaver.save(Wd, Savepath) Crashed the game, and when it did not crash, it would take more than a few minutes to save and the file size would exceed 50 megabytes, which is not ideal.

i need a way to either store the WorldData in one file without crashing the game and creating a ridiculously big file, or store the ChunkData for each chunk separately, without it causing much lag when loading and saving during runtime.

im pretty sure Json files wont solve the issue since they store the files as strings so its not any better than rescourcesaver. but im stumped in finding a way to use binary serializing for a dictionary, because how can i tell it which key corresponds with what blockname and visible face array? do i have to make a separate file for each item in the dictionary?

this is the last thing i need to finish my world generator, which i have been working on for the past 2 months.

thank you and have a good day.

Edit: File.store_var(dictionary) does create a file, but there is no way to retrieve it as a dictionary and File.get_as_string() returns null. i have no idea what to do with it.

  • xyz and DaveTheCoder replied to this.
  • Rose19 File.store_var(dictionary) didn't work.

    I can confirm that store_var() and get_var() work with Dictionaries (and Arrays and all the other basic types).

    Rose19 Are you sure you need a dictionary here? You could just store raw binary data in some custom format.

    Btw. get_var() is the loading counterpart of store_var()

    Rose19 File.store_var(dictionary) didn't work.

    I can confirm that store_var() and get_var() work with Dictionaries (and Arrays and all the other basic types).

      DaveTheCoder WHAT HOW? show me the code, cuz when im trying to load it it says this:
      Trying to assign value of type 'EncodedObjectAsID' to a variable of type 'WorldData.gd'.

      what am i doing wrong?
      func SaveWorld():
      var file = FileAccess.open("user://save_game.dat", FileAccess.WRITE)
      file.store_var(Wd)

      func LoadWorld():
      var file = FileAccess.open("user://save_game.dat", FileAccess.READ)
      Wd = file.get_var()

      EDIT: NEVERMIND IM DYSLEXIC.

      here is the correct code:

      func SaveWorld():
      var file = FileAccess.open("user://save_game.dat", FileAccess.WRITE)
      file.store_var(Wd.WorldDict)

      func LoadWorld():
      var file = FileAccess.open("user://save_game.dat", FileAccess.READ)
      Wd.WorldDict = file.get_var()

      this worked cuz its the dictionary inside the resource that gets saved not the resource itself. thanks for confirming it.

      Where and how is the variable Wd declared and initialized?

      Also, you should check whether the FileAccess.open() calls are successful before attempting to use methods such as store_var() and get_var().