Suppose one is working on a game which has more than one component of code where the data, view, and logic is disjoint from the workings of another component of code (i.e. all games have a main menu and the game itself, with their looks, logic, and data being independent). How best should one lay an application out?
I see at least two options:
- Top-level code creates model, view, and controller objects which then each have fields of each area of the application. For example, the model object may have a menuData field and a gameData field. Each of the view and controller would also have something similar. Additionally, the main controller would have access to the main view and main model.
- Top-level code creates an object for each area of the code (i.e. menu and game), and then each of these has their own model, view, and controller.
Are either of these the best way to go about this? Is there a more common approach than MVC?
MVC
is to decrease the complexity of the program by splitting logic onto layers with easily managed dependencies. There are a few implementations, each can be used aswell, but I've not heard about something like your first point. The second point seems better, but, as I've said before, you should program your layers independently, so that changes in the view will not affect the model. You can build model as you like and then connect it through controller (or observer) to the view. So the whole model should not know that there is a menu at all. – Joshua Light Feb 13 '17 at 18:24mvc
/DI
frameworks usually handle for you? Resolution and instantiation of dependencies to be injected. Since game engines arent usually build aroundmvc
I can imagine there might need to build similar framework functionality for your(or existing) game engine. – wondra Feb 13 '17 at 18:58MVC
, and the author of a question maybe misunderstood some aspects of the pattern. – Joshua Light Feb 13 '17 at 19:15MenuModel
could be? Can you please explain so that I can help you with some advice. Or you can just look at the commonMVC
-framework -ASP.NET MVC
and see what I did exactly mean.Let's look at simple example: you have a
– Joshua Light Feb 13 '17 at 20:22model
, that handles all the business-logic of your game: stats, creatures, game states and so on. Main menu then is just a set of words, that can send commands to model e.g. "Hey, Start a game, please" (via controller).MenuModel
you will implement for chess example? – Joshua Light Feb 13 '17 at 20:35SavesModel
to store saves with methods likeGetSavesFor( accountID )
, on ui create a graphical menu with states support (actually this is not a model from fromMVC
), and add click handler on button "Saves" that will go to the controller and callGetSaves()
. Controller itself will check current user id, callModel.GetSavesFor ( currentUserID )
and return it to the view. This is one way to do that. Another called Stupid View but I think for example one explained way is enough. – Joshua Light Feb 13 '17 at 21:40