So, just before i post this, i found https://github.com/godotengine/godot/issues/11866 that made me fell bad. I belieive this is a problem in the engine, and i didint really understand a straitfoard way to manage that in the link

when running:

i get!

so, the first one is correctly changed to an Array, as it is type 19

but the second one, remains an string

What can be done about it, i need it to become a array, and futhermore, as the issue states, that each element of the array becomes the correct type, in this case, a Vector2.

There is a straithfoard way to do that, is this is really a bug/non implemented thing, thats need a hack to be done? This does not sounds okay.

Any insights?

Thanks

Your second case is not a valid array definition so GDScript has no other choice but to interpret the whole thing just as a string literal. Valid array of vectors would be: [Vector2(1, 2), Vector2(3, 4)] This string will be converted to an array by str2var() without problems.

If you already have a large string in that format you can simply insert "Vector2" using String's replace method:

var s = "[(1,2), (3,4)]"
s = s.replace("(", "Vector2(") )
var a = str2var(s)
print(typeof(a)) # will print 19
print(a[1].x) # will print 3

Thats a odd thing to learn. Thanks again for the help, xyz. When serializing it to binary everthing works, but recovering it from a string, i get in that situation. I think the binary serializatin keep the Vector2 type. The problem is that any convertion and printout will not show the "Vector2(", on any of my outputs. indeed i have much to learn.

I will try out your solution, but i think it will be expensive.

There is a way to force the "Vector2(" prepend to printout in the conversion?

The code:

outputs

so thats create the problem of not having the "Vector2" prepended.

Its the only way to use the replace method to fix that?

Because when serializing in binary via network i got no problems, so the vector2 type must beem kept somehow in the translation, but the print and the put_line does not (i got into this problem when saving to file instead of serializing)

Maybe in the save part i can use your code and in the load i already have everthing formated...

Is this really the best way?

Use var2str() if you need to have 'Vector2' in the string:

var a = [Vector2(1, 2), Vector2(3, 4)]
var s = var2str(a)
print(s)

There's nothing here to "fix" really. You choose the text format of your data when saving it, and write code to properly interpret and load it. Binary serialization does that under the hood.

Thanks. Your snippet works. Before i was simply using str() of course var2str() keep all the relevant information i was missing to process it back using the str2var()

I think i will make a costume to use the var2str() on my print() messages, its a more complete readout of the information. That was what created the confusion.

If only type you need to store is Vector2, you can store the data without that 'Vector2' prefix, because type is self evident (just need to insert it when converting to actual objects). Otherwise, if you have to deal with various types then you'll need to store the type too.

Doing string replacements in inexpensive. Computers are fast :)

a year later