How can I reference an actor from the scene, inside a custom HUD?

So I have an Actor Blueprint that contains most of the logic of my game. Its called MainGame.

I have a button on the hud and I want to call a method from MainGame.

I made a variable in my hud and exposed it & made it editable but I can’t set that variable with my MainGame instance.

How can I fix this?

P.S. Somebody suggested I put all that game logic into a custom GameState since its always one instance and its easy to get GameState but I am not sure that’s the intended use of GameState.

I would put your MainGame actor reference in your player controller maybe. This way you can have the player controller spawn it if it doesn’t exist or do a quick find by class and store a reference to it if it does. This way your HUD just has to get the player controller, cast it to the proper type, then grab a reference from there.

@RimmyD But isn’t the PlayerController’s job to manage the camera and nothing else?

The player controller can sort of be used for anything you want. The nice thing about the player controller is that you will always have one so it is a nice place to house references and logic you will need for making your game work.

I house most of my input handling and game mode specific logic/references in the PC when using just blueprints. I treat the player controller as a central game manager for most stuff.

Simple Example: I have a button that wants to know when the mouse is clicked on it. Putting this trace into the button is bad since you could have 50 buttons in the map, so a single click springs 50 traces. I instead have a single trace in the PC which gets what is hit and tells that object it has been clicked on (if it cares, i use Interfaces to determine this). This way i get a single trace each click.

Someone may have another way to handle this and I’d be curious to know it :slight_smile: