Jump to content
  • Advertisement
Sign in to follow this  
Lewis_1986

c# state management, need some opinions

This topic is 2690 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

Hi I have a basic application in C# designed to use states to manage application behaviour. Below is my code for the state stuff

Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace StateApp
{
static class Program
{
public static Form1 form1 = new Form1();
public static IState[] states = {new IntroState()};
public static IState curState;
public static KeyEventHandler activeKeyHandler;
public static MouseEventHandler activeMouseHandler;
public static int count = 0;

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
curState = states[0];
Application.EnableVisualStyles();
Application.Run(form1);
}
}
}


IntroState.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace StateApp
{
public class IntroState : IState
{
public IntroState()
{
this.Name = "IntroState";
Program.count = 0;
}
public void Update()
{
Program.count++;
Program.form1.BackColor = System.Drawing.Color.Azure;
}
public void Cleanup()
{
Program.count = 0;
}
public void Pause()
{

}
public void Resume()
{

}

public string Name { get; set; }
}
}


IState.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace StateApp
{
/// <summary>
/// A State Object Abstract from drawing and eventhandling
/// by working only with data and external application settings
/// </summary>
public interface IState
{
void Update();
void Cleanup();

void Pause();
void Resume();
string Name { get; set; }
}
}


the idea of the design is that using the Program class each state can select eventhandlers, methods etc and just work on the data aspect of the program selecting the eventhandlers to use, which also change data based upon events such as input.

Share this post


Link to post
Share on other sites
Advertisement
Using global variables like you are doing is poor design. A simple alternative is to pass references to any required data to individual states. Related data can be wrapped in an object and passed as a single parameter.

Share this post


Link to post
Share on other sites
thanks guys, I soon realised it was a poor system, I have since switched to C++, and I am using pointers and references ;). Eventually I want to recompile it as a java app but as with c#, pointers et al become a major pain in my butt in managed code...

The new system has a globally declared data class that all the other classes interact with and update. Aside from the states which are basically different ways of interacting with the data, it also has an inputHandler for network input and peripheral input, and an outputHandler for drawing, sound and sending network messages. The point of the system is to be able to write applications, games and interactive media within the framework and maybe turn it into a vm at some point. It's all very data centric because that way I figure it is easier to play with.




Using global variables like you are doing is poor design. A simple alternative is to pass references to any required data to individual states. Related data can be wrapped in an object and passed as a single parameter.

Share this post


Link to post
Share on other sites
If you are interested in feedback on your new design you should post it. However, you are still depending on global data, which indicates that your design is essentially the same as your earlier proposal.

The design of this really depends on how it is to be used. Designing such things in a vacuum can be a bad idea as when you actually go to use it you'll find some fundamental flaw in the design.

Instead, start to write a game. Using the guiding principle of "You ain't gonna need it" (YAGNI), don't write stuff that isn't necessary. Your design will naturally evolve into something usable. This isn't to say that you'll come up with a perfect design, certainly not, but the important thing is that you'll minimise wasted effort in directions which aren't conducive to writing games.

Basically, write games, not engines. Once you have a working game, lurking underneath it is something that could turn out to be a nice engine, if you work hard to separate it from the game specific logic.

Share this post


Link to post
Share on other sites

thanks guys, I soon realised it was a poor system, I have since switched to C++, and I am using pointers and references ;). Eventually I want to recompile it as a java app but as with c#, pointers et al become a major pain in my butt in managed code...

The new system has a globally declared data class that all the other classes interact with and update. Aside from the states which are basically different ways of interacting with the data, it also has an inputHandler for network input and peripheral input, and an outputHandler for drawing, sound and sending network messages. The point of the system is to be able to write applications, games and interactive media within the framework and maybe turn it into a vm at some point. It's all very data centric because that way I figure it is easier to play with.



[quote name='rip-off' timestamp='1305130815' post='4809478']
Using global variables like you are doing is poor design. A simple alternative is to pass references to any required data to individual states. Related data can be wrapped in an object and passed as a single parameter.

[/quote]
I hope you realize you didn't have to switch languages to be able to use pointers. The .NET CLR does this for you automagically by using the ref keyword if you're using C#. But, if you're doing well using C++ all the more power to you.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!