How to call c++ gameMode functions in a HUD blueprint


I’ve created a widget Blueprint called “HUD”. Then I positioned two buttons. Everything around displaying them works fine, but I have problems with calling self written c++ functions.

Let’s say I have a GameMode Class like the following:

#include "Blueprint/UserWidget.h"

#include "GameFramework/GameMode.h"
#include "ICGameMode.generated.h"

class IC_API AICGameMode : public AGameMode
	virtual void BeginPlay() override;

	/** Remove the current menu widget and create a new one from the specified class, if provided. */
	UFUNCTION(BlueprintCallable, Category = "UMG Game")
		void ChangeMenuWidget(TSubclassOf<UUserWidget> NewWidgetClass);

	UFUNCTION(BlueprintCallable, Category = "ownFunctions")
		void myFunc();

	/** The widget class we will use as our menu when the game starts. */
	UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "UMG Game")
		TSubclassOf<UUserWidget> StartingWidgetClass;

	/** The widget instance that we are using as our menu. */
	UPROPERTY(EditAnywhere, Category = "widget")
		UUserWidget* CurrentWidget;

There is my own function “myFunc()” which I have declared as “BlueprintCallable”.
The implementation from ICGameMode.cpp:

void AICGameMode::myFunc() {
	UE_LOG(LogTemp, Warning, TEXT("_____my func called ______"));

When I open the Graph section for my user widget i can select the “myFunc()” methode to be called by the onClick event fired by the button.
When I’m trying to compile the game and play it in the editor, the error message as shown in the image below will be displayed.

I couldn’t finde information on how to set a proper target, which should imo be my ICGameMode.

Is there someone who could give me a hint?

Thank you in advance!

The answer will be a bit tricky.

If you are doing single player game, you can get game mode through global blueprint node “Get Game Mode” and next, cast it to your game mode type:


However, it won’t work if you are doing multiplayer game. In multiplayer, HUD’s live only on client side. However, the only instance of gamemode lives on server side. There is no game mode for clients, so Get Game Mode from HUD will return null for non-server players.

Your conduit for calling functions on the server from clients is PlayerController. Implement RPC function of Server type in player controller that calls appropriate function in GameMode. You can get reference to owning player controller from HUD via GetOwningPlayerController function of HUD.

To get more info about remote functions, look here:

Hi MiKom,

thanks for the advice. Since I’m creating a single player game, I tested it the following way:

It throws the same error as before.
I’m not really sure if the connections between the nodes are correct. These node graphs are a little confusing for me.

route the execution pin (white one) from OnClicked to cast and then from cast (the top one on rigt, meaning cast succeeded) to MyFunc call.

Every node that has this white triangle, in order to be executed, must have the execution route going through it. Execution nodes are ones that in a sense “change” state of the game or have side effects, as opposed to “pure” ones, that have no exec pin. These are ones that shouldn’t have side effects. Like the Game Mode Getter.

Once the blueprint execution reaches non-pure node, that takes some parameters, the parameters are evaluated and given to the exec node.

More generally, Blueprint execution always starts at some starting point node in the graph that has exec output pin (like onClicked here). Then, Blueprint execution machine looks where the pin is connected, and goes there. It looks at the node and checks whether it needs some input (like MyFunc here needs “Target” node). If so, it evaluates the input node. If node has output parameter (like cast here) it is saved on the stack for later use if output is connected to something.

Here, blueprint compiler can see, that you take out parameter from cast and try to pass it to MyFunc. However, cast will never be executed because execution flow doesn’t go through it, so parameter to MyFunc will not be available. Fortunately, blueprint compiler can detect this situation and reports an error.

So many thanks to you for that helpful explanations!

Everything works now as expected. I append a screenshot with my current graph for people who will run into the same problem. Maybe it will be a help for someone.