# Static variables possible?

**URL:** <https://forums.unrealengine.com/t/static-variables-possible/6841>\
**Category:** C++\
**Tags:** unreal-engine\
**Created:** [June 29, 2014, 9:02pm UTC](https://forums.unrealengine.com/t/static-variables-possible/6841 "2014-06-29T21:02:07Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![shawkes](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@shawkes](https://forums.unrealengine.com/u/shawkes)\
**Post date:** [June 29, 2014, 9:02pm UTC](https://forums.unrealengine.com/t/static-variables-possible/6841/1 "2014-06-29T21:02:07Z")

</div>

I’ve looked all around, including the wonderful tutorials done by Rama.

He does a great tutorial about static libraries.

[Link](https://wiki.unrealengine.com/Static_Function_Libraries,_Your_Own_Version_of_UE4_C%2B%2B,_No_Engine_Compile_Times)

I’m wondering if it is also possible to have static variables/properties? More specifically I am trying to have a static TArray that stores all instances of my class.

All methods I’ve attempted result in no build… is there a UE4 way of doing this or is it simply not supported?

Thanks!

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_518c3a4c](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_518c3a4c](https://forums.unrealengine.com/u/anonymous_user_518c3a4c)\
**Post date:** [June 29, 2014, 9:47pm UTC](https://forums.unrealengine.com/t/static-variables-possible/6841/2 "2014-06-29T21:47:14Z")

</div>

Static objects is C++ feature, there is no way it can’t be supported. Some pieces of custom libraries may not work with static objects though, reflection for instance.  
I’m not sure if static UPROPERTY would work actually, i have not tried it.

What have you tried and what errors did you get?

---

<div class="post-metadata">

**Author:** ![Rama](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/rama/32/882413_2.png) [@Rama](https://forums.unrealengine.com/u/Rama)\
**Post date:** [June 29, 2014, 10:08pm UTC](https://forums.unrealengine.com/t/static-variables-possible/6841/3 "2014-06-29T22:08:12Z")

</div>

**Static Const Wiki Tutorial**

You can make Static Const variables, I have wiki tutorial here 🙂

> **[A new, community-hosted Unreal Engine Wiki](https://forums.unrealengine.com/t/a-new-community-hosted-unreal-engine-wiki/141494)**
>
> After over a year in maintenance mode, the official Unreal Engine Wiki is now permanently offline. These resources now live on a new community-run Unreal Engine Community Wiki — ue4community.wiki! You will be able to find content from the official...

I am not sure if a Static TArray even makes sense by the nature of the fact that a TArray is so dynamic in its focus.

I personally just use a regular UPROPERTY() TArray and store it in my Player Controller, and access things I need via the player controller 🙂

Makes it easy to then go into Blueprints and add new properties / add new entries to the TArray later in the Defaults tab 🙂

Rama

---

<div class="post-metadata">

**Author:** ![shawkes](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@shawkes](https://forums.unrealengine.com/u/shawkes)\
**Post date:** [June 29, 2014, 10:29pm UTC](https://forums.unrealengine.com/t/static-variables-possible/6841/4 "2014-06-29T22:29:26Z")

</div>

Rama,

I will try those things. However, as far as wanting static… I suppose it’s because I can’t imagine why it would be defined in the instance of any class.

To get into my code more, I have created a static library like the one in this other tutorial:

[Link](https://wiki.unrealengine.com/Blueprint_Function_Library,_Create_Your_Own_to_Share_With_Others)

Inside of my library, I’ve created a helper function that wraps up the spawning of certain actors, calls a function on the actors and (hopefully) adds those actors to an array. I was hoping to have that array exist in the library (and therefore be static) such that I can access the array from other places.

Maybe this is a bad approach.

---

<div class="post-metadata">

**Author:** ![Rama](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/rama/32/882413_2.png) [@Rama](https://forums.unrealengine.com/u/Rama)\
**Post date:** [June 29, 2014, 10:42pm UTC](https://forums.unrealengine.com/t/static-variables-possible/6841/5 "2014-06-29T22:42:00Z")

</div>

> [@shawkes;86818](#):
>
> such that I can access the array from other places.

You can make a static function to access the instance of your class that is storing your data 🙂

But you will need to instance something, if you want to dynamically change the data during runtime!

Static stuff by definition can’t change during runtime, so you can’t spawn stuff and then add it to a static array, that’s all runtime activities requiring a runtime instanced dynamic array. 🙂

Epic had posted a tutorial on the topic of loading assets during runtime but I can’t seem to find it… I’ve already stated my preferred method of storing things in a Blueprinted Player Controller 🙂

Rama

---

<div class="post-metadata">

**Author:** ![shawkes](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@shawkes](https://forums.unrealengine.com/u/shawkes)\
**Post date:** [June 29, 2014, 10:42pm UTC](https://forums.unrealengine.com/t/static-variables-possible/6841/6 "2014-06-29T22:42:49Z")

</div>

Oh!

That clears that right up. Thank you!

---

<div class="post-metadata">

**Author:** ![Rama](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/rama/32/882413_2.png) [@Rama](https://forums.unrealengine.com/u/Rama)\
**Post date:** [June 29, 2014, 10:51pm UTC](https://forums.unrealengine.com/t/static-variables-possible/6841/7 "2014-06-29T22:51:29Z")

</div>

> [@shawkes;86825](#):
>
> Oh!
> 
> That clears that right up. Thank you!

Good luck!

Have fun with UE4 C++ !

🙂

Rama

---

<div class="post-metadata">

**Author:** ![Michael\_Noland](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/michael_noland/32/1194689_2.png) [@Michael\_Noland](https://forums.unrealengine.com/u/Michael_Noland)\
**Post date:** [July 1, 2014, 12:44am UTC](https://forums.unrealengine.com/t/static-variables-possible/6841/8 "2014-07-01T00:44:33Z")

</div>

Heya,

There’s also the concept of a game singleton class, which the engine will create for you at startup. Have a look at this answer from Ben: [Where to store global variables? - Mobile - Epic Developer Community Forums](https://answers.unrealengine.com/questions/28278/where-to-store-global-variables.html)

> [@](#):
>
> Another good place to put globals is in a GameSingleton. If you set GameSingletonClassName to something in DefaultEngine.ini (or Project Settings -\> Engine -\> General settings) it will create an instance of that object for you. You can get then get to it from the variable GameSingleton on UEngine. On Fortnite we set up a game singleton, and then added something to our kismet library to get it on demand. If you subclass public UBlueprintFunctionLibrary, those functions will be available from any blueprint. It creates exactly one game singleton ever, so it’s a good place to put data references that are global across your entire game. You can make that Singleton Class point at any blueprint you may want, and then you can set variables on that as needed.

Cheers,  
Michael Noland

---

<div class="post-metadata">

**Author:** ![Rama](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/rama/32/882413_2.png) [@Rama](https://forums.unrealengine.com/u/Rama)\
**Post date:** [July 1, 2014, 12:19pm UTC](https://forums.unrealengine.com/t/static-variables-possible/6841/9 "2014-07-01T12:19:15Z")

</div>

> [@Michael Noland;87453](#):
>
> Heya,
> 
> There’s also the concept of a game singleton class, which the engine will create for you at startup. Have a look at this answer from Ben: [Where to store global variables? - Mobile - Epic Developer Community Forums](https://answers.unrealengine.com/questions/28278/where-to-store-global-variables.html)
> 
> Cheers,  
> Michael Noland

Oh wow that’s awesome, I will do a wiki on this soon as I set it up on my end 🙂

Thanks Michael!

```auto

EDIT

Michael has this feature been removed? I can't find it in BaseEngine.ini, or in Project Settings-&gt;Engine-&gt;General

EDIT 2

I do see it in the source code for Engine.h, but the fact that it is absent from the Project Settings menu makes me wonder if it is being phased out somehow?

EDIT 3

Nvm it was hidden under "advanced" properties, God I wish that thing did not exist, and I wish there was a way to always see all properties.

Rama
```

---

<div class="post-metadata">

**Author:** ![Rama](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/rama/32/882413_2.png) [@Rama](https://forums.unrealengine.com/u/Rama)\
**Post date:** [July 1, 2014, 2:00pm UTC](https://forums.unrealengine.com/t/static-variables-possible/6841/10 "2014-07-01T14:00:37Z")

</div>

**Wiki tutorial is finished! Code and Pictures!**

**Wiki Tutorial on Global Data Storage**

> **[A new, community-hosted Unreal Engine Wiki](https://forums.unrealengine.com/t/a-new-community-hosted-unreal-engine-wiki/141494)**
>
> After over a year in maintenance mode, the official Unreal Engine Wiki is now permanently offline. These resources now live on a new community-run Unreal Engine Community Wiki — ue4community.wiki! You will be able to find content from the official...

Rama

---

<div class="post-metadata">

**Author:** ![robbiecooper](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@robbiecooper](https://forums.unrealengine.com/u/robbiecooper)\
**Post date:** [January 3, 2015, 10:05am UTC](https://forums.unrealengine.com/t/static-variables-possible/6841/11 "2015-01-03T10:05:13Z")

</div>

Hey Guys

I was wondering if you could take a moment to look at this;

[https://answers.unrealengine.com/questions/152680/interface-in-c-can-you-pass-variables-between-obje.html](https://answers.unrealengine.com/questions/152680/interface-in-c-can-you-pass-variables-between-obje.html)

In particular the answer that I posted to my own question. I’ve tested this stuff in the editor and it all works fine. It also seems to correspond to how the word “static” is used in general C++. However, having just come across this convo, I’m wondering if my solution is going to fail after cooking or something. Is the static keyword different in UE4 than in general C++?

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_98778e19](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_98778e19](https://forums.unrealengine.com/u/anonymous_user_98778e19)\
**Post date:** [February 8, 2017, 6:34am UTC](https://forums.unrealengine.com/t/static-variables-possible/6841/12 "2017-02-08T06:34:51Z")

</div>

Static stuff by definition can’t change during runtime, so you can’t spawn stuff and then add it to a static array …  
[/QUOTE]

What? ^^ You mean const (or static const), not simply static, right? 😉
