Attempting to create a state machine (?) in c# and monogame to change the menu

119 Views Asked by At

for a computer science project, i decided to do a poker game using monogame. i havent gotten too far and have gotten stuck on how to implement a fully working menu. i followed a tutorial on how to do game states (https://www.trccompsci.online/mediawiki/index.php/Game_states) and edited it to try to make it work for a menu system. it loads the main menu and allows me to click the exit button which quits the game however as soon as i press the button to change to the other menu screen i have implemented, it stops the program with the error

System.NullReferenceException: 'Object reference not set to an instance of an object.'

gameState was null.

i have absolutely NO clue how to fix this and i am nowhere near smart enough to figure it out for myself. below is the file which contains the code to construct the menu and the line the error occurs is "gameState.CurrentGameState = GameStates.MainMenu;"

namespace PokerGameNEA.UI
{
    partial class mainMenu: Panel
    {
        private GameState.Game gameState;
        public void BuildUI()
        {
            var label1 = new Label();
            label1.Text = "POKER";
            label1.Font = MyraEnvironment.DefaultAssetManager.Load<SpriteFontBase>("IyPZIZAmLtaftSwqk3gBCKNd.ttf.fnt");
            label1.TextAlign = FontStashSharp.RichText.TextHorizontalAlignment.Center;
            label1.HorizontalAlignment = Myra.Graphics2D.UI.HorizontalAlignment.Center;

            _playButton = new MenuItem();
            _playButton.Text = "PLAY";
            _playButton.Id = "_playButton";
            _playButton.Selected += (s, a) =>
            {
                gameState.CurrentGameState = GameStates.MainMenu;
            };

(the rest of the code is just for the rest of that menu, nothing that could be causing problems)

here is the code for the state machine:

namespace PokerGameNEA.States
{
    public class GameState
    {
        public enum GameStates
        {
            MainMenu,
            HowToPlay,
            EndOfGame,
        }
        public class Game
        {
            public Desktop _desktop;
            private mainMenu mainMenu;
            private howToPlay howToPlay;
            private Game gameState;

            public void Initialize()
            {
                mainMenu = new mainMenu();
                howToPlay = new howToPlay();
                _desktop = new Desktop();
                _desktop.Root = mainMenu;

                gameState = new GameState.Game();
            }

            // creates a property to store the current game state
            public GameStates CurrentGameState { get; set; }

            // constructor to initialize the game state
            public Game()
            {
                // sets the initial game state
                CurrentGameState = GameStates.MainMenu;
            }

            // Method to update the game state
            public void UpdateGameState()
            {
                // update the game state based on some condition or user input
                // for example, change to HowToPlay when the user selects a "How to Play" option
                // CurrentGameState = GameStates.HowToPlay;
            }

            // Example method to perform actions based on the current game state
            public void PerformActionsBasedOnState()
            {
                switch (CurrentGameState)
                {
                    case GameStates.MainMenu:
                        _desktop.Root = mainMenu;
                        break;

                    case GameStates.HowToPlay:
                        _desktop.Root = howToPlay;
                        break;

                    case GameStates.EndOfGame:
                        _desktop.Root = howToPlay;
                        break;

                    default:
                        Environment.Exit(0);
                        break;
                }
            }

            public void Draw(GameTime gameTime)
            {
                _desktop.Render();
            }
        }
    }

}

it should just load the new screen however it just crashes and i dont know what to do about it!! i have tried another ways of doing this however they also did not work (it is a MUCH worse way of doing it). i have been stuck at this step for a while now and i do not know at all how to fix it, any help would be greatly appreciated and if people want me to provide any further details then i would appreciate it MASSIVELY.

0

There are 0 best solutions below