TVector3 to string and/or textfile


How can I convert Vector3 values to a string for output to a textfile?
(the idea is to store values of a scene position in a file).

PartXYZ := Vector3(-95, -522, 100);

As simple as PartXYZ.ToString (or PartXYZ.ToRawString if you need it to be precise but not necessarily human-readable). See Castle Game Engine: CastleVectorsInternalSingle: Packed Record TGenericVector3

Ah yes.

Writeln (FillDBasefile, PartXYZ[I].ToString);

(It now stores the vector values in a textfile on a single line in -95.00 -522.00 100.00)

I also should have asked how to read it back then from the file or convert it back from string to Vector3;

Readln(FillDBasefile, PartXYZ[I]); (does not work and there is no stringto.vector3);


It’s Vector3FromStr function, see more here Castle Game Engine: CastleVectors

Thanks. I find it hard to find the right things in API and there are not many examples.

Now I get an`“I is an invalid float” error on running.

TempValue: array[1..9] of string; 

   For I := 1 to 9 do
      Readln (FillDBasefile, TempValue[I]);
      ViewMain.NPC[NPC_Selected].partXYZ[I] := Vector3FromStr(TempValue[I]);

I guess it has to do with the remarks on Vector3FromStr

Each component is simply parsed by StrToFloatDot, and components must be separated by whitespace

I have no examples on that.
And then I want to use it like

PartXYZ[I].Y := Max(-1000.0, PartXYZ[I].Y - 15);

Note, that it can happen that you read something weird here. Try logging what is the content of TempValue[I]. E.g. it complains about I - a letter, while it should be something like 0.01 0.03 3.21. Maybe you are saving somewhere literally “I” instead of i-th value of an array?
For me this parsing thing never works “right out of the box” (most often I forget what exactly parameters of Copy mean) however it’s always easy to debug it if you log/show what values you get at every stage.

Ah, that’s it. The wrong line was read after loading the textfile so I removed all lines and arrays and just saved and read the line that is saved in the textfile:

-95.00 -522.00 100.00

and now it works.
Now I have to reset all coordinates of the X,Y and Z properties but I get no errors anymore and the scenes are visible. :slight_smile:


1 Like