Updating a widget in multiplayer

Hello everyone,
I’m working on a multiplayer project. The players in the game all have a widget that displays all the players names in a vertical box, before the game itself actually starts.
I have actually no idea about how to make the vertical boxes update when a new player joins the game.
Where should the ‘’ UpdatePlayersList ‘’ event be ? In the widget BP ? In the Game mode ? The game state ? The player controller ? What should I set in replication for this event ?

Try the game state for example, create an array of strings, make it a RepNotify, whenever the server updates the list you will get notified, from the RepNotify get your player controller and access the widget and add the missing names, or clear the list and add all again.

Alternatively you can access the already replicated player list in the game state, you can do a timer and compare with your list in the widget and add the missing players.

Hey there @Idknoidealol! So there’s lots of ways to go about this, but one of the closer to “best practices” ways for multiplayer is that anything that’s going to be replicated to all players UI wise is always going to need a way to pass that information to the players, and much of the time I prefer to put information in the game state or player states, or at least derive the names from it to begin with. Here’s a couple of tutorials that should help you get the idea.

Here’s the documentation for the player state, and then here’s two tutorials.

Disclaimer: The two youtube links are to tutorials created by other unreal users and Epic Games is not liable for anything that occurs outside of the UE site and learning materials.

Simple scoreboard that’s replicated and uses Playerstate to take the names of each player.

One for the UI alone, a bit more basic.

Hope these help!

I have already watched these 2 videos and they helped me at the beginning :slight_smile:

@KaidoomDev
That’s pretty much what I tried and you confirmed me that I was doing right.
Now … I am not able to get a reference to the widget in my player controller.

I have tried to create the widget in the PC but I get the ‘’ only local player controllers can be assigned to widgets ‘’

So I moved it to my pawn BP, then tried a cast to my pawn with get controlled pawn but the cast always failed for all clients for some reason.

Can anyone help me quickly ? I have to finish in the following days and I’m completely blocked right now :////

In your player controller add the IsLocalController check, this should prevent creating a widget on a dedicated server for example.

In your game state you can do something like this

1 Like

I will try this, thanks

I’m running into another issue now when letting players change their names in the menu … RepNotify function only fires on clients and not on server.

What I want to do is to send the new name the player has entered to the server, and the server will set the array of names

I noticed adding and removing from an array doesn’t trigger the Rep notify in a listen server, to force it you’d have to set the array entirely, you can set it by itself, either that or after adding to the array as the server, also update the widget.

Ok I manage to find a solution. I didn’t use RepNotify ; when a player joins or changes his name, the widget calls on event in the player controller executed on server, which calls an event in the game mode.
The game mode then set the array and ask every player to update.
Thanks for your time :))

1 Like