How to serialize input event when doing control binding in C#?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By jarlowrey

I am using Godot+Mono and when I try to serialize an InputEvent using C# it crashes upon load or save

Save:

E 0:00:13.720   System.Reflection.MemberInfo[] System.Runtime.Serialization.FormatterServices.InternalGetSerializableMembers(System.RuntimeType ): System.Runtime.Serialization.SerializationException: Type 'Godot.InputEventKey' in Assembly 'GodotSharp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' is not marked as serializable.

Load (error due to corrupted save file when it crashed during saving):

E 0:00:01.157   void System.Runtime.Serialization.Formatters.Binary.__BinaryParser.Run(): System.Runtime.Serialization.SerializationException: End of Stream encountered before parsing was completed.

I got around this temporarily by creating my own SerializableInputEvent class that saves the keyboard scancode or the mouseindex and uses them to re-create the input event for input binding. Obviously this is a limited approach that fails on all other input types, defeating the purpose of input binding.

Ideally I’d like to remove this intermediary class, or at least make it work with all possible input events. Would it be possible to somehow convert the Godot.InputEvent to a blob and save that blob directly? I’d imagine this would be required functionality when sending IE’s remotely for multi-player games, but I am having trouble figuring out how to do this. Any help is appreciated.

:bust_in_silhouette: Reply From: jarlowrey

I used GD.Var2Bytes(inst,true) and (InputEvent)GD.Bytes2Var(b,true)