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.