Game.Buildings.Occupant
Assembly:
Namespace: Game.Buildings
Type: struct
Base: IBufferElementData, IEquatable
Summary:
Represents a single building occupant as a buffer element for Unity ECS. Wraps a Unity.Entities.Entity reference and implements equality, hashing, and custom serialization so it can be stored in a DynamicBuffer
Fields
public Unity.Entities.Entity m_Occupant
Holds the Entity that represents the occupant. This is the primary data carried by the buffer element and is used for identity, equality checks, and serialization.
Properties
- None This struct exposes no C# properties; it exposes a public field and implements an implicit conversion operator (see Methods).
Constructors
public Occupant(Unity.Entities.Entity occupant)
Creates a new Occupant wrapping the provided Entity. Use this when adding entries to an ECS DynamicBufferor when constructing the struct for serialization.
Methods
-
public bool Equals(Occupant other)
Compares this Occupant to another by comparing their contained Entity values. Implements IEquatablefor efficient equality checks used by collections and ECS. -
public override int GetHashCode()
Returns the hash code of the underlying Entity. Useful when Occupant instances are used in hash-based collections. -
public void Serialize<TWriter>(TWriter writer) where TWriter : IWriter
Writes the contained Entity to the provided serializer writer. Used by the game's save/load system (Colossal.Serialization.Entities). -
public void Deserialize<TReader>(TReader reader) where TReader : IReader
Reads the Entity value from the provided serializer reader and stores it in m_Occupant. Complementary to Serialize for loading saved data. -
public static implicit operator Unity.Entities.Entity(Occupant occupant)
Implicit conversion to Unity.Entities.Entity so you can use an Occupant wherever an Entity is expected without explicit field access (returns occupant.m_Occupant).
Usage Example
// Example: adding an occupant to a building's DynamicBuffer in a system
public partial class AddOccupantSystem : SystemBase
{
protected override void OnUpdate()
{
Entity someOccupantEntity = /* obtain or create an Entity representing the occupant */;
Entities.ForEach((Entity buildingEntity, ref DynamicBuffer<Game.Buildings.Occupant> occupants) =>
{
occupants.Add(new Game.Buildings.Occupant(someOccupantEntity));
}).Schedule();
}
}
// Example: implicit conversion
Game.Buildings.Occupant occ = new Game.Buildings.Occupant(someOccupantEntity);
Unity.Entities.Entity e = occ; // implicit conversion returns occ.m_Occupant
// Serialization and deserialization are invoked by the game's save/load pipeline