• Advertisement
Sign in to follow this  

MVC and the in-betweens for game development

This topic is 1804 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello GameDev! I'm starting work on a new project now, and I want to fit it as nicely into MVC as possible. I feel comfortable distinguishing game logic from graphics from input, but I want to get better/cleaner at this. A couple edge cases still confuse me, for example, where do I put cutscenes? Is the timing between shots in game logic, even though it has no impact on the rules, or should it be 100% in graphics, since it's all visuals and no engine or interaction?

 

More what I'm trying to figure out (part 2) though is how to share information between the three. In my last game, I gave everything access to the game logic main file, and had things passed through a couple files to go up/down the hierarchy (from keyboardHandler to controller/input to model/game logic to playerHandler) but that felt sloppy to have things jump through so many steps. Any advice on this?

Share this post


Link to post
Share on other sites
Advertisement

I made my game client with a slightly modified MVC.

 

The MVC you think of with the database as model usually found in web development does not really suite a game engine, according to my own beliefs.

 

Here is what I did:

MVC+L (Model/View/Controller/Logic) because standard MVC does not state where your logic would fit into the architecture.

 

The model stores all persistent data, such as game objects, mouse pointer, open windows. A game object is a unique id with a number of properties. Like this

{

    id: 1231234

    mesh: "GruntyOgre.mesh"

    pos: 1, 3, 0

    speed: 0

    hp: 100

}

 

{

    id: 231231

    window: "MenuScreen.xml"

}

 

{

    id: 23123156

    mouse_pointer: "green_arrow.png"

    pos: 100, 231

}

It has no functionality other that creating, destroying game objects and adding/removing properties on game objects.

 

The view registers itself on the model and gets notified with game objects are created/destroyed or properties changed. When a game object with a graphical representation (for example having the property mesh) is created, it creates its own graphical object with the same id used to display stuff on the screen (including GUI).

 

The controller listens for input from keyboard, mouse, network etc, translates them to events and forwards them to the logic.

 

The logic is a state machine that does all logic (duh) in the game. It creates, destroys, changes properties in the model.

 

All 4 different layers are completely decoupled and only communicates with callbacks registered in each other.

 

This gives my engine flexibility and some interesting properties such as:

- I can choose not to create a view, thus I can make it easy to create a multiplayer game (if add the feature for the layers to registers themselves on remote machines).

- I can choose to switch resultions and even graphics implementations (opengl/directx) during runtime without having to shut down the game

- I can make any type of game with the engine by just replacing the logic layer.

- I can dynamically upgrade/restart all parts of the game but the model without loosing any game data (since the model is just a stupid container chances are low I would have to update it).

 

On a side effect, this architecture makes it also easy to add and remove features since changes can not cause shockwaves of changes outside their own layers.

Edited by flodihn

Share this post


Link to post
Share on other sites

I have discussed this methodology with my professors at University. Essentially the reply was, MVC doesn't fit exactly into a game system. Mike McShaffery, writer of Game Coding Complete uses something similar to it, and it could be worth reading his book (I'd reccomended either the third or fourth edition) in order to get an idea. Essentially, the way I see it, you have your game screens, intro, main menu, level 1, level 2, etc. But you define your logic completely seperately and use controllers to CONTROL the gamer as it were. Sometimes you just need to have the rendering and the logic closely tied together, games are awesome like that.... yeah. One method Mike uses is an event based system. The view (being the screen lets say) accepts input, handles rendering and playing sound. Events will trigger, for example, player is shot which will be sent to the logic which will update and if required return player is dead, and the screen will update accordingly.

 

Thats a pretty rough explanation, but should set you on the right track. Essentially what flodihn is saying.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement