+1 vote

When I serialize PoolVector2Array by to_json function, this array converted to string like this:"[(0, 0), (0, 1), (0, 2), (0, 3)]"

parse_json return this value as String, str2val also return String

Can I convert this value to PoolVector2Array by native Godot function, or I need to parce this string by myself?

in Engine by (16 points)

1 Answer

+1 vote
Best answer

The JSON format standard does not have a data type to specifically store Vector2 values (and Vector3, and Rect2, and AABB, and Color, and poolvectors). So the information will be downgraded to something else, here a string.
You may have to parse the value manually indeed.

I usually parse JSON files manually anyways: before saving, I convert the data into a JSON-compatible dictionary and then save that ; and after loading from JSON I convert back into Godot types. Sometimes I use an automated script to format the data in and out, which works in some cases: https://github.com/Zylann/godot_texture_generator/blob/master/addons/zylann.tgen/util/jsonify.gd
Although, there is no way to differenciate an Array and a PoolIntArray for example since Array can be represented in exactly the same way. Same for int and float, JSON will always give you float, since the JSON standard only has number.
So despite its popularity, JSON is actually quite shitty if you want to work with Godot-specific data types^^"

Another option is to use a schema, i.e a specification of the data types in what you save. This is trivial in C# since you can save a class without embedding type information, and get it back easily since the language is strongly typed. But in GDScript, typing is dynamic so you may rely on a different strategy.

Using store_var and get_var with the File class should properly store Godot types, but it's a binary format.

by (28,882 points)
edited by
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.