/// <summary> /// This enters the initial state at startuo. If you rely on an initial state this must be called by any /// MonoBehaviour at least once on Start. StateListeners do this automatically. If you don't use StateListeners /// you need to call this method manually. It is required because ScriptableObject do not receive application /// start events. /// </summary> /// <param name="key">Key for instanced StateMachine.</param> public void Init(Object key = null) { if (GetState(key)) { return; } #if UNITY_EDITOR if (InitialState) { Logging.Log(this, "Initializing with " + InitialState.name, logToConsole); } else { Logging.Log(this, "Initializing with null", logToConsole); } #endif #if UE_Photon //Register main instance to sync manager to automatically propagate state changes to new players. if (PhotonSync.PUNSync) { if (Instanced && original) { PhotonSyncManager.RegisterStateManager(original); } else { PhotonSyncManager.RegisterStateManager(this); } } #endif SetState(InitialState, key); }
/// <summary> /// Propagates the current state of this instance towards all other clients. /// </summary> private void PropagateStatePhotonInstance() { if (_state != null) { PhotonSyncManager.SendEvent(original.PhotonSync, PhotonSyncManager.EventStateChange, _state.name, KeyID); } }
/// <summary> /// Sends a state change message to all clients. This is called /// when a new client joins so it has all states synchronized. /// </summary> public void PropagateStatePhoton() { foreach (var instance in GetInstances()) { var state = instance._state; if (!state) { return; } PhotonSyncManager.SendEvent(PhotonSync, PhotonSyncManager.EventStateChange, instance._state.name, instance.KeyID); } }
/// <summary> /// Sends a state change message to all clients. This is called /// when a new client joins so it has all states synchronized. /// </summary> public void PropagateStatePhoton() { if (Instanced) { foreach (var instance in GetInstances()) { instance.PropagateStatePhotonInstance(); } } else if (_state != null) { PhotonSyncManager.SendEvent(PhotonSync, PhotonSyncManager.EventStateChange, _state.name, KeyID); } }
/// <summary> /// Enters the given state in the given StateManager instance. /// </summary> /// <param name="instance"></param> /// <param name="state"></param> /// <param name="key">Key for instanced StateMachine.</param> private void SetStateInstance(StateManager instance, State state) { if (instance._state == state) { return; //The state to enter is already active. } if (state == null) { Logging.Error(this, "You are trying to set the state to null which is not supported!"); return; } if (state.stateManager != this) { Logging.Error(this, "The state " + state.name + " you want to enter " + "is not controlled by this state manager '" + name + "'!"); return; } #if UNITY_EDITOR Logging.Log(this, "Change State to: " + state, logToConsole); #endif instance.OnStateLeave.Invoke(instance._state, state); instance._state = state; instance.OnStateEnter.Invoke(state); instance.WriteHistory(); if (Instanced) { FileLogger.Write(fileLogging, state.name + " was entered.", instance.KeyID.ToString()); } else { FileLogger.Write(fileLogging, state.name + " was entered."); } #if UE_Photon PhotonSyncManager.SendEvent(PhotonSync, PhotonSyncManager.EventStateChange, state.name, instance.KeyID); #endif }
/// <summary> /// Fires the given event instance. /// </summary> /// <param name="instance"></param> /// <param name="value"></param> private void RaiseInstance(ParameterEvent <T, TS> instance, T value) { for (var i = instance.eventListeners.Count - 1; i >= 0; i--) { instance.eventListeners[i].OnEventRaised(value); } instance.OnEventTriggered.Invoke(value); if (Instanced) { FileLogger.Write(logging, name + " was raised with parameter " + value + ".", instance.KeyID.ToString()); } else { FileLogger.Write(logging, name + " was raised with parameter " + value + "."); } #if UE_Photon PhotonSyncManager.SendEventParam(PhotonSync, name, instance.KeyID, value); #endif }
/// <summary> /// Fires the given event instance. /// </summary> /// <param name="instance"></param> private void RaiseInstance(GameEvent instance) { for (var i = instance.eventListeners.Count - 1; i >= 0; i--) { instance.eventListeners[i].OnEventRaised(); } instance.OnEventTriggered.Invoke(); if (Instanced) { FileLogger.Write(logging, name + " was raised.", instance.KeyID.ToString()); } else { FileLogger.Write(logging, name + " was raised."); } #if UE_Photon PhotonSyncManager.SendEvent(PhotonSync, PhotonSyncManager.EventRaiseUEGameEvent, name, instance.KeyID); #endif }