How to convert String to Vector2

Godot Version

4.2.2

Question

I’m making a game that creates a topdown terrain from a noise function, the game then stores all of those values depending on the tilemap tile that the height correlates to. I wanted the player to be able to save and load the worlds that they have created.

The problem that I have run into is that when the data is saved as a json, it has to be converted to a string, so every Vector2 for the tilemap tiles gets converted to a string, but I can’t get it to convert back into a Vector2 to use, how can I go about this.

The stored Tiles get saved sort of like this, but obviously with much more data.

var TerrainStorage = [[(1,0), (2,0), (1,0)], [(1,0), (2,0), (1,0)], [(1,0), (2,0), (1,0)]]

and the data gets returned when loading like this

 [["(1,0)", "(2,0)", "(1,0)"], ["(1,0)", "(2,0)", "(1,0)"], ["(1,0)", "(2,0)", "(1,0)"]]

is there a solution that is simple that I just haven’t though about, or is it something more complicated

You might be able to use str_to_var for each vector, or use a better format for saves.

Heya there! If you’re using JSON.stringify() to turn your data into strings then you can read it with JSON.parse() ~

This is just an example I pulled from one of my projects!

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.