Skip to content

Game.Serialization.HouseholdSystem

Assembly: Assembly-CSharp
Namespace: Game.Serialization

Type: class

Base: GameSystemBase, IPostDeserialize

Summary:
HouseholdSystem is a game system responsible for cleaning up "moving in" household entities when deserializing older save versions. It builds an EntityQuery that selects plain Household entities that are not tourists, commuters, currently moving away, deleted, or temporary. During PostDeserialize it checks each matched household and marks it Deleted if it was not actually marked as moved-in and it has no valid PropertyRenter (i.e., no associated property). The system is annotated with Preserve attributes to ensure it survives stripping/AOT scenarios used by the game.


Fields

  • private Unity.Entities.EntityQuery m_MovingInHouseholdQuery
    Used to select Household entities that may represent households that were left in a "moving in" state from older save files. The query includes Household and excludes TouristHousehold, CommuterHousehold, MovingAway, Deleted and Temp components so only ordinary households that are candidates for cleanup are considered.

Properties

  • This type exposes no public properties.
    (Everything is managed via the EntityQuery and system lifecycle methods.)

Constructors

  • public HouseholdSystem()
    Default constructor. Present and marked with [Preserve] in the original to avoid being stripped by code stripping/AOT. The constructor itself does not perform initialization; initialization is done in OnCreate.

Methods

  • protected override void OnCreate() : System.Void
    Initializes the m_MovingInHouseholdQuery with an EntityQueryDesc that selects entities having a Household component and excluding TouristHousehold, CommuterHousehold, MovingAway, Deleted and Temp. Calls RequireForUpdate(m_MovingInHouseholdQuery) so the system will only run when matching entities exist.

  • protected override void OnUpdate() : System.Void
    No-op in the provided implementation. The system's work occurs during PostDeserialize rather than on the regular update loop.

  • public void PostDeserialize(Context context) : System.Void
    Implements IPostDeserialize. If the loaded save version is older than Version.clearMovingInHousehold, the method:

  • Converts the m_MovingInHouseholdQuery results to a NativeArray (Allocator.Temp),
  • Iterates each entity and for each:
    • Reads the Household component and checks that the HouseholdFlags.MovedIn flag is NOT set,
    • If the entity has a PropertyRenter component, checks whether m_Property == Entity.Null (no property assigned),
    • If the household was not moved in and has no valid property renter, the Deleted component is added to the entity to mark it for removal.
  • Disposes the NativeArray after processing. Notes:
  • Uses TryGetComponent to safely read optional components.
  • Allocator.Temp is used for the NativeArray so the array is short-lived — ensure the array is disposed promptly (as in the implementation).
  • This method is guarded by the version check to avoid changing newer saves.

Usage Example

[Preserve]
protected override void OnCreate()
{
    base.OnCreate();
    // Build a query selecting plain Household entities that might represent old "moving in" leftovers.
    m_MovingInHouseholdQuery = GetEntityQuery(new EntityQueryDesc
    {
        All = new ComponentType[] { ComponentType.ReadOnly<Household>() },
        None = new ComponentType[]
        {
            ComponentType.ReadOnly<TouristHousehold>(),
            ComponentType.ReadOnly<CommuterHousehold>(),
            ComponentType.ReadOnly<MovingAway>(),
            ComponentType.ReadOnly<Deleted>(),
            ComponentType.ReadOnly<Temp>()
        }
    });
    RequireForUpdate(m_MovingInHouseholdQuery);
}

Additional notes and modder guidance: - If you change the Version.clearMovingInHousehold constant or the conditions under which households should be cleaned up, update this method accordingly to avoid unintended deletions. - When iterating EntityQuery results, always dispose NativeArray to avoid leaks. If the work becomes heavier or long-running consider using a job or processing in smaller batches. - Preserve attributes are used to ensure the system and its methods are kept by code stripping; keep them if you build AOT/strip builds for the game.