Simple Game State

I think this is a basic problem that I just can’t figure out for myself.

I want to create a simple game state that will allow me to get and set the game state from within my other classes (such as Character, and various Actors).

I have something along these lines for my methods and member (which I have a feeling is totally wrong):



enum eGameState {TimePlay, TimePause, TimeRewind} eCurrentGameState;
eGameState GetGameState();
void SetGameState(eGameState StateToSet);


But how can I set this up so that I can access the TimePlay, TimePause, TimeRewind states from within other classes and I am always told that they are undefined.

Go easy on me, I’m quite the newbie!

Thanks a lot!

enum should be put in a namespace.


UENUM()
namespace ESomeEnumNameSpace
{
	enum State
	{
		STATE_ONE,
		STATE_TWO,
	};
}

And you need to override the AGameState:: class and set the new default game state class in your AGameMode:: class.

I’m still a little confused about this after trying to implement it today.
To specify what I would like to achieve is to be able to have a global gamestate that can be changed/queried by any of my classes.

I just want to say thanks for replying, and although it seems to be just what I was after, I’m having a little bit of trouble implementing it.
Would you mind breaking it down for me, so far I have the code below in my MyGameState.h (above the UCLASS())


UENUM()
namespace nGameState
{
	enum eGameState
	{
		TimePlay,
		TimePause,
		TimeRewind
	};
}

And I thought that I could simply refer to these in my other classes (once the MyGameState.h was included)
For example, to change the state from play to pause inside a function I would have the following.


if (nGameState::TimePlay)
{
	nGameState::TimePause;
}

Thanks again for the help, I think I’ve misunderstood so if you wouldn’t mind be rather transparent about your explanations!

Hi,
A couple of things. First of all, there is a built in class which handles game state called AGameState. As says you need to override this. You can do this via the editor which will produce code that looks similar to this:



UCLASS(...)
class YOURPROJECT_API AMyGameState : public AGameState
{
	GENERATED_UCLASS_BODY()
};


Secondly, this code doesn’t really do anything:



if (nGameState::TimePlay)
{
	nGameState::TimePause;
}


In order to use an enum you need to create an instance the same way you need to create an instance of any variable to use it. So your last function would like:



if (eInstance == nGameState::TimePlay)
{
	eInstance = nGameState::TimePause;
}


Yeah, I have created a GameState class and think i’ve overridden it correctly from my GameMode class. And yeah, I had a feeling that the code I showed was somewhat pointless.
The thing about the Instance is that I want to have a global game state, but I’m not sure if that fits the bill. I’d like to be able call the eInstance in any of my classes. Does that make sense?

For anything global, as in across maps and everything you will want to extend the GameInstance class as far as i am aware, as it is created once on startup, and destroyed on close, so it persists through all maps and gameplay changes, at least that is how i understand it so far :slight_smile:

Posted PM for anyone else that may need the answer down the road.

Its actualy better to bump the thread that way anyone else wondering about the same can find the answer.

What you want to do is add a member to your class.


UPROPERTY()
      TEnumAsByte<ESomeEnumNameSpace::State> CurrentState;

Edit: Just noticed that you are not using the E prefix for the Enum namespace.
Am not sure it needs to be a captital leter anymore but it defenetly need a “E” in front.


UENUM()
namespace EMyGameState
{
	enum GameState
	{
		TimePlay,
		TimePause,
		TimeRewind
	};
}