Function libraries: How to store class variables

In many cases I just want to store logic in a blueprint, essentially some functions along with some variables (accessable for ALL the functions). Essentially I like them to use like a static class.

So I tried to use class blueprint derived from Actor for everything (which doesn’t really make sense to me). But that’s really complicated and error-prone because I have to instantiate all of my ‘code containers’ by hand in each scene.

I can’t use macro libraries either, because there I can’t use variables AT ALL (not even local variables).


I essentially just want a function container with shared variables. Does anyone know how to achieve this?

Thank you,

Kind of old, but did you manage to find a good way to achieve this?

You could create a Master Actor which have all the variables you want to use. Then when you want to create a new actor you can create a Child Actor of the Master Actor instead of a generic Actor. Then all your actors will inherit the variables, function etc which are created inside the Master Actor. In theory the Master Actor will act as both function an variable library.

hm, yes for sure. I guess you can use Game Mode as well, which is both consistent during gameplay and accessible from every Blueprint (actors and UMG) by “Get Game Mode” → “Cast to [AssetName]”.

That sounds good. Although I believe storing a shared value so all actors (and UMG Widgets) can access it would still be a problem.
After some research, I ended up going the Game Instance approach. I think I’ll work nicely.

I came up with a way by creating a function with a static variable in its scope. At a glance it actually looks very ugly to me, seeming to re-run initialization and go out of scope. But as a static variable it actually persists and doesn’t re-initialize. If your function has a parameter for setting that defaults to an appropriate value, and has a return value for getting, you can use it in a Blueprint Function Library to act as a static variable. Although I don’t find the solution to be pretty at its core, you can wrap it up with your own functions and have a solution that doesn’t use a dummy Actor or global object like Game Mode.

FString UMyBlueprintFunctionLibrary::AccessMyString(FString MyString = TEXT(“”))
{
static FString MyStringValue = FString();

if (!MyString.IsEmpty())
{
	MyStringValue = MyString;
}
return MyStringValue;

}