Game.Prefabs.Modes.TrafficAccidentMode
Assembly:
Assembly-CSharp (runtime game assembly — adjust if your mod targets a different assembly)
Namespace:
Game.Prefabs.Modes
Type:
class TrafficAccidentMode
Base:
LocalModePrefab
Summary:
TrafficAccidentMode is a prefab component used to configure traffic-accident event prefabs via ECS component data. It contains an array of ModeData entries that pair an EventPrefab (the accident prefab) with an occurrence probability. The class provides three lifecycle methods used by the mode system to record required ECS component usage and to apply or restore the probability values on the corresponding TrafficAccidentData ECS components.
The component: - Resolves EventPrefab -> MonoBehaviour TrafficAccident component -> ECS Entity (via PrefabSystem.GetEntity). - Reads and writes TrafficAccidentData on that entity to set the runtime occurrence probability. - Logs a critical message via ComponentBase.baseLog.Critical if a referenced EventPrefab does not have a TrafficAccident component.
Note: The game code uses slightly different spellings for the probability field between the MonoBehaviour (TrafficAccident.m_OccurrenceProbability) and the ECS component (TrafficAccidentData.m_OccurenceProbability). This is present in the original code and must be used as-is when accessing fields.
Fields
-
public ModeData[] m_ModeDatas
Holds an array of ModeData entries. Each entry links an EventPrefab to a probability value. The array is iterated in RecordChanges, ApplyModeData and RestoreDefaultData. No null check for the array itself is performed in the original code; ensure m_ModeDatas is non-null before use when manipulating this component from a mod. -
public class ModeData
(nested) public EventPrefab m_Prefab
Reference to an EventPrefab that should contain a TrafficAccident MonoBehaviour component.public float m_OccurrenceProbability
Desired occurrence probability to apply to the corresponding TrafficAccidentData ECS component.
Properties
- None declared by this class.
Constructors
public TrafficAccidentMode()
Default (compiler-generated) constructor. No custom initialization logic in the original source.
Methods
public override void RecordChanges(EntityManager entityManager, PrefabSystem prefabSystem)
Iterates m_ModeDatas, attempts to get the TrafficAccident MonoBehaviour from each EventPrefab, logs a critical message if missing, resolves the ECS Entity using prefabSystem.GetEntity(component.prefab), and calls entityManager.GetComponentData(entity). The GetComponentData call is performed but its result is not used — in the original code this likely forces the component to be referenced/registered for change tracking or ensures the component exists.
Behavior notes: - Does not change data; only reads the ECS component. - Skips entries where the TrafficAccident MonoBehaviour is missing and logs a critical error.
public override void ApplyModeData(EntityManager entityManager, PrefabSystem prefabSystem)
For each ModeData, resolves the TrafficAccident MonoBehaviour and ECS entity, reads the TrafficAccidentData component, sets its m_OccurenceProbability field to modeData.m_OccurrenceProbability and writes the component back with entityManager.SetComponentData.
Behavior notes: - Uses the ECS field name m_OccurenceProbability (note the spelling) when setting the probability. - Logs via ComponentBase.baseLog.Critical when the required TrafficAccident MonoBehaviour is missing and continues to the next entry.
public override void RestoreDefaultData(EntityManager entityManager, PrefabSystem prefabSystem)
For each ModeData, resolves the TrafficAccident MonoBehaviour and ECS entity, reads TrafficAccidentData, sets m_OccurenceProbability to the default value taken from the TrafficAccident MonoBehaviour instance (component.m_OccurrenceProbability) and writes the component back.
Behavior notes: - Restores the ECS component probability to the original value defined on the MonoBehaviour. - Also logs a critical message and skips the entry if the TrafficAccident MonoBehaviour is not present.
Usage Example
This example demonstrates the typical pattern of modifying a mode's ModeData in code and applying it to ECS data. In the real game you usually obtain an instance of TrafficAccidentMode from a prefab or the mode system; here it's shown conceptually.
// Example: change the first mode entry's probability at runtime and apply it
void SetAccidentProbability(EntityManager entityManager, PrefabSystem prefabSystem, TrafficAccidentMode mode, float newProbability)
{
if (mode == null || mode.m_ModeDatas == null || mode.m_ModeDatas.Length == 0)
return;
mode.m_ModeDatas[0].m_OccurrenceProbability = newProbability;
mode.ApplyModeData(entityManager, prefabSystem);
}
// Example: restore defaults
void RestoreAccidentDefaults(EntityManager entityManager, PrefabSystem prefabSystem, TrafficAccidentMode mode)
{
if (mode == null) return;
mode.RestoreDefaultData(entityManager, prefabSystem);
}
Modding tips and caveats: - Ensure the referenced EventPrefab has a TrafficAccident MonoBehaviour; otherwise the code logs a critical error and skips that entry. - The ECS field name used to store the probability is spelled m_OccurenceProbability in TrafficAccidentData; be careful when mapping field names by reflection or when writing your own compatible data structures. - These methods require valid EntityManager and PrefabSystem instances. Call them from a safe point in the game's initialization/token where those systems are available. - Consider validating m_ModeDatas for null and length before invoking these methods if you manipulate the component from mod code.