#Parsing vector3
1 messages · Page 1 of 1 (latest)
So, i made something like this
try
{
foreach (string word in data.Split('\n'))
{
string[] linesplitted = word.Split('\t');
string[] commasplitted = linesplitted[0].Split(',');
string[] rotationcommasplitted = linesplitted[1].Split(',');
x = float.Parse(commasplitted[0]);
y = float.Parse(commasplitted[1]);
z = float.Parse(commasplitted[2]);
rx = float.Parse(rotationcommasplitted[0]);
ry = float.Parse(rotationcommasplitted[1]);
rz = float.Parse(rotationcommasplitted[2]);
rw = float.Parse(rotationcommasplitted[3]);
logger.addLINE("Position: " + x.ToString() + y.ToString() + z.ToString() + "Rotation: " + rx.ToString() + ry.ToString() + rz.ToString() + rw.ToString());
}
}
catch (Exception e)
{
object[] parameters = new object[]
{
e.ToString(),
""
};
FengGameManagerMKII.instance.photonView.RPC("Chat", PhotonTargets.All, parameters);
logger.addLINE(e.ToString());
}
but i'm getting a index out of range exception
Parsing vector3
i barely do parsing, so i'm kinda confused
Which line gives you the index out of range?
Debug.Log the input and see what it tries to parse
i can't really do Debug.Log since i'm not using unity editor, i'm making a mod for an existing game, tbh i'm not kinda sure where i get the exception
Because your interpretation of my pseudo code is good 👍
You do logger.addLINE, what does it say in the log then?
problaby i'm getting it foreach (string word in data.Split('\n')) <- here, since if i do break; i get no error the first time
That would cause an error for sure
how i can ignore that?
Check the length of all the arrays
if linesplitted doesn't have two elements, continue
oh yeah, that's sound good
if commasplitted doesn't have 3, etc
That is a nicer way yeah
thank you guys :3, let me try

mhhh i'm still getting the exception
public void ReadFile()
{
string data = File.ReadAllText(Application.dataPath + "/UserData/Replays" + "/" + "Replay1.txt");
try
{
foreach (string word in data.Split('\n'))
{
string[] linesplitted = word.Split('\t');
string[] commasplitted = linesplitted[0].Split(',');
string[] rotationcommasplitted = linesplitted[1].Split(',');
if(linesplitted.Length < 2)
{
break;
}
if (commasplitted.Length < 3)
{
break;
}
if (rotationcommasplitted.Length < 4)
{
break;
}
x = float.Parse(commasplitted[0]);
y = float.Parse(commasplitted[1]);
z = float.Parse(commasplitted[2]);
rx = float.Parse(rotationcommasplitted[0]);
ry = float.Parse(rotationcommasplitted[1]);
rz = float.Parse(rotationcommasplitted[2]);
rw = float.Parse(rotationcommasplitted[3]);
logger.addLINE("Position: " + x.ToString() + y.ToString() + z.ToString() + "Rotation: " + rx.ToString() + ry.ToString() + rz.ToString() + rw.ToString());
}
}
catch (Exception e)
{
object[] parameters = new object[]
{
e.ToString(),
""
};
FengGameManagerMKII.instance.photonView.RPC("Chat", PhotonTargets.All, parameters);
logger.addLINE(e.ToString());
}
}
You need to but it between those lines, linesplitted[0] does not exist at the end, there is nothing to split
if (word.Split('\t')[1] == null)
{
break;
}
else
{
logger.addLINE("test");
linesplitted = word.Split('\t');
}
like this?
Just do what you did earlier but move the length check two lines earlier
and probably use continue instead of break in case the bad line isn't at the end
since i will have problaby to store even more info (like animation status etc) i should try to use serializzation instead of saving it into a txt?
What you're now doing is serialization too, but if you used a standard format (like JSON) you wouldn't need to make your own parser
the main problem is that i tried to do a JSON Serializer but it didn't work
since i can't use the unity default one
mhh idk why but it doesn't save all the replay
public void Savedata2()
{
for (int i = 0; i < recordingQueue.Count; i++)
{
var Replaydata2 = recordingQueue.Dequeue();
Vector3 Position = Replaydata2.position;
Quaternion Rotation = Replaydata2.rotation;
Savedata.SaveReplayData(Position, Rotation);
}
Savedata.Serialize();
}
public void SaveReplayData(Vector3 position, Quaternion rotation)
{
final = final + ($"{position.x},{position.y},{position.z}\t{rotation.x},{rotation.y},{rotation.z},{rotation.w}\n");
}
public void Serialize()
{
byte[] data = Encoding.ASCII.GetBytes(final);
if (!Directory.Exists(Application.dataPath + "/UserData/Replays"))
Directory.CreateDirectory(Application.dataPath + "/UserData/Replays");
File.WriteAllBytes(Application.dataPath + "/UserData/Replays" + "/" + "Replay1.txt", data);
}