[Serializable] namespace not found


#1

Hello, i wanted to have a class that would contain all the game important variables in order to save and load serialized save files by using the [Serializable] attribute on the class, but i get a namespace not found error, xenko doesn’t support the Serializable attribute ?
Would i have to make myself the serialization script ?
it wouldn’t be a big deal but it would be easier to simply have directly a value that could be serialized automatically at runtime


#2

What are you trying to achieve? If you want to serialize properties in a script, you need the [DataContract] attribute on the class (and eventually the [DataMember] attribute on properties.

If you want to use a custom serialization you indeed could use the [Serializable] attribute. The issue is that this attribute is not available in .net standard 1.4. It was only introduced in .net standard 2.0. You can also look at XML serialization that doesn’t need that attribute.

But the best would be to reuse Xenko functionalities with [DataContract] on class. Note that all scripts are by default serializable.


#3

I’m trying to make a save/load system, i have a script that contains dictionnaries containing the various global variables required by the game to remember the various informations (since my game is an RPG)
i’ll try to look at DataContract


#4

Looking in the SiliconStudio.Core.Serialization nothing really stands out to me about how you would use the built in serialization library to save and load game state. You have any hints for this @Kryptos? i.e. unit tests or something I can look at?

Like @Kryptos says you could also use another serialization method. XML, JSON etc. Though you may need to add other NuGet packages etc.


#5

I have made some tests and there is something weird, i can perfectly serialize as binary a string,string dictionnary,a int,string dictionnary but a int,int dictionnary will cause a crash
'Could not find serializer for type System.Collections.Generic.Dictionary2[System.Int32,System.Int32].’`


#6

Can you show us a bit more code? I might be able to help better with more info.

FYI If you want to chat you can quite often find me (and others) over at Xenko gitter chat room or over at the GDN Xenko chat room on discord. Though I am about to go out for lunch…


#7
 public class GlobalVariables : StartupScript
    {
        Dictionary<int, int> GameDataString;
        //   CultureInfo ci = CultureInfo.CurrentUICulture;
        public GlobalVariables()
        {
            CODENAME = "test";
            GAME_VERSION = "ALPHA";
            GameDataString = new Dictionary<int, int>
            {
                { 0, 1},{1,2}
            };

        }
        // Declared public member fields and properties will show in the game studio
        public override void Start() { 
            base.Start();
             using (Stream saves = File.Create(AppContext.BaseDirectory + "SAVES.bin"))
                BinarySerialization.Write(saves, GameDataString);
            // Initialization of the script.
        }

this is the part that serializes the dictionary, with a int,string and a string,string it works but a int,int causes an error
even with the [datacontract] on the class it doesn’t change anything,same with the [datamember]


#8

Hi,

I’m back with an answer.

So I got the built in serialization to work using BinarySerialization and a custom data class to save/load game data as trying to save script class itself threw exceptions.

My test class:

[DataContract]
public class SaveGame
{
    [DataMember]
    public Dictionary<int, int> Data { get; set; }

    [DataMember]
    public string Name { get; set; }
}

I decided to use the VirtualFileSystem to access the file system. It is supposed to be platform agnostic. See http://doc.xenko.com/latest/en/manual/engine/file-system.html.

Saving:

using (var saveGameFile = VirtualFileSystem.ApplicationRoaming.OpenStream(saveGameFileUrl, VirtualFileMode.Create, VirtualFileAccess.Write))
{
    var saveGame = new SaveGame
    {
        Name = "Meow",
        Data = new Dictionary<int, int>
        {
            {0,1},
            {5,6}
        },
    };

    BinarySerialization.Write(saveGameFile, saveGame);
}

Loading:

if (VirtualFileSystem.ApplicationRoaming.FileExists(saveGameFileUrl))
{
    using (var saveGameFile = VirtualFileSystem.ApplicationRoaming.OpenStream(saveGameFileUrl, VirtualFileMode.Open, VirtualFileAccess.Read))
    {
        var saveGame = BinarySerialization.Read<SaveGame>(saveGameFile);

    }
}

Personally for save game data I would use something else though. Doing save games this way is probably not version-able. But that is probably a too big of a topic to cover here.

Also you should probably ask questions like this over on the Answers site. That way I can earn more rep points. :wink:

Anyways, hope this helps.

Cheers,
dfkeenan


#9

The last time i asked something on the answer site i got no responses so i stopped using it, but i’ll maybe use it a next time

thanks for the answer though, i’ve worked on a serialization system using sharpserializer but i’ll try your suggestion