Download

Static variables possible?

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

He does a great tutorial about static libraries.

Link

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!

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?

Static Const Wiki Tutorial

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

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 :slight_smile:

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

Rama

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

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.

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

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. :slight_smile:

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 :slight_smile:

Rama

1 Like

Oh!

That clears that right up. Thank you!

Good luck!

Have fun with UE4 C++ !

:slight_smile:

Rama

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? - UE4 AnswerHub

Cheers,
Michael Noland

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

Thanks Michael!


EDIT

Michael has this feature been removed? I can't find it in BaseEngine.ini, or in Project Settings->Engine->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

Wiki tutorial is finished! Code and Pictures!

Wiki Tutorial on Global Data Storage

Rama

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

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++?

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