Save and Load 2D array from file?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By ondesic

So I wish Godot supported 2D arrays. Anyway, Is there an easy way to save an array to file and then load it back into in an array variable?

if the file could be readable, that would be great, like

[[1,1,1,1],[1,0,0,1],[1,0,0,1],[1,1,1,1]]

Then load that into an array[4][4].

I know I can do this with splitting the string and doing a FOR loop, I just wondered if there was a built in way to do it.

:bust_in_silhouette: Reply From: coffeeDragon

I would suggest using the JSON object, see: JSON — Godot Engine (3.0) documentation in English
Just check the type of the parsed array,
in my experience numbers tend to be read as numbers.

are there any downsides to using a JSON? Is there any hit on performance?

ondesic | 2018-04-04 21:22

Good side of JSON is, it’s a well-known human-readable format that is easy to parse in many languages, and often used to communicate with web services.

The bad side of JSON is, it’s limited to a strict set of data types (you don’t have vectors, colors, dictionaries with number keys or typed objects for example) and it’s slower to load than binary files.

If you look for performance and don’t care about human-readability, you can save a binary file using store_var and get_var from the File API: File — Godot Engine (3.0) documentation in English

Zylann | 2018-04-05 01:23