GraphicsDeviceManager error

So I’m trying not to post too much when I’m having issues, but this error I’m getting when I compile I cannot figure out.

Cannot evaluate expression because the current thread is in a stack overflow state.

        public Main() : base()
    {
        GraphicsDeviceManager graphics = new GraphicsDeviceManager(this);  //This line produces the error message
        Content.RootDirectory = "Content";         
    }

Basically, I’m trying to learn more about classes, inheritance, and a more Object Oriented approach. I’ve setup my project “Main” which inherits from Game. I then created a new class called “Menu” which inherits from Main.

and Main is:

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
//using Microsoft.Xna.Framework.GamerServices;   //discontinued?
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
//using Microsoft.Xna.Framework.Net;            //discontinued?
using Microsoft.Xna.Framework.Storage;


namespace PRS
{
    public class Main : Game
    {
/// <MAIN CLASS DECLARATIONS>
/// 
        public GraphicsDeviceManager graphics;
        public Boolean menuOpen = false;
        

        public SpriteBatch renderText;
        public SpriteFont testFont;
///
/// </MAIN CLASS DECLARATIONS>
        
       
        public Main() : base()
        {        
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }


        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            Main main = new Main();
            main.menuOpen = false;

            if (main.menuOpen == false)
            {
                Menu menu = new Menu();
                

            }
            else
            {
                //do nothing
                
            }
            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            renderText = new SpriteBatch(GraphicsDevice);
            testFont = Content.Load<SpriteFont>("fonts/Arial16");
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
            {
                Exit();
            }

            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            Main main = new Main();
            
            GraphicsDevice.Clear(Color.Red);

            renderText.Begin();

                if(main.menuOpen == false)
                {
                    renderText.DrawString(testFont, "menuOpen = closed", new Vector2(400, 300), Color.White);
                }
                else
                {
                    renderText.DrawString(testFont, "menuOpen = open", new Vector2(400, 300), Color.White);
                }          
            
            renderText.End();

            base.Draw(gameTime);
        }
    }
}

The main code in Menu is:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;



namespace PRS
{
    public class Menu : Main
    {
/// <MENU CLASS DECLARATIONS>
///      
        private SpriteFont menuFont;
///
/// </MENU CLASS DECLARATIONS>
 

/// <METHODS>
/// 
    public Menu()
    {
        menuOpen = true;

        Initialize();
        LoadContent();
        UnloadContent();
        Update(null);
        Draw(null);
    }
    protected override void Initialize()
    {
        base.Initialize();
    }

    protected override void LoadContent()
    {
        renderText = new SpriteBatch(GraphicsDevice);
        menuFont = Content.Load<SpriteFont>("fonts/Arial24");

    }

    protected void unloadContent()
    {

    }

    protected override void Update(GameTime gameTime)
    {
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
        {
            Exit();
        }

        base.Update(gameTime);
    }

    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.Black);

        renderText.Begin();

        renderText.DrawString(menuFont, "TEST MENU SUCCESS", new Vector2(400, 50), Color.White);

        renderText.End();

        base.Draw(gameTime);
    }
///
/// </METHODS>
    }
}

Note - If I don’t use the Menu class at all and use Initialize() & Draw() in my “Main” class, my spritefont and background load fine.


Upon further review, I always forget about the stack… I think it’s looping from the constructor in “Main.Initialize” to “Menu.Initialize” etc until it crashes…

If I comment out the methods in my constructor for Menu,

public Menu()
{
    //menuInitialize();
    //menuLoadContent();
    //menuUnloadContent();
    //menuUpdate(null);
    //menuDraw(null);
}

it will display my debugging value for menuOpen as false. Why isn’t it reassigning it to true from the if statement?


Edited OP to include all my code for both .cs files.

The Game class is not intended to be a base class for all your different screens in your game. There should only be one instance of the Game class. When it is destroyed the game should end.

If you want to build some sort of menu system you don’t need to derive from Game.

I suggest you examine the old XNA Game State Management sample…

http://xbox.create.msdn.com/en-US/education/catalog/sample/game_state_management

… it is a good starting menu/screen system that many developers have extended. At worst it could help you learn how to develop your own.

I see, thanks, this was driving me crazy. You would think that might be something they would want to throw on this .

[quote=“Tom, post:2, topic:1656”]
If you want to build some sort of menu system you don’t need to derive from Game.
[/quote]My intent was try a test case playing with classes and inheritance, looks like I picked the wrong class for a test case lol, lesson learned. Related to this; other than reading through the msdn.xna framework documentation and learning these mistakes the hard way. Is there a better way to approach learning this than I am currently doing? I’m realize that I won’t learn this in a week, month, or year… and it will be a process to ever get a working build of my concept going. I’m just trying to ground myself and create some obtainable goals and milestones.

Also, @Tom thanks for the link, I’ll be running through this once I correct my mistakes.
EDIT - Wow that link is perfect! I will need to spend bunch of time reading this, but the overall structure design is exactly what I needed to see. Thanks again for all that you do!

My suggestion is always to learn by doing.

Pick a simple 2D game to build… tic-tac-toe, pong, etc. Then just go about building it. Focus on the game and not menus or anything to begin with. Once you have some gameplay it is easy to move that code around and change it into anything you want.

We just did a Microsoft Virtual Academy presentation on getting started with MonoGame. As soon as it is up for streaming which should be any day now I suggest giving it a look too.

Perfect, I would love to take a look at that presentation as soon as you guys post it! :smile:

[quote=“Tom, post:4, topic:1656”]
Pick a simple 2D game to build… tic-tac-toe, pong, etc. Then just go about building it. Focus on the game and not menus or anything to begin with. Once you have some gameplay it is easy to move that code around and change it into anything you want.
[/quote]I’ve got a text based version of rock paper scissors I made in VS2010 using C# a while back; I guess that’s why I’m focusing on menus, rending, etc.

[quote=“Tom, post:4, topic:1656”]
My suggestion is always to learn by doing.
[/quote]As I was drinking my coffee this morning I remembered that there is never some set formula for hard work, so my question was kind of stupid. So I made a list of things I want to learn or use:

  • learn github (self explanatory)
  • learn LINQ to XML for data creation and manipulation (instead of relying on text files)
  • expand my C# knowledge (like further with classes, structs,
    lists,interfaces, arrays)
  • focus on the structure of the project, I have a habit of hard coding
    too much I think
  • then applying all of this to the text game from above.

I think these are obtainable long and short term goals, grounded in reality. Hopefully, I’m headed in the right direction.

Microsoft seems to have posted it today…

Thanks for the link Tom!

@Tom @zman

Finally got around to running through all the modules and just wanted to thank you for taking the time to do this. Working through this has helped me to digest Monogame MUCH quicker than running through a forum and has helped me to focus on the areas I should be for a beginner. It has also helped to me to realize that my short and long terms goals can actually be accomplished. Cheers!

1 Like