Godot Version
Godot Engine 4.3
Question
Hello! I am making an multiplayer project and have stumbled upon serious issues regarding rpc functions. My game works like that client → server. Instead of creating two different projects - one for client and another one for the server - I’ve made one project. When I need to do only server specific things in the main_game node I do check like this if multiplayer.is_server(). Seems simple, but not when it comes to the rpc functions.
Let’s say I want to enable specific ui for ALL players connected to the server. Request comes from the server to all players.
Each player have it’s own Control node for a GUI with a script attached to it. The script have @rpc function declarations such as @rpc(“reliable”) func enable_screen_ui():.
Previously, I thought that I would just call from the server the rpc() method like so rpc(“enable_screen_ui”) but it doesn’t work because this would only work if @rpc function declaration would be in that same script.
So then I’ve decided to just go through all players → get their Control node → get their callable method and use rpc() on it. Like so: player.player_hud.enable_screen_ui.rpc().
Now I have multiple questions.
-
This does not work for me at all… why? Player is authority of
player_hudand calling from server script (the one in themain_gamescene that has authority of 1) the player’s rpc function just does not work. -
if I go through all players what would be difference between
player.player_hud.enable_screen_ui.rpc()andplayer.player_hud.enable_screen_ui.rpc_id(network_id)? Both would tecnically call that rpc function on that player because we’re using.rpconly on the player’s existing function. -
What is the purpose of having
@rpcdeclared function on the same script and then callingrpc()? At first I thought that main purpose ofrpcs is to communicate between server and client but I don’t see any sense in this.
I assume it wa made for the case when you have two projects for one game. Server project have that script with@rpcdeclared function and callsrpc(). And client project have script with@rpcdeclared but with client specific implementation and it would be called. I am not sure about this, but atleast it makes sense to me… but unfortunately this is not what I really need. -
When to use
"any_peer”on@rpc? I mean as far as I understand when instead of"any_peer”it’s"authority”then only that peer that have this@rpcdeclaration can call it. And when"any_peer”any peer can call it.
At first I thought that would be useful for client → client communication. Player can call peer of another player usingrpc_id. But that thought was back then when I thought I could callrpc_idin the script that had no@rpcdeclaration in it.
So, technically if I wanted to either client → client or client → server then in any way I would need to set"any_peer”to all@rpcdeclarations. I am probably horribly wrong on this. Please help… -
When to use
"call_local”on@rpc? Supposedly, I doplayer.cool_func.rpc_id(network_id); #network id is player’swhat would happen with"call_local”? I mean probably nothing but uh still is there any difference?
I mean it’s probably needed again when you have two different projects for client and server and you could from server project dorpc_id(network_id, “cool_func”)and if I’ve had that@rpcdeclaration on server it would be called on the server and on the client because I have"call_local”on it. Makes sense to me. But what about when I have one single project for both server and client?
To be honest, I really don’t want to create two different projects for client and server. I want to have one project that have two export settings as both for client and server because this is more convenient and I can include specific resources that each build should have (e.g. server build should NOT have textures).
And the worst part this thing just does not work. I’ve did everything and my attempts to do player.coolr_func.rpc_id(network_id) did not help me.
The only thing I’ve understood from the documentation that rpc calls should be on same NodePath is that it’s meant when you have two projects for client and server.
Because, technically on single project if player is it’s own scene that have Control node with a script that have @rpc declaration then server should also somehow have that same declaration on the LITERAL player’s OWN SCENE. SO it would be like 1 @rpc declaration just in one script just for both server and client.
I literally don’t understand and I probably miss so many things. Plaese help me. I am literally unable to go further. The documentation is literally no help. It has good definitions that makes sense but at the same time it does not answer all the occuring questions.