Download

RPC function vs RoleAuthority check?

Hello.

I’ve found this piece of code in the ShooterGame example:

1.jpg

All the function does is verifying whether I’m the server or the client.

If I am the client, return.
If I am the server, "remove all weapons from inventory…"

Why didn’t they use a RPC call that executes on the server?



UFUNCTION(Server)
void ServerRPCFunction();


In that way they’d have had a function that simply executed on the server.

Is my logic uncorrect? If yes, why?
Thanks!

They just make sure, that no Client can call that Function.
You could, in fact, structure your own code a way, that you wouldn’t need authority checks.

But you will find yourself often at the position where you want to make sure that really only the Server can call that function.

A simple example is:

Something needs to be done on the Server and you have a ListenHost and Clients using one and the same Function.
The Server/ListenHost can directly do the stuff you need, but the Client needs to call the RPC.
That’s when you might want to check with Authority who exactly called the function.

But yes, at this point, the Authority check is made to make sure that no Client can call this.
The function is probably not even called on a client. It’s just for anti cheat safety stuff.

If I want something to be executed on the server, in both cases I achieve my desire!
As the documentation states:

So at the end of the day, the function will always be executed exclusively on the server… so what’s the problem?
When to use one or another?

Well, you can have execution chains that never touch the Client. That happen on Server only, but you still want to
limit the Call to the Server, since a Client could call that function when cheating.

A Server RPC is just to get the Call from Client over to the Server. You don’t want to use a Server RPC to ensure that
it runs on the Server. You only use ServerRPCs if something happens on the Client and it must move over to the Server.
For example a KeyPress or a Button Press in UMG.

Let me check If I got the point

**ServerRPC: **
When things that happen on the client should be communicated to the server to make it execute something.
Example: the player kills someone, and the kill counter in the leaderboard should be updated
(uhm, not kinda sure… the client may still call the function by cheating and increasing his own kill-counter, right?) :confused:

AuthorityCheck:
When something MUST be called and executed on the server.
Like making the server add a weapon to a player’s inventory.

One is an RPC call that moves the call to the Server, Client or Multicast (3 different types of RPC).

The other one is to check if the instance, which is calling the function, is running on the Server or the Client.
There are even more checks, for example if it’s a dedicated server or not.

For example: Dedicated Server don’t need a UI, so you can make sure the UI spawning logic is only called on ListenServer
and Clients.

Authority Checks are not about calling something. They are more about making sure that only a specific kind of authority can
use a specific function.

A Server RPC would mostly not be used to update the Leaderboard, since the whole killing should only happen on the Server.
The Client only gets the visual effects.

But pressing “Shoot” (left mouse button) is a client action and needs a Server RPC to actually handle the shooting and replicate it.

But then when I start the game and in the BeginPlay I print a DebugMessage, why does the DebugMessage gets printed as many times as the number of players?

To execute that function exclusively on the client I’m controlling, I should use a RPC-Client call then?

@eXi Also, it looks like for each player there’s a server… look at this:

1.jpg

When I start the game with 2 players (1 server, 1 client), I get

Server: YOU ARE THE SERVER
Server: YOU ARE THE SERVER

both in the Client and Server window.

#Headache

BeginPlay is called on every instance of that Actor. I don’t know what Actor you used there, but as an example:

The PlayerCharacter in a Multiplayer Game with 2 Players (ListenHost and Client).

The Server has a PlayerCharacter. This one exists on the Server and replicated on the Client.

The Client has a PlayerCharacter. This one also exists on the Server and replicated to the Client who owns it.

So in total, there are 2 copies of the Server PlayerCharacter and 2 copies of the Client PlayerCharacter.

BeginPlay is called 4 times at this point. You limit it to the Authority (Server). That means you get 2 calls, one
for the Server Version of the Servers PlayerCharacter and one for the Server Version of the Clients PlayerCharacter.

Oh so I was right when I used to think that for each player both a client part and a server part are instantiated.

But how can that be even logically possible? It’s weird.

