How to communicate an InputAction to a GameMode?

I am trying toggle a menu with a button.
My main part of concern is not the menu, but the communication from the button to the function that toggles the menu.

I made an InputAction (IA_ToggleMenu) which is called in my Character (BP_ThirdPersonCharacter).
This then activated the required function in the GameMode (GM_Main).

On BeginPlay of my Character, I store a reference to the GameMode (cast to GM_Main).
When the InputAction is pressed I simply call the function “ToggleMenu” from the GameMode.

But now, lets say I have three seperate levels.
And they all have a different purpose, so I gave each a different GameMode.
The Character is still the same, and it needs to be able to toggle the menu wherever it is.

IA_Final_03

What do I cast the game mode to?
Or is there a different way of storing a reference to the current game mode, so that I can still call “ToggleMenu”?
Also, this would mean I am needed to have the function “ToggleMenu” in all GameModes.
How do you handle Input in a flexible and simple way?

(I am using blueprints and UE 5.3)

Perhaps the Game Mode is not the best place to reference the widget. How about the venerable HUD class? Each of your Game Modes can use the same HUD class:

image

It’s easily accessible via the PC:

You can make the HUD class the hub of all things UI related.


Or the Game Instance.

Thank you for your reply.
I took some time to look into this HUD class.
And I think I will make a lot more use of it in the future.

I also took some time to review my code structure and changed it around to a new proof of concept.
Now it looks like this:

I now use the PlayerController to intercept the InputActions.
This is because from what I can find online, everyone suggests to use the PlayerController to handle initial input.
This is now also where I store the boolean for the menu being open/closed.

After the InputAction is received the GameMode will check if menus are accessible now.
Maybe there is a cutscene playing, or something else which prohibits menus right now.

If allowed the PlayerController will let the HUD class create the menu.

Then it will tell the GameMode to react on now having the menu open.
This could be pause/unpausing or some other aspect which should function different when a menu is open.

Finally, the PlayerController will tell the character to play a menu animation.
This could be an animation of the character holding out a map in front of him, so that (in multiplayer) other players can see this player is looking at a menu.

It is getting somewhere, but still has the initial problem of how do I store references to the GameMode in the PlayerController?
The only thing I can think of is to have other game modes inherit from GM_Core, so that GM_Chill, GM_Social and GM_Sport would all have functions like ToggleMenuOverviewRequest.

Another thing is that this is just an example of opening a menu.
Further down the line there will be InputActions for interacting with objects, using emotes, AoE abilities etc.
So I am looking for a solid structure to build all of this on with multiplayer in mind.

Any tips for achieving this?
Also if anyone has links to Tutorials/Guides/Documentation on code structure with Unreal please do let me know.

Nevermind, I just realized the GameMode will not be accessible with multiplay (for clients).
So now I am thinking of having the GameMode display neccessary variables in GameState.

For my simple menu toggling example it would look like this:
IA_Final01

When the PlayerController receives an InputAction, it will check GameState for a boolean like “MenuOverviewAllowed”.
And if true it will tell the HUD class to spawn the menu, and tell the character to play its animation.

Now I am still not sure how to tell the GameMode that it needs to respond to this change.
But I suppose and RPC will do.

How do other games structure this communication?
Any example of a good structure?