I've been reading about how to design code for a game. One thing I haven't been able to figure out is - how do you manage writing an outside-game level editor (not an 'in-game level editor') without 'copying' code from the game? For example, you might have to copy all code about the different types of entities you can have. You'll have to add the game rendering code.

My guess is this can be done by making a DLL out of the 'engine' part of the game. Then, share it between the actual game and the level editor.

Or is there a better/easier way to do this?


You will have to separate your code into separate projects (in the same solution). Usually you would have the following setup:

-Engine Core       (DLL)
-Game + Game Logic (EXE)
-Content Pipeline  (DLL)
-Editor            (EXE)

For even larger projects you might want to separate the Engine Core over multiple projects like Core, Physics, Graphics, Audio, Input and Networking. All these projects can't reference each other, but they all reference Core where you can store some common base types.

If they would reference each other the separation would be useless.

Anyway this should help you reuse parts of your engine in other projects. For example for the editor you would need to reference the Graphics project which references Core itself.

This separation also means that you can now use Windows Only technologies, like WPF, in your editor while your game can still target Windows and XBLIG.

For more info on using XNA inside a WPF environment see this link: http://blogs.msdn.com/b/nicgrave/archive/2010/07/25/rendering-with-xna-framework-4-0-inside-of-a-wpf-application.aspx

  • \$\begingroup\$ Thanks! A little more looking around and I found this - create.msdn.com/en-US/education/catalog/sample/… - It appears WinForms is supported better than WPF! \$\endgroup\$ – Utkarsh Sinha Mar 31 '12 at 19:08
  • \$\begingroup\$ Hey that's true, but that sample is from 2008, before WPF really got off the ground. I've switched from WinForms to WPF a year ago and I wouldn't for the world switch back :). If you want to stay compatible with that sample you could always use the WinFormsControlHost in a WPF app :). \$\endgroup\$ – Roy T. Mar 31 '12 at 19:50

Separate out rendering logic as best as you could from actual game logic when designing your game engine. One way of doing this is to use Component pattern when building your game engine. For example, XNA uses this pattern at the framework level for flexibility. Use the same code-base for rendering with your level editor; write wrappers if you have to.

Similarly, another somewhat popular pattern is MVC (Model-View-Controller) which could also help. Code your engine using MVC normally. You can then use the same code for View and Model, but inject a custom Controller for editing the Model for your level editor instead of the game engine's Controller that updates according to game input. Implement serialization for game Models to load/save them from editor or game.

Keeping components in separate assemblies will of course encourage separation...


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.