Jump to content
  • Advertisement
Sign in to follow this  
Ekim_Gram

[.net] Way to simulate globals in C#? Gamestate managed between a number of classes

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

One word - Gamestates. They were simple in C++. You declare an enum and a global to represent it. C# doesn't support globals though, which puts me in a situation. I wish I were able to remember what I read but at this time of the night I cannot skim through every page related to OOP or anything similar. So my dilemma is how can I manage a gamestate that I would like all of my classes to be able to modify?

Share this post


Link to post
Share on other sites
Advertisement
Eh, one cheap trick to make something global in a namespace is to just make a class with static members for the data you need. C# 2.0 will support static classes as well which is what would be more applicable.

Share this post


Link to post
Share on other sites
Yep, make it static. Another method I heard is more OOP and basically involves IGameState and have things like FightingState, PauseState implement it.

Share this post


Link to post
Share on other sites
A better solutiong than using a global would be to maintain the state in some class that is referenced by all the other classes that need to access the state. Yes, it's a little bit more work, but being able to have more than one game state and to be able to use Contruction/Finalization symantics with it can lead to features you may not have though of otherwise.

Share this post


Link to post
Share on other sites
You could just make a class with static protected variables and have all your main classes that use the states inherit from it?

~Graham

Share this post


Link to post
Share on other sites
For game states I usually end up creating a singleton state manager so I can retrieve or set it at any time.

StateManager.Instance.CurrentState = GameStates.Paused;



--Pinz

Share this post


Link to post
Share on other sites
I implemented a dictionary key-value class that was serializable that I use for global configuration values: DynamicLoader.

You can store any object in the class and retrieve it with the set key. You can then save the data on the disk in a binary file format. Perfect for configuration files and saved games. As for global values, I'd recommend using static variables, or implementing a singleton design.

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!