# How do I use a Game Instance to have a global variable, or what's an alternative method to accomplish what I want to do?

**URL:** <https://forums.unrealengine.com/t/how-do-i-use-a-game-instance-to-have-a-global-variable-or-whats-an-alternative-method-to-accomplish-what-i-want-to-do/1877958>\
**Category:** Blueprint\
**Tags:** unreal-engine, question\
**Created:** [May 29, 2024, 1:12am UTC](https://forums.unrealengine.com/t/how-do-i-use-a-game-instance-to-have-a-global-variable-or-whats-an-alternative-method-to-accomplish-what-i-want-to-do/1877958 "2024-05-29T01:12:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![c67f](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@c67f](https://forums.unrealengine.com/u/c67f)\
**Post date:** [May 29, 2024, 1:12am UTC](https://forums.unrealengine.com/t/how-do-i-use-a-game-instance-to-have-a-global-variable-or-whats-an-alternative-method-to-accomplish-what-i-want-to-do/1877958/1 "2024-05-29T01:12:46Z")

</div>

I’m modifying the base FPS game project and I’m trying to make it so that when the player hits an enemy with a projectile from their gun, they gain back their ammo. My general idea for doing this was a variable that gets changed when a player projectile hits an enemy. What is the most straightforward way to do this? From what I found online, using a Game Instance to create a global-ish variable might work, but I can’t figure out how to do it.

---

<div class="post-metadata">

**Author:** ![robert2003b](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@robert2003b](https://forums.unrealengine.com/u/robert2003b)\
**Post date:** [May 29, 2024, 3:42pm UTC](https://forums.unrealengine.com/t/how-do-i-use-a-game-instance-to-have-a-global-variable-or-whats-an-alternative-method-to-accomplish-what-i-want-to-do/1877958/2 "2024-05-29T15:42:09Z")

</div>

Remember to initialize the PlayerAmmo variable with a starting value at the beginning of the game, and ensure that your HUD or ammo display is updated whenever the ammo count changes. Open MyGameInstance and add a new variable, let’s call it PlayerAmmo. Set the variable type to Integer and make it public so other Blueprints can access it. When a hit is detected, cast to MyGameInstance and increase the PlayerAmmo variable by the desired amount. In your player Blueprint, you can access the PlayerAmmo variable by casting to MyGameInstance.

Event Hit (On Projectile Blueprint)  
Cast to MyGameInstance  
Get PlayerAmmo  
Set PlayerAmmo (PlayerAmmo + AmmoReward)

Event Any (On Player Blueprint)  
Cast to MyGameInstance  
Get PlayerAmmo  
Update Ammo Display

---

<div class="post-metadata">

**Author:** ![c67f](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@c67f](https://forums.unrealengine.com/u/c67f)\
**Post date:** [June 1, 2024, 12:37am UTC](https://forums.unrealengine.com/t/how-do-i-use-a-game-instance-to-have-a-global-variable-or-whats-an-alternative-method-to-accomplish-what-i-want-to-do/1877958/3 "2024-06-01T00:37:21Z")

</div>

Thank you!! That seems to have worked.

---

<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:** [July 1, 2024, 2:28am UTC](https://forums.unrealengine.com/t/how-do-i-use-a-game-instance-to-have-a-global-variable-or-whats-an-alternative-method-to-accomplish-what-i-want-to-do/1877958/5 "2024-07-01T02:28:40Z")

</div>

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