#Parsing vector3

1 messages · Page 1 of 1 (latest)

shell hill
#

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

solemn edge
#

Which line gives you the index out of range?

neat onyx
#

Debug.Log the input and see what it tries to parse

shell hill
#

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

solemn edge
#

Because your interpretation of my pseudo code is good 👍

#

You do logger.addLINE, what does it say in the log then?

shell hill
#

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

solemn edge
#

Yeah, or you have an empty line at the end

#

That has no commas in it

shell hill
#

yeah i think i have the last line of the replayfile that is empty

solemn edge
#

That would cause an error for sure

shell hill
#

how i can ignore that?

solemn edge
#

Make sure each line is longer then 10 characters

#

Or something similar

#

Else break

neat onyx
#

Check the length of all the arrays

#

if linesplitted doesn't have two elements, continue

shell hill
#

oh yeah, that's sound good

neat onyx
#

if commasplitted doesn't have 3, etc

solemn edge
#

That is a nicer way yeah

shell hill
#

thank you guys :3, let me try

solemn edge
shell hill
#

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());
        }


    }
solemn edge
#

You need to but it between those lines, linesplitted[0] does not exist at the end, there is nothing to split

shell hill
#

aah ok

#

yeah

#

i'm stupid xD

solemn edge
#

Or maybe 0 exist, but [1] most certainly does not

#

No, you are learning 👍

shell hill
#

if (word.Split('\t')[1] == null)
{
break;
}
else
{
logger.addLINE("test");
linesplitted = word.Split('\t');
}

like this?

neat onyx
#

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

shell hill
#

oh ok, let me try

#

that's perfect, now is working ty ❤️

solemn edge
shell hill
#

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?

neat onyx
#

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

shell hill
#

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

shell hill
#

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);
}