Game.Companies.ResourceExporter
Assembly:
Namespace: Game.Companies
Type: public struct
Base: IComponentData, IQueryTypeParameter, ISerializable
Summary:
Represents a company "resource exporter" component used by the game's ECS. Stores which resource is being exported and how much of it. Implements ISerializable to support writing/reading its state (for save/load or network) and can be attached as an ECS component (IComponentData) and used as a query parameter (IQueryTypeParameter). Serialization encodes the resource as an sbyte index (via EconomyUtils.GetResourceIndex/GetResource) followed by the amount as a 32-bit integer.
{{
- Intended to be a lightweight value-type component for use with Unity.Entities.
- Serialization layout: [sbyte resourceIndex][int amount] (writer order must match reader order).
- EconomyUtils is used to convert between the Resource type and a numeric index.
}}
Fields
-
public Game.Economy.Resource m_Resource
{{ The resource type exported by this component. The actual type is defined in Game.Economy. EconomyUtils.GetResourceIndex/GetResource are used to convert between this value and the serialized sbyte index. }} -
public int m_Amount
{{ The amount/quantity of the resource to export. Serialized as a 32-bit int after the resource index. }}
Properties
- None.
{{ YOUR_INFO: This struct exposes only public fields and no C# properties. It relies on the ECS pattern where component data fields are public for direct access. }}
Constructors
public ResourceExporter()
{{ The default parameterless struct constructor is used. Initialize fields inline or via object initializer when creating instances. Example: new ResourceExporter { m_Resource = ..., m_Amount = ... }. }}
Methods
public void Serialize<TWriter>(TWriter writer) where TWriter : IWriter
{{ Writes the component to the provided writer. Steps:- Calls EconomyUtils.GetResourceIndex(m_Resource) and casts the result to sbyte, then writes that sbyte.
- Writes m_Amount as an int.
Notes: - The resource index is stored as sbyte, so it is expected that the number of resources and their indices fit within the signed 8-bit range. If EconomyUtils returns an out-of-range value, the cast may truncate or change the value. - The write order is important and must match Deserialize. }}
public void Deserialize<TReader>(TReader reader) where TReader : IReader
{{ Reads the component from the provided reader. Steps:- Reads a sbyte resource index into a temporary.
- Reads an int into m_Amount.
- Converts the sbyte index back to a Resource via EconomyUtils.GetResource(value) and assigns m_Resource.
Notes: - The read order must match the Serialize implementation. - If the serialized index is invalid, EconomyUtils.GetResource(value) determines how an invalid index is handled (e.g., returns a default/None value). }}
Usage Example
// Example: Creating and using the component in code (ECS + serialization-aware):
var exporter = new ResourceExporter
{
m_Resource = EconomyUtils.GetResource( /* some index or enum */ ),
m_Amount = 500
};
// Adding to an entity (EntityManager usage example)
entityManager.AddComponentData(entity, exporter);
// Serialization example (pseudo-code, depends on IWriter implementation)
using (var writer = GetWriter())
{
exporter.Serialize(writer);
}
// Deserialization example (pseudo-code)
var deserialized = new ResourceExporter();
using (var reader = GetReader())
{
deserialized.Deserialize(reader);
}
{{ YOUR_INFO: - Typical use: attach to company or building entities that export resources, query via ECS systems that operate on IQueryTypeParameter components. - Keep in mind the sbyte resource index when adding new resource types to the economy: ensure mapping indices remain within the sbyte range used here. }}