Skip to content

Game.Prefabs.Modes.GameModeSystem

Assembly:
{{ Likely Assembly-CSharp (game runtime assembly). Confirm in your build if different. }}

Namespace: Game.Prefabs.Modes

Type: class

Base: GameSystemBase, IDefaultSerializable, ISerializable

Summary:
Manages game modes by applying and restoring ModeSetting prefabs within the ECS world. It queries available ModeSetting and GameModeInfo prefabs, applies a selected ModeSetting (or the default "NormalMode") each update, and can be instructed to switch modes via the overrideMode property. Supports saving/loading the active mode via prefab IDs (serialization) and exposes helpers to list available GameModeInfo objects. Uses PrefabSystem to lookup prefabs and EntityQuery to find mode entities. Implements ISerializable for persisting the selected ModeSetting across loads.


Fields

  • private PrefabSystem m_PrefabSystem
    {{ Reference to the PrefabSystem used to look up prefab components (ModeSetting, GameModeInfo, etc.). Populated in OnCreate by retrieving the PrefabSystem from the ECS world. }}

  • private ModeSetting m_ModeSetting
    {{ The currently applied ModeSetting prefab wrapper (if any). When set, StoreDefaultData/ApplyMode have been called and the system considers this mode active. }}

  • private ModeSetting m_NextMode
    {{ Temporary holder for the ModeSetting that should be applied on the next update. Set by overrideMode or by Deserialize. }}

  • private EntityQuery m_ModeSettingQuery
    {{ EntityQuery configured to find entities that hold GameModeSettingData (i.e., ModeSetting prefabs). Used to enumerate available modes. }}

  • private EntityQuery m_ModeInfoQuery
    {{ EntityQuery configured to find entities that hold GameModeInfoData. Used to enumerate GameModeInfo prefabs. }}

  • [CanBeNull] public string overrideMode { get; set; }
    {{ Nullable public property used to request a mode change by prefab name. When non-null, OnUpdate will search mode prefabs for a matching name and set m_NextMode accordingly, then clear overrideMode. }}

Properties

  • public ModeSetting modeSetting => m_ModeSetting
    {{ Read-only accessor for the currently applied ModeSetting wrapper (may be null). Use this to inspect the active ModeSetting instance. }}

  • public string currentModeName { get; private set; }
    {{ Name of the currently applied mode prefab (string). Updated whenever a ModeSetting is applied. Empty string on creation until a mode is applied. }}

Constructors

  • public GameModeSystem()
    {{ Default constructor. Marked with [Preserve] in the source to avoid stripping. Initialization of fields occurs in OnCreate. }}

Methods

  • public List<GameModeInfo> GetGameModeInfo()
    {{ Returns a list of GameModeInfo objects for all GameModeInfo prefabs found via m_ModeInfoQuery. Implementation: converts the query to a NativeArray, iterates, obtains the GameModeInfoPrefab for each entity from m_PrefabSystem and calls GetGameModeInfo(), builds and returns a managed List. Note: disposes the NativeArray after use. }}

  • protected override void OnCreate() : System.Void
    {{ Called when the system is created. Retrieves PrefabSystem from the world, sets up m_ModeSettingQuery and m_ModeInfoQuery (ComponentType.ReadOnly / GameModeInfoData) and initializes currentModeName to an empty string. Marked with [Preserve]. }}

  • protected override void OnUpdate() : System.Void
    {{ Main update logic run each frame (or system tick):

  • If overrideMode is non-null, enumerates m_ModeSettingQuery to find a ModeSetting prefab whose prefab.name matches overrideMode, sets m_NextMode and clears overrideMode.
  • If a current m_ModeSetting exists, calls RestoreDefaultData on it (passing the EntityManager and PrefabSystem) and clears m_ModeSetting.
  • Sets m_ModeSetting = m_NextMode and clears m_NextMode.
  • If m_ModeSetting is still null, searches for a ModeSetting whose prefab.name == "NormalMode" and uses it as fallback.
  • If m_ModeSetting is non-null after that, calls StoreDefaultData, then ApplyMode (which may return a JobHandle) and assigns the returned JobHandle to base.Dependency. Updates currentModeName to the applied prefab.name.
  • Writes debug logs via COSystemBase.baseLog for cleanup/apply steps. Notes: NativeArray results are disposed after each enumeration. ApplyMode returns a dependency that is assigned to the system's dependency chain. }}

  • public void Serialize<TWriter>(TWriter writer) where TWriter : IWriter
    {{ Writes the active ModeSetting's prefab ID to the writer. If m_ModeSetting is null, writes the default(PrefabID) (i.e., an empty ID). This allows persisting the selected mode across saves. }}

  • public void Deserialize<TReader>(TReader reader) where TReader : IReader
    {{ Reads a PrefabID from the reader, resolves the prefab via m_PrefabSystem.TryGetPrefab, and attempts to get exactly a ModeSetting component from that prefab. If successful, sets m_NextMode to that ModeSetting so the mode will be applied on the next update. Otherwise sets m_NextMode to null. }}

  • public void SetDefaults(Context context)
    {{ IDefaultSerializable implementation; in this class the method is empty (no defaults to set). }}

Usage Example

// Example: request a mode change by prefab name
var gameModeSystem = World.GetOrCreateSystemManaged<GameModeSystem>();
gameModeSystem.overrideMode = "SandboxMode"; // OnUpdate will find and schedule this mode

// Example: read current mode name
string active = gameModeSystem.currentModeName;

// Example: list available GameModeInfo
List<GameModeInfo> infos = gameModeSystem.GetGameModeInfo();

Additional notes: - The system relies on ModeSetting prefabs exposing StoreDefaultData/RestoreDefaultData/ApplyMode to snapshot/restore and apply mode-specific ECS state. Ensure ModeSetting prefabs implement those correctly. - OnUpdate will always attempt to apply a mode; if none was requested, it falls back to a prefab named "NormalMode". - NativeArray allocations use Allocator.TempJob and are disposed immediately; ensure not to retain those arrays beyond the method scope. - The system updates base.Dependency with the JobHandle returned by ApplyMode to chain job dependencies correctly.