Having some trouble with my vector in c# (Unity)

127 views
Skip to first unread message

Jobe Lloyd

unread,
Mar 4, 2016, 7:38:51 PM3/4/16
to FlatBuffers
I have been trying to serialize and deserialize some test data, but I cant seem to get it to work.  I don't get any errors, but my result is a zero length vector and I am not quite sure what I am doing wrong.  I have been looking through the forums here and the example/tutorial code, but I cant quite seem to figure out my error.  Here is the code simplified:

////Flatbuffer layout

table Foo{
     foo_id:int;
     is_purchasable:bool;
     fooName:string;
}

table Bar{
     data1:int;
     data2:double;
     data3:int;
     data4:long;
}

table StaticData{
     FooVec:[Foo];
     BarVec:[Bar];
}


///C# Code

void FlatBufferSave(List<FooAsAClass> Foos)
    {
        FlatBufferBuilder fbb = new FlatBufferBuilder(1);

        Offset<Foo>[] foosOffset = new Offset<Foo>[Foos.Count];

        for (int i = 0; i < Foos.Count; i++)
        {
            Debug.Log("Adding car: " + i);
            FooAsAClass data = Foos[i];

            //Strings
            StringOffset FooNameOffset = fbb.CreateString(data.DriveType);

            Foo.StartFoo(fbb);
            Foo.AddFooId(fbb, data.FooID);
            Foo.AddIsPurchasable(fbb, data.IsPurchasable);
            Foo.AddFooName(fbb, FooNameOffset);
            var FooOffset = Foo.EndFoo(fbb);

            foosOffset[i] = FooOffset;
        }

        // Pass the foosOffsetarray into the `CreateFooVector()` method to create a FlatBuffer vector.
        VectorOffset foosVectorOffset = StaticData.CreateFooVector(fbb, foosOffset);


        StaticData.StartStaticData(fbb);
        StaticData.AddFooVec(fbb, foosVectorOffset);
        var endStaticData = StaticData.EndStaticData(fbb);

        fbb.Finish(endStaticData.Value);

        var buf = fbb.DataBuffer;

        // Save the data into "SAVE_FILENAME.whatever" file, name doesn't matter obviously
        using (var ms = new MemoryStream(fbb.DataBuffer.Data, fbb.DataBuffer.Position, fbb.Offset))
        {
            File.WriteAllBytes(Application.persistentDataPath + "/" + "SAVE_FILENAME.whatever", ms.ToArray());
        }
    }


void FlatBufferLoad()
    {
        ByteBuffer bb = new ByteBuffer(File.ReadAllBytes("SAVE_FILENAME.whatever"));

        StaticData data = StaticData.GetRootAsStaticData(bb);

        //this is triggered every time
        if(data.FooVecLength == 0)
        {
            Debug.LogWarning("ZERO LENGTH DATA");
        }

    }

Jobe Lloyd

unread,
Mar 4, 2016, 8:02:51 PM3/4/16
to FlatBuffers
And I found my error moments after posting...

Here is the correct code, it was just a file load problem.  I had an old file from earlier on in my development that was getting loaded, so the bug (bad file path) was not throwing an error since there was a zero length file to be loaded.

Hopefully this code will help anyone in the future looking to serialize vectors in C#


        //HERE WAS THE ISSUE, BAD FILE PATH
        ByteBuffer bb = new ByteBuffer(File.ReadAllBytes(Application.persistentDataPath + "/SAVE_FILENAME.whatever"));
Reply all
Reply to author
Forward
0 new messages