Shouldn’t have the server be ONE in a Multiplayer game?

If the Server only had one Character instead of one for each Client + himself, how should the rest of the network game
see his Character? :smiley: Every player has an instance of the Character of the other players. Otherwise they wouldn’t be able to see each other.

Mhm… so hoping back to the original issue.

I print a String on the server, but since I have X players in my game, that string gets printed X times?

So this makes me think that If I want the server to do something, that “something” will be done for every player in the game!!

It’s not an RPC because if the client called that function, it would tell the Server to destroy the inventory (and it would work, because ROLE on the server would be authority).

That completely depends on where you call “something”. If you do that in the BeginPlay, then yes, it might get called on every Server Instance of that Actor.
But if you have a single ServerRPC, it will only get called on the Server Version of that one Player who called the ServerRPC.

I can only recommend you to watch some more tutorial videos and maybe read the docs a bit more and, in the end, just play around with what you learned here.

The point is there’s no enough documentation about Networking.

Speaking about videos, on YouTube there is ONLY one playlist about Networking.
Speaking about Unreal Documentation, yes they speak about the basic things of multiplayer… but doubts constantly come to my head.

Other than the ShooterGame, there’s no more precious source for learning UE4 + Networking in C++.

Anyway,

Look at this:

08f1b650ebf4dbc0fdd6e9668a6f39e090940413.jpeg

When I jump, both the DebugMessage gets printed on both the client and the server windows.

Even if I change the RPC call to be a ServerRPC instead of a ClientRPC…** same things happen.
**
Is that a problem related to the AddOnScreenDebugMessage()???

Yo, that could be due to PIE sessions running in one single process. Messages might appear on both screens, even if they are only called on
one of the Players. If you really want to know if it’s Server or Client, use the



if(Role < ROLE_Authority)


check and make 2 different DebugMessages.

ALSO: You have Tom Loomans Survival Game, which should also be networked: http://www.tomlooman.com/survival-sample-game-for-ue4/

And you don’t need C++ to learn networking. Blueprints and C++ work the same. You know how to code, so that’s not the problem.
Download the Multiplayer Shootout Game or my “BallBump” project (can be found through my Signature in the Free Projects thread) and
check out how the networking works in these. It’s not different to C++, despite the way to actually “write” a function instead of a BP node.
But the logic stays the same.

I know the feeling. It is sort of feast or famine for the C++ networking world and ue4. Shooter game and Tom’s stuff are awesome! But there really isnt much out there in c++ where you start from scratch and build up to something like Tom’s or the shooter game.
There is a C++ Mp shooter on the marketplace, i haven’t bought it yet, but it might help?

Man, thank you for the advices. But did you see the price?

I will take a look at that.

Right, but C++ gives you more freedom and I really don’t feel comfortable with Blueprints

What? I’ve been searching for that project for a while, but I don’t find it!
Is it still available? Where?

In the “LEARN” tab of the Launcher, under “Gameplay Concept Examples”.
It looks a bit orange and is a western like shootout game.

@eXi

Look at this:

90ff64e61202a29760cf6956c798d713940d7238.jpeg

When the player takes the Pickup, the OnPickup() function is called.

I want the SERVER to manage the Pickup and edit the bIsActive variable (which determines if the pickup is visible or not)
So for that purpose I do the RoleAuthority check

AFTER I’m sure that it’s no longer the client, but it’s the server that keeps going through my function, and so calls the NetMulticasted function Tell_PickupPicked() to tell ALL the clients the pickup has been taken.

All the Tell_PickupPicked() function does is what I want it to happen ON ALL THE CLIENTS, and so: print a debug message, hide the mesh, start a rspawn timer and set it’s variable bIsActive to false.
But none of these happens.

I know my logic is extremely wrong, but help me find out why!

I feel so stupid about Networking, but not Networking in general… but with UE4 in particular.

Well, you should also tell me what happens when you call that stuff. Like, what actually happens, so i know what might went wrong.

Is “OnPickup” actually called on the Server?