# Problems interacting with GameState

**URL:** <https://forums.unrealengine.com/t/problems-interacting-with-gamestate/1234466>\
**Category:** Programming & Scripting\
**Tags:** gamestate, unreal-engine, question\
**Created:** [July 20, 2023, 8:21am UTC](https://forums.unrealengine.com/t/problems-interacting-with-gamestate/1234466 "2023-07-20T08:21:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![NightmareGames](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/nightmaregames/32/711529_2.png) [@NightmareGames](https://forums.unrealengine.com/u/NightmareGames)\
**Post date:** [July 20, 2023, 8:21am UTC](https://forums.unrealengine.com/t/problems-interacting-with-gamestate/1234466/1 "2023-07-20T08:21:57Z")

</div>

I’m attempting to create a UI that counts and displays the number of enemies in the level. It stands to reason this logic would need to be in some class that transcends any particular actor. After some digging, I came across GameMode and GameState.

It’s my understanding one of the biggest differences is that GameMode only exists on the server. Since each client’s hud would need to track this info, I decided GameState was a better choice.

There’s very little in the custom game state right now:

```auto
UCLASS()
class MECH_API AMechGameState : public AGameStateBase
{
	GENERATED_BODY()

public:
	virtual void BeginPlay() override;
	virtual void Tick(float DeltaTime) override;

	UPROPERTY(EditAnywhere)
	int enemiesInLevel = 0;
};

```

Here in the hud widget’s event graph, I’ve promoted the GameState to a variable so I can access it for UI.

 ![2023-07-20 01_04_17-Mech - Unreal Editor](https://d3kjluh73b9h9o.cloudfront.net/original/4X/6/6/b/66b9e5203f7a92319c43c87d6a411b2c8b20e446.png)

However, I’m unable to access its members from inside this binding.

 ![2023-07-20 00_38_30-](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/4/5/2454c69fb0a8d4c1752a46246b1608bef7af61e4.png)

It’s also my understanding that GameState classes are created automatically as a singleton when the game launches. However, I came across this category in Project Settings and it occurred to me that perhaps I need to manually set the project to my newly-created state class before it will work.

 ![2023-07-20 00_37_23-Mech - Unreal Editor](https://d3kjluh73b9h9o.cloudfront.net/original/4X/d/d/0/dd0a9a1ea00c429e415d2eb676cfd604a81b9de4.png)

However, the option to change any of these values is disabled, save for GameMode. After much more reading, I’m getting the distinct impression that I need to set a custom GameMode before setting a custom GameState (although no one has explicitly said this outright).

Is that true? If so, why? If not, is there another flaw in my approach?

---

<div class="post-metadata">

**Author:** ![Tuerer](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/tuerer/32/1008648_2.png) [@Tuerer](https://forums.unrealengine.com/u/Tuerer)\
**Post date:** [July 20, 2023, 8:30am UTC](https://forums.unrealengine.com/t/problems-interacting-with-gamestate/1234466/2 "2023-07-20T08:30:56Z")

</div>

You need to specify a blueprint visibility property to access variables from blueprints. In this case `UPROPERTY(BlueprintReadOnly)` should suffice.  
`EditAnywhere` does not allow blueprint access.

---

<div class="post-metadata">

**Author:** ![system](https://d3kjluh73b9h9o.cloudfront.net/original/4X/7/1/3/713c9d3f58553f0de89543d76a8a3a2779dc9fa4.png) [@system](https://forums.unrealengine.com/u/system)\
**Post date:** [August 19, 2023, 8:31am UTC](https://forums.unrealengine.com/t/problems-interacting-with-gamestate/1234466/3 "2023-08-19T08:31:49Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
