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.