Skip to content

Game.UI.InGame.FactorInfo

Assembly:
Namespace: Game.UI.InGame

Type: readonly struct

Base: IComparable

Summary:
Represents a single factor (identified by an integer index) together with an integer weight. This struct is used to collect and sort non-zero factors for demand, citizen happiness, and building happiness calculations and to serialize individual factor entries to JSON using the game's enum names for readability.


Fields

  • private readonly int <factor>k__BackingField
    Compiler-generated backing field for the public read-only property factor. Holds the factor index.

  • private readonly int <weight>k__BackingField
    Compiler-generated backing field for the public read-only property weight. Holds the factor's weight.

Properties

  • public int factor { get; }
    Index identifying the factor. The numeric index is typically interpreted as a value from one of the game's enums (e.g., DemandFactor, CitizenHappinessSystem.HappinessFactor, BuildingHappinessFactor) depending on context.

  • public int weight { get; }
    The integer weight associated with the factor. Positive/negative values indicate contribution direction and magnitude.

Constructors

  • public FactorInfo(int factor, int weight)
    Creates a new FactorInfo with the given factor index and weight. Both values are stored in the read-only properties/backing fields.

Methods

  • public int CompareTo(FactorInfo other)
    Comparison implementation that orders FactorInfo instances primarily by the absolute value of weight (descending), and secondarily by factor (descending) when absolute weights are equal. This is used for sorting lists of factors so the most influential factors appear first regardless of sign.

  • public void WriteDemandFactor(IJsonWriter writer)
    Serializes this struct to JSON via the provided IJsonWriter. Writes the type name, the factor name looked up from the DemandFactor enum (using Enum.GetName), and the numeric weight. Useful for debugging/logging demand contributions.

  • public void WriteHappinessFactor(IJsonWriter writer)
    Same as WriteDemandFactor but resolves the factor index against CitizenHappinessSystem.HappinessFactor enum names when writing the "factor" property.

  • public void WriteBuildingHappinessFactor(IJsonWriter writer)
    Same pattern but resolves the factor index against the BuildingHappinessFactor enum names when writing.

  • public static NativeList<FactorInfo> FromFactorArray(NativeArray<int> factors, Allocator allocator)
    Converts a NativeArray (where each index is a factor and the value at that index is the weight) into a NativeList using the provided allocator. Zero-weight entries are skipped. The resulting list is sorted using the struct's CompareTo implementation before being returned. Caller is responsible for disposing the returned NativeList when appropriate.

Usage Example

// Example: build a list of non-zero demand factors and write them to a JSON writer.
// 'factors' is a NativeArray<int> where index == factor and value == weight.
// 'writer' is an IJsonWriter implementation supplied by the modding environment.

public void ExampleUsage(NativeArray<int> factors, IJsonWriter writer)
{
    // Convert to a sorted NativeList<FactorInfo>
    var list = FactorInfo.FromFactorArray(factors, Unity.Collections.Allocator.Temp);
    try
    {
        for (int i = 0; i < list.Length; i++)
        {
            FactorInfo fi = list[i];
            // Write factor using the DemandFactor enum names
            fi.WriteDemandFactor(writer);
        }
    }
    finally
    {
        list.Dispose();
    }
}

// Simple creation and comparison:
FactorInfo a = new FactorInfo((int)DemandFactor.LowDensityResidential, 5);
FactorInfo b = new FactorInfo((int)DemandFactor.HighDensityCommercial, -8);
int cmp = a.CompareTo(b); // sorts by absolute weight first, then factor index