I am making a game using Unity in which I have a main game state machine that controls the current game scenario.

My question is how do I handle communication between different components of the game and the state machine in a way that keeps the code decoupled and scaleable and so that the code doesn't turn into a spaghetti later on.

What I have thought so far

  • Make the state machine a global in a static class or vice versa and allow components to directly call upon its methods
  • Make a global event dispatcher system allowing the FSM to subscribe to necessary events like changeState() etc and fire them from the component classes with the required data
  • Use the service Locator pattern to provide the FSM to locator and just use it from anywhere

Which one of these will be suited more for the application keeping it scaleable and reusable no matter how big the code grows?

Or is there any other better way of communication that I am missing out on?


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.