GameState Help

This is a discussion on GameState Help within the C# Programming forums, part of the General Programming Boards category; Hi!, I've just started to play around with C# and basic game programming. I've been following this guide C# Game ...

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    10

    GameState Help

    Hi!,
    I've just started to play around with C# and basic game programming.
    I've been following this guide C# Game Development: Game States and having lots fun with it.

    But I've run into a problem making the GameStates, I have followed the guide to the best of my abilities and my files are as following:

    ArchitectureI.cs
    Code:
    using System;
    using System.Windows.Forms;
    using System.Drawing;
    
    namespace ArchitectureI
    {
        public class StringHolder
        {
            private string output;
    
            public string Output
            {
                get { return output; }
            }
    
            public void Change(string newString)
            {
                output = newString;
            }
        }
    
        public class Arch : Form
        {
            GameStateManager gameStateManager;
            StringHolder pretendDevice;
    
            public GameStateManager GameStateManager
            {
                get { return gameStateManager; }
            }
    
            public Arch()
            {
                pretendDevice = new StringHolder();
                pretendDevice.Change("No State!");
                gameStateManager = new GameStateManager(new TitleScreen(pretendDevice));
            }
    
            static void Main()
            {
                Arch arch = new Arch();
                arch.Show();
    
                while (arch.Created)
                {
                    gameStateManager.Process(); // When i add this line i can't compile the program anymore
                    Application.DoEvents();
                }
            }
            protected override void OnPaint(PaintEventArgs pea)
            {
                pea.Graphics.DrawString(pretendDevice.Output, this.Font, Brushes.Black, 0, 0);
            }
    
        } 
    }
    IGameState.cs
    Code:
    using System;
    
    namespace ArchitectureI
    {
        public interface IGameState
        {
            void Render();
        }
    }
    GameStateManager.cs
    Code:
    using System;
    
    namespace ArchitectureI
    {
        public class GameStateManager
        {
            private IGameState state;
    
            public void Process()
            {
                if (state != null)
                    state.Render();
            }
    
            public IGameState SwitchState(IGameState newState)
            {
                IGameState oldState = state;
                state = newState;
                return (oldState);
            }
    
            public GameStateManager(IGameState intialState)
            {
                state = intialState;
            }
    
        }
    }
    Examples.cs
    Code:
    using System;
    
    namespace ArchitectureI
    {
        public class TitleScreen : IGameState
        {
            StringHolder s;
    
            public TitleScreen(StringHolder output)
            {
                s = output;
    
            }
    
            public void Render()
            {
                s.Change("The Wonderful State Game");
            }
        }
    }
    The problem im getting is "Error 1 An object reference is required for the non-static field, method, or property 'ArchitectureI.Arch.gameStateManager'" when i add the following into the Main() while loop in ArchitectureI.cs: gameStateManager.Process();

    I would be very grateful if someone could help me figure out where i did something wrong, and perhaps help me fix it.

  2. #2
    Registered User
    Join Date
    Mar 2009
    Location
    england
    Posts
    209
    This works:

    Arch

    Code:
        public class Arch : Form
        {
            public GameStateManager gameStateManager;
            public StringHolder pretendDevice;
    
            public GameStateManager GameStateManager
            {
                get { return gameStateManager; }
            }
    
            public Arch()
            {
                pretendDevice = new StringHolder();
                pretendDevice.Change("No State!");
                gameStateManager = new GameStateManager(new TitleScreen(pretendDevice));
            }
    
            protected override void OnPaint(PaintEventArgs pea)
            {
                pea.Graphics.DrawString(pretendDevice.Output, this.Font, Brushes.Black, 0, 0);
            }
        }
    Program

    Code:
        public class Program
        {
            static Arch arch;
    
            static void Main()
            {
                arch = new Arch();
                arch.gameStateManager.Process();
                Application.Run(arch);
            }
        }
    Last edited by theoobe; 05-18-2009 at 03:57 PM.

  3. #3
    Registered User
    Join Date
    Jan 2007
    Posts
    10
    thanks alot worked perfectly

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Rewrote my GameState Manager class...
    By Raigne in forum Game Programming
    Replies: 1
    Last Post: 04-05-2008, 08:20 AM
  2. Looking for criticism on my GameState class
    By Raigne in forum Game Programming
    Replies: 1
    Last Post: 03-21-2008, 10:45 AM
  3. Game design -> GameWorld
    By h3ro in forum Game Programming
    Replies: 2
    Last Post: 03-09-2008, 06:49 PM
  4. Game design question
    By h3ro in forum Game Programming
    Replies: 6
    Last Post: 02-28-2008, 08:20 AM
  5. Weird error
    By arabacus in forum Game Programming
    Replies: 2
    Last Post: 10-03-2002, 01:40 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21