Game.Buildings.ResourceConsumer
Assembly:
Namespace: Game.Buildings
Type: struct
Base: IComponentData, IQueryTypeParameter, Colossal.Serialization.Entities.ISerializable
Summary: Represents a building component that tracks the availability of a resource for a consumer building in Cities: Skylines 2. Implemented as a DOTS/ECS component (IComponentData) so it can be attached to entities and used in high-performance systems. Implements the game's serialization interface so the single-byte state is saved/loaded with the entity.
Fields
public byte m_ResourceAvailability
Holds the resource availability value as a single byte (0–255). Typically used to indicate how much of a particular resource a building currently has or how available the resource is to the consumer. Using a byte keeps the component small and cache-friendly when packed into archetype chunks.
Properties
- (none)
This struct exposes no properties; it is a plain data container intended for fast access in ECS systems.
Constructors
- (implicit default constructor)
As a value type (struct) there is an implicit parameterless constructor that zero-initializes fields. m_ResourceAvailability defaults to 0 unless explicitly set.
Methods
-
public void Serialize<TWriter>(TWriter writer) where TWriter : IWriter
Writes the m_ResourceAvailability byte to the provided writer. Used by the engine's serialization pipeline when saving entity state. -
public void Deserialize<TReader>(TReader reader) where TReader : IReader
Reads a single byte from the provided reader into m_ResourceAvailability. Used when loading entity state from saved data.
Both methods are simple and minimal to keep serialized data compact and fast to process.
Usage Example
using Unity.Entities;
using Game.Buildings;
using Colossal.Serialization.Entities;
// Create an entity with the ResourceConsumer component
var entityManager = World.DefaultGameObjectInjectionWorld.EntityManager;
var archetype = entityManager.CreateArchetype(typeof(ResourceConsumer));
Entity consumer = entityManager.CreateEntity(archetype);
// Set availability (e.g., 200 out of 255)
entityManager.SetComponentData(consumer, new ResourceConsumer { m_ResourceAvailability = 200 });
// Example writer/reader usage (pseudocode; actual writer implementation comes from Colossal.Serialization)
void SaveComponent<TWriter>(TWriter writer, ResourceConsumer component) where TWriter : IWriter
{
component.Serialize(writer);
}
void LoadComponent<TReader>(TReader reader, out ResourceConsumer component) where TReader : IReader
{
component = new ResourceConsumer();
component.Deserialize(reader);
}
// Typical system read example
public partial struct ResourceConsumptionSystem : ISystem
{
public void OnUpdate(ref SystemState state)
{
var em = state.EntityManager;
foreach (var (consumer, entity) in SystemAPI.Query<RefRW<ResourceConsumer>>().WithEntityAccess())
{
// Check and modify availability
if (consumer.ValueRW.m_ResourceAvailability > 0)
{
consumer.ValueRW.m_ResourceAvailability--;
}
}
}
}
{{ Notes and Tips - Because the component is just one byte, it's ideal for tracking simple availability or small-range counters/flags. If you need larger ranges or multiple resource types, consider using additional fields or a different component. - Implementing ISerializable ensures the field participates in the save/load cycle. Keep Serialize/Deserialize in sync and minimal to avoid save bloat. - As an IComponentData struct, mutation should occur inside systems (main or jobified) using proper component access patterns (RefRW/RefRO) to avoid race conditions. - When designing mods, document what the byte values represent (e.g., percentage, 0=none, 255=full) so other modders can interoperate. }}