Launch MonoGame From a form

I need to run the game in the same project I have a windows form running. I have the need of an editor and I do not want to include Extended or to flip back and forth between two applications. I know I did this years ago at a job for some idea the boss had but I can’t seem to get it to work. Right now I stop the project, comment out the code to run the form and uncommet the code to run the game :~
Any suggestions?

It’s been years since I used MonoGame with WinForms but looking at GitHub and, MonoGame.Forms seems like a pretty nice solution to build an editor with both of them.

As for making sure your game works inside your editor seamlessly, it depends on exactly what your needs are but it mostly comes down to decoupling. This is what I do:

  • I have 3 projects: Editor.exe, Game.DesktopGL.exe and Game.Core.dll. Almost all of my game is in the later.

  • I decouple the game logic from the game executable and make sure that the executable project contains the least amount of code possible, just enough to hook up my actual game code from Game.Core.dll.

  • The most crucial part of decoupling, in my case, is a class I call GameContext that mirrors the Game class but without dependencies on windowing and such. This way I can use an instance of GameContext inside the editor.

  • In the editor, I reference Game.Core.dll and I have a user control that handles the initialization, drawing and updating of this GameContext. The drawing part will differ a bit depending on what library you’re using. You might encounter some bumps down the road with the GraphicsDeviceManager that really wants an instance of the Game class passed to it (hopefully this gets fixed at some point). All libraries I’ve seen implement their own editor-specific IGraphicsDeviceService.

In WPF, I have a ViewModel that can control all aspects of the GameContext class. I can play/pause/stop, I can change the scene or even inject editor-only services and components because it also handles that. To me, the Game class is mostly just a dumb container that I use for windowing and other platform specific things. Everything else is handled by GameContext. I got good results using this method!

This got a bit lengthier than I expected, hope this helps!

Thanks, I’ll look at that idea if all else fails but I really just need to lunch the game from one button inside of the same executable as the form.