0 votes

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])
Godot version 4.0 dev
in Engine by (26 points)

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.

1 Answer

0 votes

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

by (14 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.