Weapon Spawner

I’m Trying to get the weapon to Rotate when the weapon spawns at the spawner. Everything replicates except the “add Rotating Movement Component”. I can’t set it to rotate in the weapon Blueprint because the weapon will spin while the character is holding it. The Server side works just fine. If both authority and remote run this blueprint script the client spawns both server (not spinning on client) and client weapon(spinning and appearing only on client). Please help!

Hi Fatherg00se

The first thing I noticed, is you are doing everything in one place. When working with networked games, you need to ensure you are using classes appropriately. I always recommend creating functionality on the objects themselves and allow the server to tell them to update when required.

I have set up a very quick project to show you.

  1. Create a static mesh actor called BP_Gun.
  2. Add a function to spawn the rotation component.
  3. Configure the actor’s replication settings.

  1. Open your game mode class (Or create one if you haven’t got one, note you will need to apply that game mode in your “Maps And Modes” settings in project settings).
  2. Create a server event called Spawn Gun. Add the script to spawn the object and call the rotation component function we created in step 2.

  1. Finally, go back to your pawn and added the functionality to call the spawn

Hit play with 2 clients: Using my code, when you hit Space on one of the clients, it will spawn a gun at the client’s location. the rotation component is added automatically and replicates to all clients.

I strongly recommend you check out: http://cedric-neukirchen.net/Downloads/Compendium/UE4_Network_Compendium_by_Cedric_eXi_Neukirchen.pdf

This is a clear guide on how classes should be configured and used when considering networked games.

Hope this clears things up. Good luck!

I read that cedric network compendium a couple times, but i get confused on what to do in which class. I have my switch weapon logic to destroy weapon_BP(which contains Spawn Actor) on my back socket and spawn a new weapon_BP in my hand socket. All that Logic is in my character_BP. Is that a bad way of doing things? I hope i didn’t make this too confusing.

Handling world objects which you interactive with inside your Character BP is fine and would work. Remember that pawns are designed to be destroyed for when players die. Player controllers can possess multiple pawns throughout a session. So it would be wise not to store persistent information inside actors which will be destroyed/reused.

I will give an example of where you should store particular pickups.

Storing Pickups

Character/Pawn blueprints

  • If you want the player to lose their pickups when they die

Player Controller

  • If you want the player to keep their pickups when they die.

Player state

  • If you want the player to keep their pickups when they die and other players can see what they have in their inventory.

Handling spawning of the pick ups.

Character/Pawn blueprint

  • If the object is only for this player and cannot be collected or interacted with by any other player

PlayerController

  • Only if the object is relevant to the current player. This can be things like menus, helper markers etc.

GameMode / GameState / Level BP

  • If the objects can be interacted with by multiple players or if they can be affected by other objects in the world.

The aim is the keep amount of references to objects to a minimal. Classes should be accessing objects through the correct classes. The pickup doesn’t need to know anything about the player, the player should communicate with the object via the server.
Pickups which can be collected by multiple players are owned by the world and not a player specifically, so it makes sense that a world object handles spawning them.

Hope this helps :slight_smile:

Thanks! Well said and very informative. I’m Developing a TPS like Gears of War. When the a player dies he drops his weapons and other players can pick them up. i have it set up in the character_BP. I’m thinking this is wrong since other players are going to be interacting with it, it should be spawned by the Game mode class. right? I really appreciate you helping me. Wish i knew someone that could go over my work and explain to me what is wrong and why.

If the person drops items on death which other players can then collect. I would suggest the GameState class. The GameState can naturally replicate information which means fewer calculations are required on the server.

The GameState is more a data blueprint, but I think in this case it would be appropriate.

If the items the player drops are owned by your character_BP, once the character_BP is destroyed, so are the references to the objects. Overlap/hit events would still work fine, but if you wanted to call extra functionality, such destroy the objects if they aren’t collected within a certain amount of time, then you would have to search for them using “GetAllActorsOfClass” which is a node I try and avoid as much as possible.

The thing with Unreal (and most game engines), there isn’t one way of doing things. You could, in theory, amend your original code to handle all this. However, in my experience, ownership handling (which is a different kettle of fish) can get very complicated.

If you’re still stuck, I can hand you my Discord name and we could discuss further.

When the character drops the weapons on death, i use a interface call message in the character_BP which calls the interface event in the weapon_BP with a delay and destroy actor. Is that Logical? PM me your discord name. Thanks again

And yah, ownership is what confused me the most.

If the logic is predetermined then sure, that would do what you want it to do. It may become problematic if you need to call any functionality on the object after the pawn has been killed and before another pawn collects it. But it is all down to what your game requires.

As I mentioned, there are different ways to approach the same problem. The methods I have suggested is purely about the organisation. It is simpler to debug if it is clear in your code, what state the object is, who / what owns the object and their status on the server /clients.

If an object is spawned by a pawn, you would assume that object belongs or has a permanent relationship with that pawn. If an object is designed to be interacted with by multiple entities in the world, then a world class would be more sensible.

My discord is Alekann#7543