I’m having trouble deciding on a good structure for the programming of my game. At school, we learned to use a State Manager, which is loaded in the first scene (the main menu) as a Singleton. In the Update() function of the State Managers, we call a custom update function of the active state. Then, we created an interface for our custom states, and gave each scene a unique state.
What bothers me with this approach, is the difficulty to use it when creating a game where the player can visit the levels multiple times. Let’s say we have a Resident Evil type game, where each room or area is a different scene. We would then have to create each state, and make sure that the state knows about the progress of the puzzles in each scene, and all the other scenes. And, if the player is able to pick up and carry objects in an inventory, and is able to for example, throw objects anywhere, wouldn’t this cause some serious issues? This seems like a bit of a mess.
What would be a good solution? Would a global Game Manager be a better idea? And how would you keep track of the progress of Resident Evil like puzzles, and save the positions of objects in a scene?
A global state manager sounds like a horrible idea, why recreate so much that Unity already provides for you? It also makes you very dependant on a single script, which is something you want to avoid.
Its a very difficult question because there is no one approach that works for everyone and in every game. I personally prefer small components that are mostly self-relient.
The best way to figure out what works is to simply iterate. Just try something you think will work right now and adjust it accordingly.
Well obviously you need one script to store global variables and progression so it is always there and then when needed save thoese. but as tjheuvel said try to split work into multiple scripts but also you cant split too much. This comes with expirience, just try how you feel it should and if something is working slow or doesnt work, fix it otherwise it is okay.
fix something only if it is broken (or you find much simpler and better way to do it)
I also prefer this, but how would you incorporate this into the Resident Evil example I mentioned? And what do you mean by recreating much what Unity already provides? Do you mean the Update() function?
Some global state is needed, (but it doesn’t have to be a singleton). Normally at a minimum you would need to manage menus, play, and game over states at a minimum.
But beyond that there are almost as many ways to structure a game as there are developers.
At one end there are developers like me that build hundreds of tiny little pieces of game that are all self sufficient. Each entity takes care of itself. Together the interactions between entities create a game.
On the other end there are developers that build massive managers with references to every item in a scene and every thing that can be instantiated. Each manager delegates tasks off to sub managers and so forth down the chain until something actually happens.
Then there are a variety of ideas in between. And hybrids of both systems and so forth.
Structure will also vary game to game. Choose the structure that’s best for your game.