I'm working on multiplayer game. I want to pass object by rpc to another player, but I can't due security risk and in other computer the only I get is EncodedObjectAsId that I guess is useless for me. In Github issue I found Faless's answer:

This must be a bug down the line. allow_object_decoding is false by default, so the label should not be decoded. I'm investigating. But where I can to set it as false? I didn't found any object in my multiplayer realization that have to change this variable.

extends Node

const SERVER_IP := "127.0.0.1"
const SERVER_PORT := 666
const SERVER_MAX_PLAYERS_COUNT := 2


func host_server():
	var peer = NetworkedMultiplayerENet.new()
	peer.create_server(SERVER_PORT, SERVER_MAX_PLAYERS_COUNT)
	get_tree().network_peer = peer

func join_server():
	var peer = NetworkedMultiplayerENet.new()
	peer.create_client(SERVER_IP, SERVER_PORT)
	get_tree().network_peer = peer

So, okay, if make all by default security rules... Actually I don't know how properly transfer much of info by just string or int, I need my custom object. I have only idea to convert it to dictionary, but complete solutions I didn't found, so, I'm sure there's exists a better way... P.S. By object I mean object exactly, I need to send just the object, not a node. Maybe it could simplify the task.

I don't know how to successfully send an object. I get no errors, it just can't send it over. Instead one solution is to turn the object into a string format with all the objects properties. Then send the string and reconstruct the object on the client side.

So... actually I don't found any solution for successfully send object, but definitely we shouldn't use string as ondesic offered(if only we pass the json string, I'd looked at this variant, but don't get proper result), it's better to use dictionary cuz it has simular key(var name) - value structure. So, okay, let's try dictionaries I've found very interesting Serialization/Deserialization functions: inst2dict and dict2inst, so, let's try 'em. Problems?: 1. If you have _init() in the instance or in instances in its properties, then it must don't have any required arguments in the _init() - Github issue about 2. Also there's problem in deep converting, cuz only main inst/dict will be converted(GitHub issue about), their properties will not. So, ya need to make it by yourself... I have a code that doing that:

func inst2dict_properties_of_inst(inst):
    for property_name in get_property_names(inst):
    		var property = inst.get(property_name)
			if typeof(property) == TYPE_OBJECT:
				inst.set(property_name, inst2dict(property)) # IT'S CHANGING ORIGINAL INST TOO!

func get_property_names(obj : Object) -> Array:
	var property_names = []
	var property_list := obj.get_property_list()
	var i := 1
	while true:
		var property_name = property_list[-i]["name"]
		if property_name == "Script Variables":
			break
		else:
			property_names.append(property_name)
		i+=1
	return property_names

Also you need to deserialize it:

for key in dict:
		if typeof(dict[key]) == TYPE_DICTIONARY:
			dict[key] = dict2inst(dict[key])
  1. If your instance have some built-in instance in its properties like a Line2D you gonna have problems, cuz built-in objects doesn't have any script and then can't be converted... - GitHub issue about - So, I didn't found some good solution for it and don't know how to make it by myself. There's only one built-in object in my properties, it's named "line" and it's Line2D so I just added this condition into deserialization func:

if typeof(squad[key]) == TYPE_DICTIONARY and key != "line":

My final solution: Kinda messy... I've thought the same, so I managed to make simplify my instance, so I can have all necessary info without containing in properties any objects and just create with it the dict and pass it in rpc and that's works, on the other computer all I need to do it's create complete instance by received minimal info, so I satisfied with it.

Hope this long post is useful. Maybe I'll think of some more elegant solution, that will be without many troubles pass whole object, not just little info, but can't promise anything.

Updated: I've found how to allow_object_decoding, but I will not assign it as accepted cuz I didn't tried it, but it might works, so look below.

Oh, I guess I found how to allow_object_decoding

get_tree().multiplayer.allow_object_decoding = true

Although I've didn't tried it, but it might works.

18 days later
6 months later