Subscribe not to miss - Events

An event-driven development approach allows you to naturally isolate the implementation of the application's business logic from the user interface. As a more Unity developer, I will demonstrate everything in the context of a game development. In the last article, I showed how state machines allow you to organize business logic in the form of a state system with well-defined behavior. Today I would like to pay attention to how the automaton should interact with game objects and control the state of the scene.





In the examples in the article about automata, I used the following construction:





Game.Event.Invoke("joystick_updated", input);
      
      



By the structure of the function call, you can understand that there is some static Game class that provides global access to its fields, which is very convenient for small project sizes. Here is some of its content:





public static class Game
{
    public static FSM Fsm = new FSM();
    public static EventManager Event = new EventManager();   
    public static ObservableData Data = new ObservableData();
...
      
      



In these examples, you can see some liberties in implementation details. When scaling a project, for example, you will have to abandon the static context and, on the basis of the Game class, implement components, call them pretentiously MonoBehaviourPro with a similar structure for complex subsystems, and pass it as a context to the automaton and the components of these subsystems. I am deliberately smoothing these corners for better clarity of the example. Today we will look at the class with the long-suffering name EventManager , since it is an ObservableData dependency and without it we cannot move on. The link shows the complete implementation of the EventManager class ., the principle of its operation is extremely simple. We keep a list of delegates with an arbitrary signature, subscribed to events with a string key.





, Generic-, Type safety. , . , , EventManager binds binds_global . , Unity. , , . , . Awake OnDestroy. binds, . . , , - . , " " , Global.





, EventManager c 5 :






        public void Bind<T>(string name, Action<T> ev)
        public void BindGlobal<T>(string name, Action<T> ev)
        public void Unbind<T>(string name, Action<T> ev)
        public void UnbindGlobal<T>(string name, Action<T> ev)
          
        public void Bind(string name, Action ev)
        public void BindGlobal(string name, Action ev)
        public void Unbind(string name, Action ev)
        public void UnbindGlobal(string name, Action ev)
          
        public void Invoke<T>(string name, T arg)
          
        public void Invoke(string name)
      
      



. . FSM , , EventManager , , ( MonoBehaviourPro, PlayerController, , SPlayerDriving , , , SPlayerClimbing, , , . , ). , SWin level_done, , , .





, , - , , , - PlayerPrefs, . .





Thus, the automaton in the course of its work can send events to the objects subscribing to them through the EventManager. In the next article, I will discuss the capabilities provided by the ObservableData class in conjunction with the described tools.





This article is the second in a series:





  • Divide and Conquer - Using FSM in Unity





  • Subscribe not to miss - Events








All Articles