Run On Server vs. Has Authority? (and more)

I’m creating a pretty simple pickup system and have a few questions about it. Shall we?

  1. I’ve heard that in multiplayer games we have to spawn actors through the server, but I honestly am not sure what it does mean. In my editor, for instance, I’m placing actors through the level screen and not programmatically. Is this acceptable? If not, do I really need to get the locations I want to place these items and manually spawn these Pickup Actors using a Blueprint (If so, which one?)?
  2. When a character picks an item up, I have to say to the server “hey, dude, player X picked item Y, do something.”. But the thing is: do I need Switch Has Authority here? A simple Event Call with Run On Server wouldn’t be enough, passing the player and the item that made the interaction?
  3. How Run On Server and Switch Has Authority are different? If I want to run something on the server, wouldn’t be simply calling an Event replicating with Run On Server when I want the server to participate in the interaction?

Beforehand I must say I REALLY watched these videos and even though they’re good, these certain things weren’t clarified enough to me. In practice, these concepts just messed up in my mind.

Finally, pardon me if these questions are somehow duplicated.

to address your first question if you are adding things to your level by dragging them into the viewport in the editor that is a fine thing to do. the items you add this way will be in the game prior to runtime and thus will exist to every player. you only need to worry about using the server when your spawning things into the game at runtime. this is because if you let the individual clients spawn things into the game they could be cheating and in this instance the server and other clients wouldnt know it happened so replication would be a mess. for this reason and many others in multiplayer games its best to have a actions taken by the server.

1 Like

when dragging it into the viewport it is part of the base level so it isnt spawned at all in a sense, its built with the level so it exists to all players. replication only affects things that happen after the start of the game so if somethings in the level before the game starts everyone can see it, but if its spawned into the world after the beginning of the game only the one who spawned it can see it, until that person tells everyone else about it that is. thats where the server has an advantage as it can communicate with all clients, whereas the clients can only communicate with the server and not each other.

Thanks for the clarification, Thompson. The thing is: when dragging an actor into the level viewport, is it spawned by the server by default?

  1. As said, UE4 does not consider actors placed in the map (from the editor) to be “spawned”. Spawning happens at runtime after the level is loaded. Actors placed in the map before runtime will work as if they were spawned on the server – assuming replication was set up properly
  2. The RunOnServer functions will never run on clients so no, you do not need to check if it has authority
  3. RunOnServer and SwitchHasAuthority are different because RunOnServer is a flag you set on a function before runtime, while SwitchHasAuthority is a blueprint node that gets evaluated at runtime

There will be many times you will need SwitchHasAuthority, though. Most built-in functions don’t assume they run on the server (i.e. BeginPlay, Tick, overlap events). Like on BeginPlay you may need to initialize a variable so you’d do a switch and the server would set and use the variable while the client would get and use the variable.

Hope that helps.

It really helps, @anonymous_user_64f7311b. Thank you very much for your response. It’s clearer now. Just wanted these tutorials to dive into these details just like you did.

Again, thank you. :slight_smile:

@anonymous_user_64f7311b, great answer, I hope u or someone else can clarify one last thing: You said that “you’d do a switch and the server would set and use the variable while the client would get and use the variable.” So I set up a quick test level with blueprint and created a replicated variable myVar. I then do a switch, set myVar on Authority and try to print it on Remote. However it is not set on the client. Of course if I put a delay and give the server time to set and replicate the variable it can be read on the client, but I don’t like using delays for things like that. I could do a isValid loop, but is there a better way to “set on server and read on client” in a clean way? The way you said it makes it seem there is a good and obvious way to do something like this, and I’m hoping there is!

So that’s a level blueprint? If so, then that makes sense. The client loads the level before replication can occur, so it can’t get replicated values until after BeginPlay has been executed. What I said is for replicated actors which are explicitly spawned. For something like a level, you will need to use (reliable) RPC, and you want it to happen for each client when they connect. Its been a while, but I think the GameMode has an event - “new player” something or other, IIRC? Maybe the level has an event on the client side that says it’s connected and ready. If so, you could do your print string there. Sorry, I’m not using UE4 much these days so I’m a bit rusty.

If you want to call a function after a variable is replicated - check RepNotify.