Po0ka

Member
I basically just need advices on how to serialize/deserialize a collection the right way.
Theorically i have an ArrayList<StuffClass>, it contains some "StuffClass" that i add/modify/delete from the list (easy stuffs).
The hard stuffs is when i want to serialize it.
My StuffClass contains: one "Type", two integers and a boolean.

1: Should i do something close to faction persistance?
ex:
Code:
List<Faction> factions = Faction.Factions;
for ( int i = 0; i < factions.Count; ++i )
{
	writer.WriteEncodedInt( (int) PersistedType.Faction );
	factions[i].State.Serialize( writer );
}
If so the deserialize is kind of crappy:
Code:
PersistedType type;
while ( (type = (PersistedType)reader.ReadEncodedInt()) != PersistedType.Terminator )
{
	switch ( type )
	{
		case PersistedType.Faction: new FactionState( reader ); break;
		case PersistedType.Town: new TownState( reader ); break;
	}
}

2: Can we just "save" the collection aswell as it's content in one shot?

3: Can we save a class directly? (I would do a for loop and serializing each class)

Notes:
External files (XML, txt files, etc.) is not a valid option.

Thanks for any direction or small code samples.
 
Your serialize and deserialize are 2 seperate objects (Serialize is Faction, Deserialize is FactionState) so I don't really understand what you are trying to do, but generally you serialize as follows.

Write:
Code:
List<Faction> factions = Faction.Factions;
writer.Write((int)factions.Count);
for ( int i = 0; i < factions.Count; ++i )
{
    writer.WriteEncodedInt( (int) factions.FactionType ); // You will need this on the faction to know what type to new up on read
    factions[i].State.Serialize( writer );
}

Read:
Code:
int count = reader.ReadInt32();
for ( int i = 0; i < count; ++i )
{
    PersistedType factionType = (PersistedType)reader.ReadEncodedInt();
    Faction faction = null;
   switch ( factionType )
   {
       case PersistedType.Faction: faction = new FactionState( reader ); break;
       case PersistedType.Town: faction = new TownState( reader ); break;
   }
    Faction.Factions.Add(faction);
}
 
Last edited:

Active Shards

Donations

Total amount
$0.00
Goal
$1,000.00
Back