Skip to content

Game.Citizens.HouseholdNeed

Assembly: Assembly-CSharp
Namespace: Game.Citizens

Type: struct

Base: IComponentData, IQueryTypeParameter, ISerializable

Summary: Represents a single need for a household (resource type + required amount). This struct is used as an ECS component and supports custom serialization via Colossal.Serialization. It stores which Resource the household needs and how much of that resource is required. During deserialization there's special handling to ensure households never keep a positive "Money" need (amount is reset to 0 if Resource == Money).


Fields

  • public Resource m_Resource Holds the type of resource this household needs (e.g., Food, Water, Electricity, Money, etc.). Serialized as a signed byte index via EconomyUtils.GetResourceIndex/GetResource.

  • public int m_Amount The quantity required of the resource. Serialized as a 32-bit integer. During deserialization, if this value is > 0 but the resource is Money, it will be clamped to 0 (households do not have a positive money need).

Properties

  • This type exposes no properties.

Constructors

  • implicit default constructor Structs have an implicit parameterless constructor which initializes m_Resource to its default and m_Amount to 0.

{{ YOUR_INFO }} - You may create instances with an object initializer, e.g. var need = new HouseholdNeed { m_Resource = Resource.Food, m_Amount = 3 };

Methods

  • public void Serialize<TWriter>(TWriter writer) where TWriter : IWriter Serializes the HouseholdNeed to the provided writer.
  • Writes a single signed byte that is the resource index returned by EconomyUtils.GetResourceIndex(m_Resource).
  • Writes the m_Amount as an int.
  • Generic writer must implement IWriter from Colossal.Serialization.Entities.

  • public void Deserialize<TReader>(TReader reader) where TReader : IReader Deserializes the HouseholdNeed from the provided reader.

  • Reads a signed byte into a temporary value and later converts it to a Resource via EconomyUtils.GetResource(value).
  • Reads an int into m_Amount.
  • After reading, if m_Amount > 0 and the deserialized resource is Resource.Money, m_Amount is set to 0 (households should not have a positive money need).
  • Generic reader must implement IReader from Colossal.Serialization.Entities.

Additional notes: - The resource is serialized as an index (sbyte). Ensure EconomyUtils.GetResourceIndex and EconomyUtils.GetResource remain consistent between serializing and deserializing versions; otherwise resource mapping can break. - The sbyte range (-128..127) must be sufficient for the Resource enumeration indexing used by the game. If custom mods extend resources beyond expected range, serialization may overflow or map incorrectly. - The reset behavior for Money on deserialization is an intentional safeguard; be aware when creating or loading saved data.

Usage Example

// Creating a HouseholdNeed instance
var need = new HouseholdNeed
{
    m_Resource = Resource.Food,
    m_Amount = 5
};

// Example: serializing to a writer (pseudo-code; use your game's writer implementation)
using (var memStream = new MemoryStream())
{
    var writer = new BinaryWriterWrapper(memStream); // replace with IWriter implementation
    need.Serialize(writer); // TWriter must implement IWriter
    // persist memStream.ToArray() ...
}

// Example: deserializing from a reader (pseudo-code)
byte[] data = /* previously serialized data */;
using (var memStream = new MemoryStream(data))
{
    var reader = new BinaryReaderWrapper(memStream); // replace with IReader implementation
    var deserializedNeed = new HouseholdNeed();
    deserializedNeed.Deserialize(reader); // TReader must implement IReader

    // Post-deserialize: if deserializedNeed.m_Resource == Resource.Money && deserializedNeed.m_Amount > 0
    // then deserializedNeed.m_Amount will have been set to 0 by the Deserialize method.
}

{{ YOUR_INFO }}