How to run Event from WidgetClass in PlayerController

How to run Event from WidgetClass in PlayerController?

i mean there is Event in WidgetClass so i need to run it in Player controller?
How to do that?

in PlayerController:

in WidgetClass:

Your code is halfway there. All you are missing is to call the event you created in the widget class. For instance, I created a CustomEvent in MyWidget called MyWidgetCustomEvent, and I can access it as shown.


However, there is a catch with using the widget like this. Because we need to make sure the widget is loaded in memory before we attempt to use its function(s). In my case, I added my widget to the viewport in my level blueprint, and I instructed my controller to wait 3 seconds after its creation so I know by the time I try to access the widgetBP, it will be loaded in memory already. But this is a cheating way to do things and I would definitely opt for a more stable method for production.

i think your BPs in GameMode right? i need to do it in PlayeController

image

1 Like

The BP I show is a PlayerController.

1 Like

i did same as you but there is an error. Where i lost. Please help me

in GameMode:

in Player controller:

in WidgetClass:

error is:

1 Like

I think I know the reason. Because the widget you created is never loaded. You can fix this by adding these to your Level Blueprint.

my game needs to change levels so i think creating Widget in map is not suitable for it. Can you create widget in other places?


set TopLevelOnly is false, and try again.

i added but its still same

PlayerController:

Map:

Error:

i added but its still same

PlayerController:

Map:

Error:

1 Like

Did you check the World Outliner and make sure the widget is loaded at your level?

Creating a widget in the level has the benefit of easy access to everything on the map. You can create a widget every time you load a new level and that should solve the problem. That being said, I have not tried to do it anywhere else so I don’t know if there is a better location.

1 Like

I reproduced your error report. I’m sorry I didn’t see it clearly the first time. Your project was an RPC Demo. So I need to know more about whether the Widget is owned by each client or unique to the server.

1 Like

@goderyu its owned by each user

Have your player controller create the widget on BeginPlay (and make sure to only create it for the local player by adding a branch with IsLocalPlayerController) and store the widget in a variable of the player controller. Then you can use the variable later on to call functions on the widget

@Fluppi393 can you show on Blueprints?