📜 ⬆️ ⬇️

Notification of changes to the shared state of SharedState in components

As a continuation to the previous article about the general state for components , I continue to develop the topic and I want to implement a feature that allows you to catch and process an event in each component by a state change (when the data in the general state changes in some component).

Link to the project

Usually, in order to track the data change, in some component, a direct link is created, that is, we, or in the Update method, check whether the data has changed


Frame data verification via Update method

or we subscribe to a very specific event, which is triggered independently in the “monitored” component, and is eventually processed.


Subscribing to an event in a component and processing it

There are many shortcomings in this approach, which for the most part are described in the last article. The main common cause of these shortcomings is strong coherence and complex maintenance (the ability to maintain, develop the project, refactor).

Creating a solution "Status Notifications"


Now that you have a system that allows you to weaken the connection between components and the means to create mailings between components, you can get rid of manually checking values ​​in a general state, as follows:


General Status Notification Scheme

In the implementation described in the previous article, the data is changed in one place in the indexer of the SharedState component and therefore it is very easy to control their change.


Indexer in general condition

In the indexer setter, we assign state values.

Now, for the SharedState component, you need to add a dependency on SharedEvents , since I’ll use it to send notifications. Add attribute for SharedState


Add dependency to SharedState from SharedEvents

And create a class that inherits from EventData to send data in the state change notification. It will contain the name of the parameter, the state and its new value.


Adding a class that contains information about the change in the general state

Now you need to add a link to SharedEvents , getting it from the game object in the SharedState component


Getting SharedEvents in the SharedState component

Now we will change the setter of the indexer so that every time a state changes, a notification with the name “sharedstatechanged” is created and we will pass an object containing all the data about the change.


Change Indexer Setter

Now it remains to subscribe to changes in any component, change the data in another, and check how it works.

Suppose the SecondComponent changes state, for example the “somedata” parameter, and the FirstComponent component tracks the state change by subscribing to notifications

In the SecondComponent, we invoke the parameter change.


Call data change

And now, in the FirstComponent method, we add a handler and output information on changes in the general state to the console.


Handling General State Change Events

Now, if we start the game, after the general state is changed in the SecondComponent , in the FirstComponent component we will receive a notification and output the data to the console



Now that it works, you can refactor a bit and make the code even more convenient. To do this, you need to transfer the subscription to common state change notifications to the SharedStateComponent base class and create an abstract method, implementing which each component will process state changes in it, or not, at the component’s discretion.

We transfer the subscription to SharedStateComponent


Subscribing to general status notifications in base class

Add an abstract method and call it in the event handler.


Adding an abstract method to handle events in child classes

And now in each child component FirstComponent and SecondComponent you need to implement this method and process the change in the general state in it. But as before, we will simply output this to the console.


Implementation of the abstract method in the components FirstComponent and SecondComponent

And now when we start the game we will see 2 entries in the console, from both components.



Important!


In the previous implementation there was a bug in the SharedEvents class and in order to fix it, you need to change the Subscribe method to:

Source: https://habr.com/ru/post/438628/