+4 votes

This function is capable of converting any string that has gone through json back into a vector2() their by makeing it easyer to store vector2()s in a file, and retrieve them. :)

cords = "(1324, 1654)"
cords.erase(cords.find("("),1)
cords.erase(cords.find(")"),1)
cords.erase(cords.find(","),1)
var x = cords.left(cords.find(" "))
var y = cords.right(cords.find(" "))
cords = Vector2(x,y)

just wanted to let everyone use it because I couldn't find one that was already made.

in Engine by (659 points)

1 Answer

+10 votes

I think this could work equally as well in your use case:

var cords = "(1324, 1654)"
var some_vector = var2str("Vector2" + cords)

Edit: Wouldn't saving the Vector2 to JSON using str2var() be more convenient? This way, the string can be passed directly to var2str() without the need to concatenate it.

by (12,869 points)
edited by

Yes use str2var and var2str:

var coords = vector2(1234, 1654)

func savefile():
file.store
line(var2str(coords))

func load_file
file open etc... to json etc
coords = Vector2(str2var(json["coords"]))

I tried to make string Vector3(1,2,3) into actual Vector3. For me it worked with str2var("Vector3(1,2,3)"). It became Vector3.
Thanks!

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.