Download

Global Variables

There have been many threads on global, easy to access variables and most point to either interfaces or creating a “variable bank” object. We ended up using the variable bank option for our latest project and it works great but gets a bit old having to call several nodes to read/set one variable. Not only that, blueprints tend to start to look like spaghetti after a while. After some playing around, we have found a much easier option that is working great for our next project. To many, this will seem like it should have been way easier to figure out but there are not many tutorials that keep it simple so I figured I would share what we are doing for variables that need to be shared across blueprints/levels.

I will keep these steps simple since there are many tutorials one how to create custom classes/nodes out there.

Step One: Create the C++ Class

CreateClass1.png
70e0723e022e2d8768abfc49983a5d3ba2b97ef4.png
745269b768cc6b189f440c5fdce12f9149bb83fc.png

Step 2: Once you have the .h and .cpp files generated and ready to code, create the callable and pure nodes.

.h file



// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "Kismet/BlueprintFunctionLibrary.h"
#include "GlobalVs.generated.h"

/**
 * 
 */
UCLASS()
class GLOBALVARS_API UGlobalVs : public UBlueprintFunctionLibrary
{
	GENERATED_BODY()

public:

//Setup Set Node
UFUNCTION(BlueprintCallable, meta = (DisplayName = "Set Player Vars", Keywords = "Player Vars"), Category = Game)
	static void SetPlayerGlobals(int32 iPHealth, int32 iPStam, bool bIsPAlive, int32 iPMoney);
	
//Setup Read Only Node
UFUNCTION(BlueprintPure, meta = (DisplayName = "Get Player Vars", Keywords = "Player Vars"), Category = Game)
	static void GetPlayerGlobals(int32& iPHealth, int32& iPStam, bool& bIsPAlive, int32& iPMoney);

	
	
};


.cpp file

The Global Variables can be placed in the .cpp file just after the #includes. For those of you who noticed that the class name isn’t the same as the screen shots, it was causing an error in VS and I created a new one to finish this post.



// Fill out your copyright notice in the Description page of Project Settings.

#include "GlobalVars.h"
#include "GlobalVs.h"

//Start of Variables
int32 iPlayerHealth;
int32 iPlayerStam;
bool bIsPlayerAlive;
int32 iPlayerMoney;

////Set global variable function
void UGlobalVs::SetPlayerGlobals(int32 iPHealth, int32 iPStam, bool bIsPAlive, int32 iPMoney)
{
	iPlayerHealth = iPHealth;
	iPlayerStam = iPStam;
	bIsPlayerAlive = bIsPAlive;
	iPlayerMoney = iPMoney;

}

////Get global variable function
void UGlobalVs::GetPlayerGlobals(int32& iPHealth, int32& iPStam, bool& bIsPAlive, int32& iPMoney)
{
	iPHealth = iPlayerHealth;
	iPStam = iPlayerStam;
	bIsPAlive = bIsPlayerAlive;
	iPMoney = iPlayerMoney;

}


Step 3: Use the Get and Set nodes however you wish.

This is just and example and creates all of the variables in just two nodes, one to set, and one to read. You can use the same setup and create a read and set node for each variable if needed/wanted.

b2c38965be89e1e7d0773a6e6def2baad1e4d2b4.png
4d29deabcf9be39175d2a6b71c16ead577d82302.png