Link - [UniState: A State Management Library for Unity]
After focusing on game client development for 9 years, I began working on backend tasks last year and am now nearing the completion of my first year. Backend tasks are highly diverse, including not only API development but also infrastructure management, customer support, and abuse detection through log analysis.
I found it very convenient that the backend does not maintain state when handling REST APIs. Since each request comes from different users, maintaining state is impossible. Therefore, retrieving the latest state data from the database each time was a novel discovery for a game client developer who needs to manage a lot of state information.
However, there are still areas in backend tasks that I haven’t touched yet, namely the maintenance of operation tools. Currently, the operation tools are implemented based on React, and as a beginner in backend development, I didn’t have the leisure to study frontend as well. Now that I’m getting used to the backend, I’m slowly learning React.
The essence of React lies in using JavaScript syntax simultaneously with HTML and maximizing reusability by componentizing each feature. As the number of components increases, there is a need for sharing state between components. In React, components are managed in a tree structure, which leads to the phenomenon of “Prop Drilling” during the state transmission process.
To solve this problem, various state management libraries are competing, including Redux, Recoil, Jotai, and Zustand. Compared to the game development field, the web development field has active open source competition, which is a definite advantage as it offers various choices for users.
Main Content In games, ‘state’ often refers to a Finite State Machine, which is primarily for AI, and is a different concept from the state of variables. UniRx has made it easier to subscribe to and filter the state of variables, but strong references between classes remain a problem.
To address this, I created a simple state management library, UniState, which is modeled after Zustand but adjusted for game development. UniState centers around the Store class, designed to manage any type of state. These stores are managed as a map through a singleton manager class, allowing each store to be registered or retrieved.
One aspect considered but not applied is immutability. In C#, when updating the state, struct types are automatically copied, maintaining immutability, but strings, objects, and collections of class types do not have immutability. Although recreating them each time during an update is possible, this was not chosen due to the importance of memory management in games. If you wish to maintain immutability, you should copy and pass objects during updates.
I hope that the game industry continues to share open source resources actively and learn from each other’s strengths and weaknesses. Feedback or corrections are always welcome.