Static variables in BP?

Is it possible to use static variables in BP? I’m sure there are many workarounds but it would be so much easier to get the variables from the class itself, instead of having to create all these other components/static classes attached to GameMode to retrieve everything.

Its so out of the way, and often times, I realize I wanted to change a var to be static, but now this would mean having to change the whole set up so that it fetches it from the GameMode.

To my knowledge, you can’t create static class variables. It would be nice to have this feature, though I honestly haven’t needed it so far.

Have you thought about having a basic Blueprint function library with pure functions in place of static variables? At least this way the return value of the pure functions won’t change during run time and you don’t need an object instance to call the pure function.

So far I haven’t had the need for static variables. The only reason I’d need one is to store a something like a game/level setting in which case I prefer to use CSV files and UE4 data tables to drive that data instead. Using data tables allows you to tweak the settings without recompiling code (and therefore higher chance of causing regression bugs).

Yeah, I have used a function library called Globals, just to access create global variables, usually to get an actor from the game, and save it, so I only need to call GetAllActorsOfClass(x class)[0] once, to save on performance, though I haven’t thought about creating one for each class and naming it with a convent of BlueprintClassX_StaticVars. I’ll try to implement that now, but it will surely come out cleaner once I make my next game so I can have the bonus from planning ahead.

Anyways, this is a good idea, so thanks.

Coming back to this a few years later, I’ve realised you’re solution is only good for static const variables. If you do want the value to change, then you do have to create an instance or read/write stuff manually, both are incredibly annoying and out of the way.

Hi!
The way I managed to use static variables in Blueprints is through C++.
You can declare a static variable in a C++ UCLASS, and getter/setter UFUNCTIONs for it.
Then, you can have your Blueprint parented to this C++ UCLASS. The Blueprint should be able to call the setter and getter in order to respectively modify and access the static variable, which will be shared among all instances of the Blueprint.

I see this as 50% solution, 50% workaround (it is not Blueprint-only and not necessarily flexible).

Here is a basic example with additions for a UCLASS deriving from AActor:

////////////// MyActor.h //////////////

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "MyActor.generated.h"

UCLASS()
class TESTPROJECT_API AMyActor : public AActor
{
	[...]

	// the static variable
	static int Counter;

	// setter, callable from Blueprint
	UFUNCTION(BlueprintCallable)
		void SetStaticCounter(int InCounterValue);

	// getter, callable from Blueprint (and pure)
	UFUNCTION(BlueprintPure)
		const int GetStaticCounter();    

    [...]
};


////////////// MyActor.cpp //////////////

#include "MyActor.h"

// set value so the variable has a known value :)
int AMyActor::Counter = 0;

[...]

void AMyActor::SetStaticCounter(int InCounterValue)
{
	Counter = InValue;
}

const int AMyActor::GetStaticCounter()
{
	return Counter;
}

[...]
1 Like

this is where unreal sucks

these little details

now I’ll have to have some stupid map in my game to hold data

You´re wrong. Read the documentation and please stop calling Unreal stupid.

Create a blueprint derived of gameinstance. Set this blueprint as your game instance bp in project editor.
You can get a copy of gameinstance at every actor calling getgameinstance and then casting it to you bpgameinstance.
(do a macro if you will use this a lot)
every variable declared in game instance is, in effect, a static one, since there will be only one game instance through the entire game.

Hope this helps…

Another way of doing this is using c++, but c+= does not allow you to edit static variables in unreal editor.

With respect I have to agree with another post that you need to get deeper into the documentation.

C++ static variables exist for all instances as a single copy which fills a conceptual niche in C++. It comes in a different form but this is precisely what Unreal Engine has data tables and game instances for.

I actually find it helpful to share answers, not just tell people they are wrong and to go read more docs. Thus :

Create a C++ blueprint library with a get and set method. In the CPP file add the static variable you wish to edit or retrieve.

Header :

#pragma once

#include "CoreMinimal.h"
#include "Kismet/BlueprintFunctionLibrary.h"
#include "BPFL_WorldUtilities.generated.h"

UCLASS()
class EOS_FPS_API UBPFL_WorldUtilities : public UBlueprintFunctionLibrary
{
	GENERATED_BODY()

	UFUNCTION(BlueprintCallable, Category = "Networking")
		static void SetCurrentLobbyID(FString ID);

	UFUNCTION(BlueprintCallable, Category = "Networking")
		static FString GetCurrentLobbyID();
};

Script :

#include "BPFL_WorldUtilities.h"

static FString LobbyID;
void UBPFL_WorldUtilities::SetCurrentLobbyID(FString ID)
{
	LobbyID = ID;
}
FString UBPFL_WorldUtilities::GetCurrentLobbyID()
{
	return LobbyID;
}

I see a similar answer was given in the comments by @ok_gida

1 Like

As much as I love Unreal, this is just stupid.

The solution you described is far more complicated than it needs to be, and, if I understand correctly, different classes can all access the same static variables. Is this any different than just throwing it all in game mode? It’s disoragnized and could lead to issues because it’s not encapsulated.

The alternative would be to have a singleton for each class that needs it and have a function in each blueprint that gets the singleton, but this is just baffling.

In programming languages, you just type one word and it’s done.

Anything less than a simple check box in the details pane that enables it as static is baffling dumb. All other solutions are workarounds to what is a lack of a feature, that being: static variables, which are restricted to their blueprint, and can even be private. I shouldn’t have to spend 5 minutes engineering a static variable system, and an additional 2 minutes whenever I want a new static variable in a new class.

Absurd.

11 Likes

buy my asset for static variables :wink:

i was looking for the same.
another way might be to have a settings class, expose the variables, and on bp use “get class default” to get it.
that way you can also set them in the settings or once your game has packaged.

1 Like

The closest built-in equivalent BP has to mutable static variables is its mutable default object. This can be accessed in C++ using GetMutableDefault<UMyClass>().

1 Like

(post deleted by author)