godotengine / godot

Godot Engine – Multi-platform 2D and 3D game engine
https://godotengine.org
MIT License
90.82k stars 21.14k forks source link

When trying to send an Array that is typed with a custom type a type error is emitted and no data is sent. #98783

Open TizWarp opened 3 days ago

TizWarp commented 3 days ago

Tested versions

Reproducible in 4.3.stable

System information

Godot v4.3.stable unknown - Arch Linux #1 SMP PREEMPT_DYNAMIC Thu, 10 Oct 2024 20:11:06 +0000 - Wayland - Vulkan (Forward+) - dedicated AMD Radeon RX 7700S (RADV NAVI33) - AMD Ryzen 7 7840HS w/ Radeon 780M Graphics (16 Threads)

Issue description

I have a multiplayer game it requires clients to send some extra info, like usernames etc. I have a custom class that holds this info. I have created a typed array that hold all connected players. This info needs to be sent to the client instances and so I have an rpc like so :

var players : Array[Player] = []

@rpc("authority")
func send_data(data : Array[Player]):
  players = data

I get the following error when executing this rpc

E 0:00:00:0359   _process_rpc: RPC - 'Node2D(game.gd)::data_rpc': Cannot convert argument 1 from Array to Array
  <C++ Source>   modules/multiplayer/scene_rpc_interface.cpp:290 @ _process_rpc()

when inspecting the node that hold the player array it is empty

Steps to reproduce

Create new project. Setup basic Multiplayer functionality Open a server and a client instance.

func createServer():
  var peer = EnetMultiplayerPeer.new()
  peer.create_server(6643)
  multiplayer.multiplayer_peer = peer

func createClient():
  var peer = EnetMultiplayerPeer.new()
  peer.create_client("127.0.0.1", 6643)
  multiplayer.multiplayer_peer = peer

Create Custom class and create a Typed array that is of that type

class Player:
  var id : int;
  var username : String;

var players : Array[Player] = []

Create rpc function that attempts to sync the array:

@rpc()
func data_rpc(data : Array[Player]):
  players = data

Minimal reproduction project (MRP)

mrp.zip

AThousandShips commented 2 days ago

The provided MRP is not complete, you need to make a complete project with the scene involved, please update your MRP to contain everything needed to run this code

However does it actually work when calling with just the type? Or with an untyped array? You don't seem to have enabled object encoding in your multiplayer, that means objects can't be sent regardless, but if you upload a proper MRP I can test this further