Sign in to follow this  

[.net] Switcing between different game states

This topic is 3741 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello. This is my attempt to drop some light in the dark area of the game menu programming. I have made a quick and dirty example for tests only, I need your opinions or some of your knowledge to improve it. code formation is unaltered, just drop it in C# and it will display in a correct way. --------------------------------- using System; namespace GamestateTest { public class Game { public static GameState[] gamestate = new GameState[3]; public static GameState currentstate; public static bool quitFlag = false; public Game() { gamestate[0] = new IntroState(); gamestate[1] = new MenuState(); gamestate[2] = new GameplayState(); currentstate = gamestate[0]; } public void Update() { while(!quitFlag) { currentstate.Update(); } } public static void Main(string[] args) { Game gb = new Game(); gb.Update(); Console.ReadKey(true); } public static void Debug(string s, bool waitkey) { Console.WriteLine(s); if (waitkey==true) Console.ReadKey(true); } } public abstract class GameState { protected bool finished = false; public virtual void Resetvalue(){ this.finished=false; } public abstract void Update(); } public class IntroState : GameState { public override void Update() { Resetvalue(); while(finished==false) { Game.Debug("Now the Intro state is running, press enter to go to main menu\n", true); Game.currentstate = Game.gamestate[1]; finished = true; } } } public class MenuState : GameState { public override void Update() { Resetvalue(); while(finished==false) { Game.Debug("Now the Menu state is running\npress enter to start the game. " + "Press q and then enter to quit to Windows.", true); string s = Console.ReadLine(); if (s=="q") Game.quitFlag = true; else Game.currentstate = Game.gamestate[2]; finished = true; } } } public class GameplayState : GameState { public override void Update() { Resetvalue(); while(finished==false) { Game.Debug("Now you play the game, as the time passes you "+ "are getting bored, and eventually you decide "+ "to quit to main menu. Press enter to quit to main menu."+ "Press q and then enter to quit to Windows.", false); string s = Console.ReadLine(); if (s.Equals("q")) Game.quitFlag = true; else Game.currentstate = Game.gamestate[1]; finished = true; } } } }

Share this post


Link to post
Share on other sites
This is more of a design question than it is a .NET question. You should ask a mod to move this to General Programming, you'll get a better response there.

When posting code, don't forget to use the [ source ] tag.

As for your question, what you're starting to touch on here is called a finite state machine. Read that page, and some of the reference links, and see what you can apply to your situation. I would suggest starting with using a tree-structure for your game state graph instead of a linear array.

Share this post


Link to post
Share on other sites
Quote:
Original post by capn_midnight
This is more of a design question than it is a .NET question. You should ask a mod to move this to General Programming, you'll get a better response there.

When posting code, don't forget to use the [ source ] tag.

As for your question, what you're starting to touch on here is called a finite state machine. Read that page, and some of the reference links, and see what you can apply to your situation. I would suggest starting with using a tree-structure for your game state graph instead of a linear array.


This is quite an interesting subject. I'm curious how you would manage a tree structure, what sort of data type would you use in C# for it?

Share this post


Link to post
Share on other sites
what, you can't lookup trees?

class TreeNode
{
private List<TreeNode> branches;
public TreeNode()
{
this.branches = new List<TreeNode>();
}
public void AddBranch(TreeNode node)
{
this.branches.Add(node);
}
public TreeNode GetBranch(int index)
{
return this.branches[index];
}
}

//watch a tree grow:
TreeNode root = new TreeNode();
root.AddBranch(new TreeNode());
root.AddBranch(new TreeNode());
root.AddBranch(new TreeNode());
root.GetBranch(0).AddBranch(new TreeNode());
root.GetBranch(0).AddBranch(new TreeNode());
root.GetBranch(1).AddBranch(new TreeNode());
root.GetBranch(1).AddBranch(new TreeNode());
root.GetBranch(1).AddBranch(new TreeNode());
root.GetBranch(2).AddBranch(new TreeNode());

Note that this design isn't optimal and definitely does not include everything you'll need for an FSM. I'm leaving that stuff for you to figure out on your own.

Share this post


Link to post
Share on other sites
That's not exactly a built in data type, that is just a List<T> array of a class. Anyway, I don't think using it is a very OOP way to implement a state machine scenario for a .NET programmer.

While this would probably get a better response in the general programming section I would like to hear about any C# programmers that have implemented a state machine using OOP.

I'm going to research this and I'll show my results, but off the top of my head it seems I would need an abstract class called "StateMachine" which all other classes that have states will inherit. In this StateMachine class there would be abstract methods for dealing with entry and exit actions and also internal states. I guess another class would be necessary called "StateManager" or something that keeps track of the current state. Or maybe each class can manage the state themselves.

Am I on the right track do you think?

Share this post


Link to post
Share on other sites
Quote:
Original post by Headkaze
That's not exactly a built in data type, that is just a List<T> array of a class.
So? It's still a basic N-ary tree. And what are you talking about, "Not a very OOP way of implementing blah blah blah"? You asked how to make a tree, I showed you the basics of a tree, it's up to you to figure out what kind of abstractions you want and what you want to store in that tree.

Share this post


Link to post
Share on other sites
Quote:
Original post by capn_midnight
Quote:
Original post by Headkaze
That's not exactly a built in data type, that is just a List<T> array of a class.
So? It's still a basic N-ary tree. And what are you talking about, "Not a very OOP way of implementing blah blah blah"? You asked how to make a tree, I showed you the basics of a tree, it's up to you to figure out what kind of abstractions you want and what you want to store in that tree.


Sorry I wasn't picking at your reply I was just wondering if there was some data type built into .NET that was in the form of a tree, like a Queue or Stack. So when you showed a List<T> it made me realise you were talking about something more abstract.

I was also curious as to how this tree structure would be used in a state machine manager for an OOP programmer, specifically a C# .NET programmer. To me it doesn't seem like the best way to approach it. I think an abstact class you inherit would be a better way to go about it. Part of the reason why I think this question fits well in the C# forums, otherwise you would probably get more approaches from C++ programmers that don't necessary work the best for a .NET programmer. No offense intended.

BTW I found a good article related to this [url=http://www.codeproject.com/cs/library/statemachinetoolkitparti.asp?df=100]here[/url] which seems to take a similar approach to the one I was suggesting earlier. I'm just glad I found this post because it's made me rethink an application I'm currently working on.

Share this post


Link to post
Share on other sites

This topic is 3741 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this