Game.Citizens.TravelPurpose
Assembly:
Assembly-CSharp
Namespace:
Game.Citizens
Type:
struct TravelPurpose
Base:
IComponentData, IQueryTypeParameter, ISerializable
Summary:
Represents a citizen travel purpose used by the ECS for citizens' travel behavior and persisted across save/load. The struct stores a Purpose enum (the reason for travel), an integer data field for purpose-specific metadata, and a Resource value (from Game.Economy) used when the travel involves a particular resource. Implements Unity ECS marker interfaces and custom binary serialization via ISerializable so it can be written to and read from the game's save stream.
Fields
-
public Purpose m_Purpose
Holds the travel purpose as a Purpose enum (e.g., commute, shopping, leisure, etc.). Serialized as a single byte in the save data. -
public int m_Data
Purpose-specific integer data. Could be used to store additional context (target ID, sub-type, counters, etc.). Serialized as a 4-byte integer. -
public Resource m_Resource
A Resource enum (from Game.Economy) associated with the travel purpose (for example, a good being transported or consumed). Serialized as an sbyte index via EconomyUtils.GetResourceIndex.
Properties
- This type has no properties.
Constructors
public TravelPurpose()
(default)
No explicit constructors are defined in the source; the default value-type constructor is used. Initialize fields manually as needed before use.
Methods
public void Serialize<TWriter>(TWriter writer) where TWriter : IWriter
Serializes the struct into the provided writer. Serialization steps:- Writes m_Purpose cast to a byte.
- Writes m_Data as an int.
- Calls EconomyUtils.GetResourceIndex(m_Resource) to obtain an sbyte index and writes that sbyte. Notes:
-
The resource is stored as an index (sbyte) for compactness; ensure the resource index fits in sbyte.
-
public void Deserialize<TReader>(TReader reader) where TReader : IReader
Deserializes the struct from the provided reader. Deserialization steps: - Reads a byte and assigns it (cast) to m_Purpose.
- Reads an int into m_Data.
- Reads an sbyte and resolves it to a Resource via EconomyUtils.GetResource. Notes:
- The same index mapping used in serialization must be used here (EconomyUtils.GetResource).
Implementation details: - Uses EconomyUtils.GetResourceIndex to convert Resource to an sbyte index and EconomyUtils.GetResource to convert back. - Purpose is written/read as a single byte, so the Purpose enum values must be within byte range for compatibility.
Usage Example
// Constructing a TravelPurpose for a shopping trip involving a resource:
var tp = new Game.Citizens.TravelPurpose
{
m_Purpose = Purpose.Shopping,
m_Data = 42, // purpose-specific metadata
m_Resource = Resource.Food
};
// Serializing (conceptual — actual writer depends on the game's serialization APIs)
someWriter.WriteComponent(tp); // or tp.Serialize(writer) if using a raw writer
// Deserializing (conceptual)
Game.Citizens.TravelPurpose loaded;
loaded.Deserialize(someReader);
{{ Additional notes: - Because TravelPurpose implements IComponentData, it is intended to be attached to entities in Unity.Entities (ECS) and used in queries where travel intent/metadata is needed. - When modifying Purpose or Resource enum layouts, ensure backward compatibility with the single-byte and sbyte storage formats to prevent corrupt saves. - EconomyUtils.GetResourceIndex/GetResource are the canonical mapping helpers for compact resource serialization; prefer these over custom mappings. }}