In a level I have an actor with a widget to display a 3D menu. I don’t spawn it when the game stars, it is already there.
How can I pass the two-component from the default pawn reference to this actor when the game starts?
Maybe instead of having the actor in the level, I can spawn it and the pass the reference. But my problem is that I don’t know which method (BeginPlay?) from which class (PlayerController?) do I have to use to pass the reference. Or maybe in the Menu’s BeginPlay method I can look for the Default Pawn and get the reference to its components.
I have to do it before game stars because I’m going to use those references on the Menu’s Tick Event.
maybe in the Menu’s BeginPlay method I can look for the Default Pawn and get the reference to its components.
Sure, that would work.
have this actor Get Pawn on Begin Play → cast → get components & reference them
or have this actor reach to the pawn, as above → hook up to a dispatcher (a better solution in case the pawn or its components get destroyed). Here it’s the pawn that updates the menu by broadcasting the necessary data. And the 3d menu merely listens to the updates (if any, which is another bonus - easy to control update frequency)
Maybe instead of having the actor in the level, I can spawn it and the pass the reference.
Also fine, perhaps better than associating the level with the menu - definitely better if you have many levels.
I’d combine your idea of dynamic spawning with the abovementioned dispatcher, but also take it one step further:
have the Game Mode / Player Controller / HUD singleton spawn the 3d menu on Begin Play
have the components themselves broadcast data directly to the 3d menu
Perhaps there is no need to reference the components or get the player BP involved in any of this…
I’m asking this because I’m adapting a Menu actor from the Unreal’s VR Template project. In this template the actor spawns the menu and I was thinking how can I do it if the menu is already spawned in the level. I want to adapt to it to make a Main Menu.
Use the Menu’s onBeginPlay or, if there is widget, you can use its onInitialise.
If you’re considering doing it in the Pawn, you might as well use a Child Actor Component and that’s pretty much it. You have a direct communication line set up, or a dispatcher, or an interface.
Another question to ask yourself: do you actually need an actor to represent the menu? Can the menu be a world space widget component? Note that you can:
And place the entire hook up / broadcast script in the widget component. This gives you the fewest elements to work with. And it’s super compartmentalised.
the widget comp looks up its owner (the player), or their comps
it hooks up the player’s components’ dispatchers to its own widget
the only thing the player comps need to do is to call a dispatcher
I found somewhere that the widget menu should be on an actor if I want to show it in 3D.
Absolutely nope. But it can help to have a dedicated menu actor - perhaps the menu must do some heavy lifting, run timelines, do multiplayer stuff or something more advanced, be reusable in other scenarios.
That’s how you make VR widgets. It’s just a widget component in world space mode - a widget that is displayed on a mesh and can be interacted with via the Widget Interaction Component - in scenarios where you do not have a mouse or don’t / cant use a traditional pointing device.