I have made a String to Vector2() conversion function

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

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. :slight_smile:

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.

:bust_in_silhouette: Reply From: Calinou

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.

Yes use str2var and var2str:

var coords = vector2(1234, 1654)

func save_file():
file.store_line(var2str(coords))

func load_file
file open etc… to json etc
coords = Vector2(str2var(json[“coords”]))

headingwest | 2020-03-30 02:22

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!

Ingeniou5 | 2022-08-23 01:36