Class FactionController
Controls faction reputation, alliance grouping, and relationship queries for a character. Handles network synchronization of faction standings via FishNet broadcasts and payload serialization.
public class FactionController : CharacterBehaviour, IFactionController, ICharacterBehaviour
- Inheritance
-
ObjectComponentBehaviourMonoBehaviourNetworkBehaviourFactionController
- Implements
- Inherited Members
-
NetworkBehaviour.IsSpawnedNetworkBehaviour.ComponentIndexNetworkBehaviour.NetworkObjectNetworkBehaviour.MAXIMUM_NETWORKBEHAVIOURSNetworkBehaviour.UNSET_NETWORKBEHAVIOUR_IDNetworkBehaviour.ToString()NetworkBehaviour.Reset()NetworkBehaviour.OnValidate()NetworkBehaviour.IsBehaviourReconcilingNetworkBehaviour.ClearReplicateCache()NetworkBehaviour.CreateReconcile()NetworkBehaviour.Reconcile_Reader<T>(PooledReader, ref T)NetworkBehaviour.OnStartServerCalledNetworkBehaviour.OnStartClientCalledNetworkBehaviour.OnStartNetwork()NetworkBehaviour.OnStopNetwork()NetworkBehaviour.OnStartServer()NetworkBehaviour.OnStopServer()NetworkBehaviour.OnOwnershipServer(NetworkConnection)NetworkBehaviour.OnSpawnServer(NetworkConnection)NetworkBehaviour.OnDespawnServer(NetworkConnection)NetworkBehaviour.OnStartClient()NetworkBehaviour.OnStopClient()NetworkBehaviour.OnOwnershipClient(NetworkConnection)NetworkBehaviour.ClearBuffedRpcs()NetworkBehaviour.ExcludeOwnerFromUnbufferedObserversRpcsNetworkBehaviour.IsClientOnlyNetworkBehaviour.IsServerOnlyNetworkBehaviour.IsHostNetworkBehaviour.IsClientNetworkBehaviour.IsServerNetworkBehaviour.IsDeinitializingNetworkBehaviour.NetworkManagerNetworkBehaviour.ServerManagerNetworkBehaviour.ClientManagerNetworkBehaviour.ObserverManagerNetworkBehaviour.TransportManagerNetworkBehaviour.TimeManagerNetworkBehaviour.SceneManagerNetworkBehaviour.PredictionManagerNetworkBehaviour.RollbackManagerNetworkBehaviour.NetworkObserverNetworkBehaviour.IsClientInitializedNetworkBehaviour.IsClientStartedNetworkBehaviour.IsClientOnlyInitializedNetworkBehaviour.IsClientOnlyStartedNetworkBehaviour.IsServerInitializedNetworkBehaviour.IsServerStartedNetworkBehaviour.IsServerOnlyInitializedNetworkBehaviour.IsServerOnlyStartedNetworkBehaviour.IsHostInitializedNetworkBehaviour.IsHostStartedNetworkBehaviour.IsOfflineNetworkBehaviour.IsNetworkedNetworkBehaviour.GetIsNetworked()NetworkBehaviour.IsManagerReconcilingNetworkBehaviour.ObserversNetworkBehaviour.IsOwnerNetworkBehaviour.IsControllerNetworkBehaviour.HasAuthorityNetworkBehaviour.OwnerNetworkBehaviour.OwnerIdNetworkBehaviour.ObjectIdNetworkBehaviour.LocalConnectionNetworkBehaviour.OwnerMatches(NetworkConnection)NetworkBehaviour.Despawn(GameObject, DespawnType?)NetworkBehaviour.Despawn(NetworkObject, DespawnType?)NetworkBehaviour.Despawn(DespawnType?)NetworkBehaviour.Spawn(GameObject, NetworkConnection, Scene)NetworkBehaviour.Spawn(NetworkObject, NetworkConnection, Scene)NetworkBehaviour.RemoveOwnership()NetworkBehaviour.GiveOwnership(NetworkConnection)NetworkBehaviour.GetInstance<T>()NetworkBehaviour.TryRegisterInstance<T>(T)NetworkBehaviour.UnregisterInstance<T>()NetworkBehaviour.CanLog(LoggingType)MonoBehaviour.IsInvoking()MonoBehaviour.CancelInvoke()MonoBehaviour.StopCoroutine(Coroutine)MonoBehaviour.StopAllCoroutines()MonoBehaviour.destroyCancellationTokenMonoBehaviour.useGUILayoutMonoBehaviour.didStartMonoBehaviour.didAwakeMonoBehaviour.runInEditModeBehaviour.enabledBehaviour.isActiveAndEnabledComponent.GetComponent<T>()Component.TryGetComponent<T>(out T)Component.GetComponentInChildren<T>()Component.GetComponentsInChildren<T>()Component.GetComponentInParent<T>()Component.GetComponentsInParent<T>()Component.GetComponents<T>()Component.GetComponentIndex()Component.CompareTag(TagHandle)Component.transformComponent.transformHandleComponent.gameObjectComponent.tagObject.GetEntityId()Object.GetInstanceID()Object.GetHashCode()Object.InstantiateAsync<T>(T)Object.InstantiateAsync<T>(T, Transform)Object.InstantiateAsync<T>(T, Vector3, Quaternion)Object.InstantiateAsync<T>(T, Transform, Vector3, Quaternion)Object.Instantiate(Object, Vector3, Quaternion)Object.Instantiate(Object, Vector3, Quaternion, Transform)Object.Instantiate(Object)Object.Instantiate(Object, Scene)Object.Instantiate<T>(T, InstantiateParameters)Object.Instantiate<T>(T, Vector3, Quaternion, InstantiateParameters)Object.Instantiate(Object, Transform)Object.Instantiate<T>(T)Object.Instantiate<T>(T, Vector3, Quaternion)Object.Instantiate<T>(T, Vector3, Quaternion, Transform)Object.Instantiate<T>(T, Transform)Object.Destroy(Object)Object.DestroyImmediate(Object)Object.DontDestroyOnLoad(Object)Object.DestroyObject(Object)Object.FindObjectsOfType<T>()Object.FindObjectsByType<T>(FindObjectsSortMode)Object.FindObjectsByType<T>(FindObjectsInactive, FindObjectsSortMode)Object.FindObjectOfType<T>()Object.FindFirstObjectByType<T>()Object.FindAnyObjectByType<T>()Object.FindFirstObjectByType<T>(FindObjectsInactive)Object.FindAnyObjectByType<T>(FindObjectsInactive)Object.FindObjectsByType<T>()Object.FindObjectsByType<T>(FindObjectsInactive)Object.nameObject.hideFlags
Properties
Allied
Public accessor for allied factions.
public Dictionary<int, Faction> Allied { get; }
Property Value
Factions
Public accessor for all factions and their standing values.
public Dictionary<int, Faction> Factions { get; }
Property Value
FactionsAreTemplateDerived
True while this character's standings are DERIVED from
RaceTemplate.InitialFaction rather than owned as mutable state.
public bool FactionsAreTemplateDerived { get; }
Property Value
Remarks
This is the difference between the two kinds of character in the faction system. A player carries standings that change during play and are persisted per character, so they are authored by the server and must travel. An NPC never changes faction — SetFaction(int, int, bool) and Add(FactionTemplate, int) refuse to move an NPC's standing on purpose — so its roster is a pure function of immutable template data that every peer already holds, and sending it is both wasted bytes and a second source of truth.
A pet is the exception that proves the rule: CopyFrom(IFactionController) installs its owner's standings, which are not derivable from any template, so copying clears this flag and the roster travels like a player's.
Hostile
Public accessor for hostile factions.
public Dictionary<int, Faction> Hostile { get; }
Property Value
IsAggressive
Gets or sets whether the character is aggressive (treats others as enemies).
public bool IsAggressive { get; set; }
Property Value
Neutral
Public accessor for neutral factions.
public Dictionary<int, Faction> Neutral { get; }
Property Value
OnFactionChangeTriggers
Triggers invoked when a faction standing changes for this character.
public List<Trigger> OnFactionChangeTriggers { get; }
Property Value
RaceTemplate
Gets the race template for this character.
public RaceTemplate RaceTemplate { get; }
Property Value
Methods
Add(FactionTemplate, int)
Adds amount to the faction value.
public void Add(FactionTemplate template, int amount = 1)
Parameters
templateFactionTemplateThe faction template to adjust.
amountintThe amount to add to the faction standing (can be negative).
AdjustFaction(IFactionController, float, float)
Adds a percentage of the defenders hostile faction and removes a percentage of the defenders allied faction.
public void AdjustFaction(IFactionController defenderFactionController, float alliedPercentToSubtract, float hostilePercentToAdd)
Parameters
defenderFactionControllerIFactionControllerThe defender's faction controller to reference.
alliedPercentToSubtractfloatPercentage of allied faction standing to subtract.
hostilePercentToAddfloatPercentage of hostile faction standing to add.
CopyFrom(IFactionController)
Copies all faction data from another faction controller, replacing current state.
public void CopyFrom(IFactionController factionController)
Parameters
factionControllerIFactionControllerThe source faction controller to copy from.
GetAllianceLevel(IFactionController)
Gets the alliance level between this character and another faction controller. Checks party, guild, aggression, and faction standings to determine the relationship.
public FactionAllianceLevel GetAllianceLevel(IFactionController otherFactionController)
Parameters
otherFactionControllerIFactionControllerThe other faction controller to evaluate against.
Returns
- FactionAllianceLevel
The FactionAllianceLevel between the two characters.
GetAllianceLevelColor(IFactionController)
Gets the color representing the alliance level between this character and another faction controller. Green for Ally, Sky Blue for Neutral, Red for Enemy or Aggressive.
public Color GetAllianceLevelColor(IFactionController otherFactionController)
Parameters
otherFactionControllerIFactionControllerThe other faction controller to evaluate against.
Returns
- Color
A UnityEngine.Color representing the alliance level.
InitializeOnce()
Seeds an NPC's derived roster as soon as the character is assembled, before any network activity, so the server holds the same table a client will build for itself.
public override void InitializeOnce()
InitializeTemplateFactions()
Rebuilds the faction roster from RaceTemplate's initial faction, exactly as character creation seeds a new player: allied at maximum, neutral at zero, hostile at minimum.
public void InitializeTemplateFactions()
Remarks
Runs on every peer, from InitializeOnce() on the server and from
ReadPayload(NetworkConnection, Reader) on a client, so both ends compute the same table from the same
immutable asset instead of one end being told. The values mirror
CharacterCreateSystem.BuildStartingFactionEntries; if those diverge, an NPC and a
freshly created player of the same race would disagree about the same faction.
OnStartCharacter()
Called when the character is started on the client. Registers broadcast listeners for faction updates.
public override void OnStartCharacter()
OnStopCharacter()
Called when the character is stopped on the client. Unregisters faction update listeners.
public override void OnStopCharacter()
ReadPayload(NetworkConnection, Reader)
Called before network start callbacks, but after the object is initialized with network values. This may be used to read information from predicted spawning, or simply have values set before initialization without depending on SyncTypes.
public override void ReadPayload(NetworkConnection conn, Reader reader)
Parameters
connNetworkConnectionreaderReader
ResetState(bool)
Resets the faction state for this character, clearing all standing data.
public override void ResetState(bool asServer)
Parameters
asServerboolWhether the reset is being performed on the server.
SetFaction(int, int, bool)
Sets the faction to value.
public void SetFaction(int templateID, int value, bool skipEvent = false)
Parameters
templateIDintThe faction template ID to modify.
valueintThe new reputation or standing value.
skipEventboolIf true, faction change events will not be invoked.
SetRaceTemplateOnSpawn(RaceTemplate)
Replaces the race template this character derives its initial factions from.
public void SetRaceTemplateOnSpawn(RaceTemplate raceTemplate)
Parameters
raceTemplateRaceTemplateThe race template to adopt. Ignored when null.
Remarks
Spawn-time only. The race template is read lazily — nothing is derived from it during
initialisation — so assigning it before ServerManager.Spawn takes effect cleanly.
Calling it on an already-spawned character would leave the alliance tables it has
already built disagreeing with its new race, which is why there is no plain setter.
Exists so one NPC prefab can be hostile at one spawner and neutral at another without duplicating the prefab — and a duplicated prefab is a second object-pool bucket and a second fixed slice of the map's memory budget.
WritePayload(NetworkConnection, Writer)
Writes the current faction state to the network payload for synchronization.
public override void WritePayload(NetworkConnection conn, Writer writer)
Parameters
connNetworkConnectionThe network connection.
writerWriterThe network writer to write to.