Godot Version
4.4-stable-mono
Question
I’m trying to convert between a C# List of type QuestionCategory (a class I created to serialise/deserialise JSON) and a Godot Array.
This doesn’t work because the type I have created isn’t a variant type, however I’m using Godot’s RPC for multiplayer features, which require variant types for serialisation across the wire, so I’d like to know what my options are for serialising this C# List of a custom class for use with RPC.
An option I’m aware of would be to send the raw JSON string to the client and serialise it there, but it feels like more work that the client could do without…
QuestionCategory class:
public class QuestionCategory
{
public string Category { get; set; }
public Godot.Collections.Dictionary<int, string> Questions { get; set; }
}
Serverside assignment of questions variable:
private void DeserialiseQuestions()
{
if (!LifecycleState.IsServer) return;
LifecycleState.MightFailFatally(() =>
{
string questionsFile = File.ReadAllText("questions.json");
_questions = JsonSerializer.Deserialize<List<QuestionCategory>>(questionsFile);
});
}
Clientside assignment of questions variable passed from the server:
[Rpc(MultiplayerApi.RpcMode.Authority, CallLocal = false, TransferMode = MultiplayerPeer.TransferModeEnum.Reliable)]
private void TransferQuestions(List<QuestionCategory> questions)
{
_questions = questions;
}
This RPC call doesn’t work since the C# List isn’t a variant:
RpcId(Multiplayer.GetRemoteSenderId(), MethodName.TransferQuestions, _questions);
Something like this also unfortunately won’t work:
Godot.Collections.Array _marshalledQuestions = new Array(_questions.AsEnumerable());
Any alternative solutions would be much appreciated!