How does one use RPCs with custom_multiplayer in Godot 4?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By EquineRecline

Are RPCs usable yet with custom_multiplayer or am I doing something wrong?

The code is mostly from an example project for Godot 3 that I’ve modified for 4. The original worked as expected.

The error:

_on_network_peer_connected: Unable to get the RPC configuration for the function "server_msg" at path: "/root/Server". This happens when the method is not marked for RPCs.

The code:

network.gd

extends Node

var _peer:ENetMultiplayerPeer

func _init():
	custom_multiplayer = MultiplayerAPI.new()
	custom_multiplayer.root_node = self
	_peer = ENetMultiplayerPeer.new()
	
func _process(_delta:float):
	if custom_multiplayer.has_multiplayer_peer():
		custom_multiplayer.poll()

client.gd

extends "network.gd"

func _ready():
	_peer.connection_succeeded.connect(_on_connected_to_server)
	var result = _peer.create_client("127.0.0.1",5005)
	if result == OK:
		custom_multiplayer.multiplayer_peer = _peer
	
func _on_connected_to_server():
	print_msg("Connected to server")
	print_msg("Connected peers: %s" % [custom_multiplayer.get_peers()])

@rpc
func server_msg():
	print_msg("Server says hi")
	custom_multiplayer.rpc_id(1,StringName("client_msg"))

func print_msg(msg:String):
	print("Client: %s" % [msg])

server.gd

extends "network.gd"

func _ready():
	_peer.peer_connected.connect(_on_network_peer_connected)
	var result = _peer.create_server(5005)
	if result == OK:
		custom_multiplayer.multiplayer_peer = _peer

func _on_network_peer_connected(id:int):
	await get_tree().process_frame
	print_msg("Client %s connected" % [id])
	print_msg("Connected peers: %s" % [custom_multiplayer.get_peers()])
	rpc_id(id,StringName("server_msg"))

@rpc(any_peer)
func client_msg():
	print_msg("Client says hi")

func print_msg(msg:String):
	print("Server: %s" % [msg])

IIRC, the net code for 4.0 is still changing, i.e. it’s a moving target. Therefore, it may be a good idea to wait on the network code until it’s finalized.

Ertain | 2021-11-18 01:33

:bust_in_silhouette: Reply From: luxroy

I have had success by putting @rpc before the functions where I use rpc_id(). In your example, I would try putting it before _on_network_peer_connected(), or moving rpc_id() to another function and putting the @rpc tag before that.