# C++ game architecture

I'm trying to make a game, but I'm not sure of the best way to set up the main loop and classes. For really small games, I could put everything in the main() loop, including event handling, collision checking, etc. However, for large games, that's seems like a highly inefficient way to get things done. The architecture I want is kind like the way the Minecraft coders did it (I quote Minecraft code because I've seen the source code when downloading MCP).

They have objects entity classes EntityCow and EntityChicken and they have methods like onDeath(), onLivingUpdate(); and item classes like ItemSword have methods like onItemUse(). I've never seen these methods get called directly, but apparently, they get stored in a class called DataWatcher, which, I think "watches" all the data (as the name implies) and calls the appropriate methods in the objects.

Is that how most games do it? If so, how is the DataWatcher class implemented? Any help or alternate suggestions is really appreciated.

• I think you're talking about polling vs event driven. I haven't looked at the source, but I imagine that DataWatcher is an event listener. But your question is pretty broad as it is. Do you have a more specific question? – MichaelHouse Jul 9 '12 at 18:01
• Don't trust the MCP code, IT WASN'T WRITTEN ENTIRELY BY MOJANG! It was deobfuscated by MCP and edited by the community! – thePalindrome Jul 9 '12 at 18:16
• @thePalindrome I don't think the community modified the basic structure of the code, they likely just added method and variable names that are logical. Additionally, who cares if it wasn't written entirely by mojang? – MichaelHouse Jul 9 '12 at 18:27
• yeah, i think the only thing mcp did was give names like a_2352_sd to variables to which it didn't know the names of... rest of it seems reliable... – rxc Jul 9 '12 at 18:28
• As worded, I don't think is a constructive question. Not only is it asking for suggestions and advice (as opposed to asking a very specific question with a specific answer), I don't see why it's relevant what other games (especially a specific other game) does -- how is that relevant to your game? There are many correct and "efficient" ways to architect a game and copying the implementation of another isn't necessarily the best idea for whatever your game is. – user1430 Jul 9 '12 at 19:49

There are a few different ways to implement a game. For entity classes as described above, they probably have a main loop running all of them. For example, if you have EntityCow and EntityChicken, they may be created/instantiated within a main class and are drawing their own animations and recording their own damage. Here is some pseudocode:

main()
{
EntityCow cow = new EntityCow();
}

class EntityCow
{

private cow methods:
onDeath() //cow dies - make invisible

update() //should be constantly updated per game tick
{
//draw cow animations
//check cow damage
//call onDeath() if cow damaged enough
}
}


In many cases of multiplayer and online games, the damage/animation data (etc.) is sent to a server that verifies the data. This helps to make sure that players cannot manipulate data on their own machines and also so each player is able to see the game exactly the same on their own machines. So to answer your question, most larger games have an overall game loop running individual classes that either update themselves or are updated by a server by sending their data to the server so the data can be verified and then used. It is possible with MC's DataWatcher class that it is acting as their server in this case to verify all incoming data and is taking control over each Entity's actions . That means that the above private cow methods are actually public to the DataWatcher class which then calls them. In that case, update would not call OnDeath(), DataWatcher would.