Last Updated: November 15, 2023
·
3.458K
· alexanderbrevig

MVC in games, yeah - you heard me!

Model View Controller, in a game!?

The story
I am no longer surprised by the constant change of requirements in games. Especially for UI elements.
Most of the time, it's not the data that should be changed however (it's not like they suddenly want the high score to be a label congratulating you on your progress) it's the presentation of it.
The odd case where the behavior is subject of tweaking also occur.

The kicker is that they want to do an A/B demonstration of the two variants, or maybe an A/B/C/D test.

It's pretty clear where I'm going with this right?

The solution
Make the model contain the data of what should be visualized. Then, implement a small controller where you control the rendering of whatever data that should be rendered. Then, have this controller configure and initiate various views for the data depending on some input that enable you (the rock star programmer) to change UI in run time with no hazzle.
The next time your boss changes his mind, you may even have the implementation ready.

In contrast to the web MVC model, I've found that the controller should be allowed to play a bigger part. It should be allowed to constantly control the state of the view, i.e. for helping with animations and transitions.

Good luck on your mini MVC for your UI!

5 Responses
Add your response

Thank you. I work on web applications all day for banks and such. If I don't write readable code, then the next guy (possibly even me!) get's to wade through my pile of spaghetti. So, I cringe every time I hear game developers say things like "It doesn't matter what your code looks like, just start hacking!" Especially in the current open source renaissance, your code needs to be organized and readable. You should pay attention to the things that business app developers have been getting right for years.

A few ideas:

If your game connects to a server, you need to secure it. Use hashes, salts and all that.

You can write unit tests in just about any language. Since Javascript is popular for games right now I'll point anyone to Jasmine js: https://github.com/pivotal/jasmine

...and yes, use MVC. Seperate drawing logic from "business" logic (behind the scenes number stuff that doesn't have to do with screen dimensions). It takes a bit more work, but man, it really cuts down on confusion in your code.

Thanks again for the article.

over 1 year ago ·

MVC is a very obvious separation of concerns for games development. I encourage it, and so did the books I self-assigned myself at university (Game Coding Complete, Second Edition, for example).

Sometimes compromises are necessary in order to get a game shipped, but MVC should help to reduce the complexity of the code such that bugs are easier to find (primarily because it's easier to test).

over 1 year ago ·

Yes, I agree, never had any problem with integrating MVC into games in any language and there's absolutely no probs in doing so for javascript as well.

I also agree with the comments that making your code readable is important. A maintable, debuggable and extensible codebase is a joy to work with, one that does not meet those criteria is a pain.

It would depend on what sort of game it was though as to how useful implementing MVC would be but it would have relevance to all and probably in most cases produce a better codebase.

over 1 year ago ·

I think MVP is more suitable for the presentation layer. Because the visual components should react to events, not direct calls. Also MVP is much more testeable than MVC.

over 1 year ago ·

Good info, thanks, just started working on this.

over 1 year ago ·