Skip to content

Game.Serialization.IPreSerialize

Assembly: Game
Namespace: Game.Serialization

Type: interface

Base: (none)

Summary:
IPreSerialize is a simple interface used by the game's serialization pipeline to allow objects to perform preparation work immediately before they are serialized. Implement this interface on any type that needs to compute or store transient state, add custom serialized data, or otherwise adjust its state right before the serialization process begins. The Context parameter supplied to PreSerialize comes from Colossal.Serialization.Entities and represents the serialization context for the current operation.


Fields

  • (none)
    This interface declares no fields.

Properties

  • (none)
    This interface declares no properties.

Constructors

  • (none)
    Interfaces do not define constructors.

Methods

  • void PreSerialize(Context context)
    Implement this method to perform any pre-serialization logic. Typical uses:
  • Capture or compute transient values that are not part of the regular object state but must be stored with the serialized data.
  • Write additional custom data into the provided Context (if the Context API exposes writer/metadata APIs).
  • Mark or prepare child objects/resources so they serialize in a consistent state. Notes:
  • The Context type is defined in Colossal.Serialization.Entities (imported via using Colossal.Serialization.Entities;).
  • This method is called by the game's serialization system; do not assume it is invoked on the main thread unless documented elsewhere by the game's API.
  • Keep PreSerialize implementations focused and fast to avoid slowing down the serialization pipeline.

Usage Example

using Colossal.Serialization.Entities;
using Game.Serialization;

public class MyComponent : IPreSerialize
{
    private int transientChecksum;

    // Called by the game's serialization pipeline before this object is serialized.
    public void PreSerialize(Context context)
    {
        // Compute a transient checksum or other derived state
        transientChecksum = ComputeChecksum();

        // Optionally write custom metadata to the Context if available:
        // context.Writer.Write("checksum", transientChecksum);
    }

    private int ComputeChecksum()
    {
        // Example computation
        return /* ... */ 0;
    }
}