[Advice] Note for rpc users - don't send Dictionaries :)

Godot Version

4.2.1

Question

I’m posting this more for posterity, but if have a Dictionary and you’re thinking that you would want to send the dictionary as the argument to an RPC function - don’t. In 4.x, this will actually result in the server sending the dictionary crashing to the console/dump core.

    var result:Dictionary = {}
    result["is_enabled"] = true
    result["tag"] = 'Some string data'
    rpc_id(client_id, "my_method", client_id, result)

If you want to send a dictionary, you must send it over the wire as a JSON payload:

rpc_id(client_id, "my_method", client_id, JSON.stringify(result))

There probably should be something that can catch this as an error in some way, but until then - here’s your sign.

I don’t have problems with dictionaries, although they are not very bandwidth friendly.

1 Like

Definitely crashing Linux and MacOS servers on RPC in 4.2.1. Been able to put that into a little self-contained for the bug report.

I just tried it on Ubuntu 22.04 works fine

minimal code:

extends Node
var enet = ENetMultiplayerPeer.new()
var port = 5555
var ip = "localhost"

func _on_server_pressed():
	enet.create_server(port,4)
	multiplayer.connect("peer_connected",_on_peer_connected)
	multiplayer.multiplayer_peer = enet

func _on_peer_connected(id):
	var result:Dictionary = {}
	result["is_enabled"] = true
	result["tag"] = 'Some connection string data'
	rpc_id(id,"rpc_diction",result)

func _on_client_pressed():
	enet.create_client(ip, port)
	multiplayer.multiplayer_peer = enet
	multiplayer.connect("server_disconnected",get_tree().quit)
	multiplayer.connect("connected_to_server", func():print("client connected"))

@rpc("authority","reliable")
func rpc_diction(dic:Dictionary):
	print(multiplayer.get_unique_id()," ",dic)

var time:float = 0.0
func _process(delta):
	if multiplayer.get_peers().size() > 0 and is_multiplayer_authority():
		time+=delta
		if time >=5.0:
			time = 0.0
			var result:Dictionary = {}
			result["is_enabled"] = true
			result["tag"] = 'Some string data'
			rpc("rpc_diction",result)

We found the issue. Your example assumed that there are no nulls/empties in the dictionary. Those are causing crashes as there is/was no check for them. It was actually blowing up in the engine C code.

2 Likes

Could you please update the title too? I’m using dictionaries in netfox for now, and it gave me a bit of an adrenaline rush :sweat_smile:

2 Likes

I tried to, but I don’t have permission to change or delete the post.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.