Skip to content

Instantly share code, notes, and snippets.

@martindevans
Created August 13, 2012 23:00
Show Gist options
  • Save martindevans/3344658 to your computer and use it in GitHub Desktop.
Save martindevans/3344658 to your computer and use it in GitHub Desktop.
void write(entity e, BinaryWriter w)
{
w.write(e.Template.Id);
foreach (Component c in e.Components) //e.Components is ordered in a specific way when it's constructed
{
Assert(e.Template.Contains(c));
c.Write(w); //Component writes out the data it needs
}
}
Entity Read(BinaryReader r)
{
Template t = Template.GetById(r.ReadInt());
foreach (Action constructorAction in t.Components) //Reflection of constructor is done at template construction time (load time)
{
constructorAction(r); //Component reads the data is needs in the constructor which accepts a BinaryReader
}
}
class ExampleComponent
{
int SomeData;
string ImportantState;
ExampleComponent(int data, string state)
{
SomeData = data;
ImportantState = state;
}
ExampleComponent(BinaryReader r)
:this(r.ReadInt(), r.ReadString())
{
}
override void Write(BinaryWriter w)
{
r.WriteInt(SomeData);
r.WriteString(ImportantState);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment