DoubleDeez / MDFramework

A multiplayer C# game framework for Godot 3.4 Mono.
https://discord.gg/UH49eHK
MIT License
77 stars 13 forks source link

Recursive MDReplicatedMembers #65

Closed Beider closed 3 years ago

Beider commented 4 years ago

The Problem

Issue #62 introduced data converters to the MDReplicatedMember, it also introduced a generic data converter that can convert any type of class.

However currently recursion is not supported, so it is impossible to have a class with another replicated class inside. It is also impossible to have a MDList with another MDList inside it. Nor can you have a MDList inside a replicated class.

The Solution

This will require some more refactoring and wider changes to the system. Currently most network methods use a params object[] Parameters to send data across the network. The MDList and the data converters send their data as index + value pairs.

[0] = <index>
[1] = <data>
[2] = <index>
etc...

The most logical way to fix this would be to modify the sending so the index part also contains length of the data. So something like,

[0] = <index>#<length>
[1] = <data>
[2] = Data
etc...

In addition we also need to make member registration recursive and more generic. MDList, class data converter should support further converters / MDList inside them. For this we may need to implement some kind of new interface or data structure. We should also take care so we only send the data that is changed.

Eg. If we got something like this MDList<MDList<CustomClass>>, when a value in a custom class in the inner list is changed only the change for that value should be sent. That means the message may look something like this

[0] <index for outer MDList>#3
[1] <index for inner MDList>#2 // This would be the data from previous line
[2] <index for CustomClass member that changed>#1
[3] <value for the CustomClass member>
Beider commented 3 years ago

In the scope of this bug please add a storage for converters supplied by MDReplicatedMemberSetting to MDStatics.

This should probably be something like, Dictionary<ConverterTypeIncludingGenericType, ConverterInstance>

Then make both MDReplicatedMember and MDList use this for lookup before instancing. Probably move the code to MDStatics.

Beider commented 3 years ago

So to outline this solution further, "all" that needs to be done here is to adjust the default IMDDataConverter for custom classes. It already does partial replication of only changed things and has a ShouldBeReplicated function