Passing Variable

Hello. Emm… I know this is quite… basic stuff but I just can’t figure out how to make it work.

I have my HUD class, and my GameMode class. On the GameMode class I have a variable (int), which it’s value will change eventually, and I want THAT variable to get printed/drawed on my HUD, the problem is that I can’t figure out how to pass that variable.

I’ve included the .h file, creating object pointers and that.


With c++ you can create a UBlueprintFunctionLibrary class, and make a global function to return that GameMode variable, you can use this blueprint function anywhere u want, this my code to get the GameModeType for my game then use it in HUD to display what gamemode i’m in

Declare in header:

UFUNCTION(BlueprintPure, Category = "GameMode", meta = (WorldContext = "WorldContextObject"))
static TEnumAsByte<EGameModeType> GetCurrentGameMode(UObject* WorldContextObject);

in cpp:

TEnumAsByte<EGameModeType> USUBlueprintFunctionLibrary::GetCurrentGameMode(UObject* WorldContextObject)
	if (!WorldContextObject) return EGameModeType::GAMEMODE_NONE;

	UWorld* const World = GEngine->GetWorldFromContextObject(WorldContextObject);
	if (!World) return EGameModeType::GAMEMODE_NONE;

	//Get GameMode if is Server
	AShooterGameMode* GM = Cast<AShooterGameMode>(World->GetAuthGameMode());

	//Get GameMode if is Client
	if (GM == NULL)
		GM = Cast<AShooterGameMode>(World->GetGameState()->GetDefaultGameMode());

	if (GM == NULL) return EGameModeType::GAMEMODE_NONE;

	return GM->GameModeType;

UPDATE: Sorry i thought u want to use in blueprint, You can still use my code the part where you get the GameMode, you can use GetWorld() through HUD class GetWorld() or use GEngine->GetWorld() for anywhere, from GetWorld() u can get GameMode like above.

THANKS! I knew I needed to use the Cast Template, Im not very familiar with it. Your code is very useful BTW, It seems passing variables between classes is not an “easy task” as I thought it was.