Spawning a number of actor from a variable of player state (multiplayer)

Hello everyone!
I am sorry to ask for help but I am learning UE4 and I can’t figure out the following question.

I have a problem to solve.

In my multiplayer game, my characters take coins.
It works, the coin is added to the correct character for the server or client (which overlaps it).

When a character take damage, he lost 3 cubes (steak) and reduce his walk speed, he has to overlap the 3 cubes steak to take his right velocity.
BUT, when he take damage, i wanted him to drop all of his coin.
The variable of coin is on player state, but i don’t know how to setup the drop (spawn of the actor) of the number of coin compared to the variable. I tested many solution but nothing work.

I already have the system for spawning the coin at X range of the character, this work if i want to spawn a coin directly by the BP, but i don’t know how to spawn it by the variable in player state.

[Global Bp Character]
Imgur

[Spawning Coin problem in orange]
Imgur

[Spawning Coin when he take damage]
Imgur

Okay so if I understand correctly, you want the character to lose all the coins when they’re hit.
Tbh I’m learning UE4 too but, is does one of the solutions you tried involve using a Switch or Gate Node?

Hi Iamlazar

Networking in UE4 can be a challenging topic to get your head around. I have taken a look at your blueprints and I will start by providing a few tips.

Server is king!

For information to get passed between clients, the server must know about it. You want to ensure the server knows of any events and data so it can broadcast (replicate) these actions to any clients.

You also need to be aware of which objects can have authority (access the server). Consider this example:
When a character (pawn) is spawned. The pawn on the owning client will have authority and can communicate with the server. However, an instance of the same pawn will be spawned on the other clients, except these instances will be known as “Remote” (Without owner). Actors without an owner cannot get the authority to message the server and only receive updates through multicast events or natural replication.

Using a Player State

The player state is intended to be a data class. You can think of the PlayerState as a Facebook profile for its owning Player Controller as even the server instance of a Player Controller can only be accessed by the owning client.
Variables like “AmountOfCoins”, “CurrentScore” etc are acceptable to be inside the Player State as other clients may need to know this information, however, the conditions and logic to control these variables should be held in the more appropriate classes such as Player Controller or Character.

Logs are your friend

Keep a very close eye on your logs, I am guessing you may have several warnings stating that the data you are trying to access is not where you think it is. When running in a stand-alone game you can add the additional launch parameter “-log” which will also expose the live log for the server and any running clients.


To answer your question, I would suggest an approach similar to this:

Character class

Oops: The switch has authority comment in the first screenshot should read “Ensure REMOTE pawns do not attempt to call the server”.

Player Controller class

Player State class

The only thing I added was a replicated variable to store the number of coins.

This is quite a simplistic setup, but it should give you some idea of how to approach communication between objects on the server and client.

I personally attempt to avoid using world objects to affect classes such as Game Modes / Player Controllers etc.
I.e. instead of BP_Coin telling the pawn what to do. The pawn should receive a response from the coin and then the pawn decides what it should do. I usually handle these relationships through delegates (event dispatchers).

Good luck

Alex

Man…
MAN!!
You are the best, this work great!! Thanks for your time really! It’s perfect!
It’s a work for a Master Degree, it’s perfect!
Just to know, if I want to get the variable to put it on a widget (number of coin added), I can do that : Imgur: The magic of the Internet

I have so a last question
If I want to take the other character number’s of coin added, how I get it (To add on the same widget, I want all player see the number of coin of other player)?

Sorry for my bad english and thanks a lot on more time!

Hi Iamlazar

I am glad you found my answer helpful. If you wouldn’t mind, could you please upvote it and mark it as the correct answer, so other people can find it who may have similar issues :slight_smile:

if I want to get the variable to put
it on a widget (number of coin added),
I can do that :
Imgur: The magic of the Internet

Yes, you can do that. However, it is far more complicated than it needs to be. I think you are overthinking a lot of your logic.

Widgets only exist on the client. The server does not care about them at all, this is why gameplay logic should be placed in the appropriate objects such as the player controller, game mode, character etc.

If your goal is just to get the variable from the player state, you can simply do this.

  1. In the widget, store a reference to the player state directly from the player controller

  1. Bind the variable from the player state.

If you want to display all the coins collected by all characters, this will require a bit more setup. There are a few approaches you can take with this.

Unfortunately, this page won’t allow me to post all the screenshots etc. So I dumped my solution in a PDF.

[link text][3]

Good luck with your project!

Alex

Hi Alex! A BIG THANKS for you work, it’s amazing. I do everything like you (pdf) but this doesn’t work. I put the widget here, i can do a mistake?

Imgur

Thanks a lot for your patience! ^^

Hi Iamlazar.

No problem. It can be difficult to understand networking in Unreal if you’re not used to it.

Just like your previous example, I think you are over complicating things.

In the second pin of the sequence, you are accessing a variable player cast, getting the owning player controller, and getting the ID.

You are already inside the Player Controller blueprint. Player controllers are only accessible by the owning client, so this check is not needed.

You also need to check your authority level. Begin Play on the Player Controller will execute both on the server and client. Widgets can only be seen by the client so make sure this is only running on the owning client.

I also see you are not storing a reference to the widget after it has been created (The return value from the ‘Create WBP Player HUD widget’ node). If you do not store the reference, you cannot access it later as the variable will always return null.

So in this screenshot from my PDF, the variable “PlayerHUDWidget” would have been set to a return from a “Create Widget” node.

One other note as well. The Game Instance is only visible to the server-side of objects (like the game mode), so make sure you’re not trying to access any properties on the client. If you do need to access a Game Instance property on the client, set the variable on the server and have it replicate down.

All the best.

Alex