godotengine / godot

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

strange error of muitplayer API #70631

Closed L2750558108 closed 1 year ago

L2750558108 commented 1 year ago

Godot version

3.5.1mono

System information

windows10

Issue description

I get the error of muitplayer API,but I just serialize a object to json image

Steps to reproduce

Run my reproduction project

Minimal reproduction project

tests_2.zip

raulsntos commented 1 year ago

I'm getting this exception when running your MRP:

Newtonsoft.Json.JsonSerializationException: Self referencing loop detected for property 'Multiplayer' with type 'Godot.MultiplayerAPI'. Path 'timeObjectNode.Multiplayer.RootNode'.

This means that when trying to serialize some C# object, the JSON serializer found a cycle[^1] because you are serializing Godot objects which contain self-referencing members.

Usually you'd want to manually specify which members to serialize for your objects, by default Newtonsoft's Json.NET serializes every public field and property. You can configure the serialization with a JsonSerializerSettings object or, likely easier, you can create a separate POCO definition to use for serialization instead of the real object.

Alternatevely, keep in mind that Godot also provides a JSON class that can be used for serialization which doesn't support custom C# types but will work by default with any Godot type (including your custom types as long as they inherit from Godot.Object). This serializer only serializes the members exported with the [Export] attribute.

[^1]: A cycle is when class A has some field or property that references class B and B has some field or property that references A creating an endless loop that can't be serialized.

L2750558108 commented 1 year ago

2022-12-28 23-49-29.zip But I had 2 errors when I ran it.One of them is what you said but the other one is strange.Here is a example video

raulsntos commented 1 year ago

It's a side-effect of the serializer accessing every property to serialize it, one of the properties that it tries to serialize throws an error because it shouldn't be accessed without setting a network peer.


When the serializer tries to access the MultiplayerAPI.IsRefusingNewNetworkConnections property, it executes this code:

https://github.com/godotengine/godot/blob/0f10eafb38bd54e59fa7fd079ceafd2c07e8be2a/core/io/multiplayer_api.cpp#L808

Which checks if there is a valid network_peer set, since there isn't it prints an error to the console.