trying to convert a vector2 string that was stored in a json file with
str2var("(93, 87)")
The "(" and ")" in the string crash the application
I get this error:
calling built-in function 'str2var': Parse error at line 120945184: Expected value, got '('.
this is a follow up to:
https://github.com/godotengine/godot/issues/11419
Well I gave you a hint in #11419, not a tutorial you should follow blindly ;). Obviously str2var expects a different format than what you're trying, and it's quite easy to find out:
func _ready():
var vector = Vector2(93, 87)
var stringified = var2str(vector)
print(stringified)
print(typeof(stringified) == TYPE_STRING)
var variantified = str2var(stringified)
print(variantified)
print(typeof(variantified) == TYPE_VECTOR2)
print(variantified == vector)
Output:
Vector2( 93, 87 )
True
(93, 87)
True
True
aah I see, sorry. I missed this part completely. I wish this was included in the documentation of the engine :)
Well there's http://docs.godotengine.org/en/stable/classes/[email protected]?highlight=str2var#class-gdscript-str2var
It doesn't tell you exactly what's the expected format for each kind of Variant, but it does tell you that it's what var2str outputs, and as long as you store the output of var2str you don't even need to know what it is. Put var2str(vector) in your JSON, parse it back with str2var.
@akien-mga No I mean in the
parse_json( String json ) and to_json() part of the documentation could also note that json cant store vector2 variables, so when writing them to the dictionary var2str should be used and when reading them it should also be used
It could help avoid people asking the question, and others answering them a broken solution like this:
https://www.reddit.com/r/godot/comments/4t25y1/string_to_vector2/+&cd=1&hl=en&ct=clnk&gl=uk
I opened a feature request here:
https://github.com/godotengine/godot/issues/11866
hope it makes sense
Most helpful comment
Well I gave you a hint in #11419, not a tutorial you should follow blindly ;). Obviously
str2varexpects a different format than what you're trying, and it's quite easy to find out:Output: