I am trying to change the texture on an object from a distance so both the server and other clients will be able to see the change when anyone makes the change. It currently works fine when the server player changes the texture on the box but if any client changes it no one else sees the change.
My event graph for changing textures:
The custom event is set to multicast and reliable
How i am calling the function in the standard first person blueprint:
Create a new Custom Event inside of your CharacterBP. Set it to “Run on Server”. Pack a “Switch has Authority” node behind the Branch that is in your CharacterBP.
If it is Authority, simply do the Cast and the ChangeTexture Methode. If it is Remote, call the custom event.
And what should the new custom event do? Easy enough, it should do this same.
The difference is, the Server is allowed to call the Multicast, but if a client does it, he will only see it on his screen. With the new Custom Event,
you are telling the Server, that you want to change the texture. He will then call the Multicast. So the new Custom Event is a bridge from client to server.
Make sure that you create this in your Character or Controller. Otherwise this call could be dropped, because of netownership problems.
Hi eXi… I’ve been doing a lot of stuff with multiplayer, and I haven’t once used “Switch has Authority” node. I can’t seem to find an actual use for it. And in the example you gave, I don’t see the point if it either. Is that node absolutely necessary in your example? The only way I could see it useful is if you are playing as the server…
The point of it is making sure only the server executes the code. The server should be pretty much handling everything in a multiplayer game to prevent client side hacks. In this particular example eXi uses it to make sure only the server calls the multicast event so that all clients also execute the event. I would say you maybe want to check your code out again that everything is properly replicating to all clients and think about potential injection points where a client can modify a property and it affects the game instead of the change being overwritten because client is not the authority.
I JUST posting a thread showing my WIP, maybe you could take a look? Everything is replicated fine to all clients.
Everything important is handled by the server. And by that I mean, I create a custom event set to Run on Server, Multicast, or Run on Owning Client. Is this not enough? Do I have to have an authority switch behind it? I did a lot of “Cheat” testing, for example… I’d have a function that would attempt to change some item values… Which works for the client (widgets are handled by the client of course), but as soon as he drops the item it reverts back to what the server has it set as. So I think I’m safe…
This node is important if you have code that could be called on both, client and server.
In C++, you nearly always check if the person who calls the method, is actually the Server or not.
Think about the “BeginPlay” method inside a PlayerController. This is called on the Server Version and on the
Client Version. If you now want to spawn a Pawn and possess it on begin play, you want to make sure, that this only runs on the server.
You could also use this node to make sure that a client calls an RPC while the server can run the function without.
It could be that you don’t need the node in your setup.
In the OPs example, the F Key press can happen on a Client and on the Server. (Let’s not talk about a dedicated server here). You need to
call 2 different things for either Server or Client. If you don’t do this, you end up in the “bug” the OP had.
So if a client presses the button, you want to call an RPCs from Client to Server. If the Server presses the button, you can simply do what the
server should do. In this case, a multicast. If you would just call the multicast, without a Client->Server RPC for the Client, you would see no
change if the client presses the key, because he can’t call the Multicast. On the other hand, the Server is allowed to do it.
And exactly for these kind of things, you will want to use a Switch Has Authority node.