c# - SaveLoad Class into Json - Monogame - Stack Overflow

internal class Quest{public int[] StatsBonus;public int[] StatsMalus;public string Title = "";

    internal class Quest
    {
        public int[] StatsBonus;
        public int[] StatsMalus;
        public string Title = "";
        public string Description = "";
        public Color Color;
        public int Total = 0;
        public DateTime LastReset;
        public TimeSpan RemainingTime;

        public List<Rectangle> QuestRectangles;
        public List<Rectangle> DoneRectangles;

        public Quest()
        {
            StatsBonus = new int[4];
            StatsMalus = new int[4];
            Title = "";
            Description = "";
            Color = Color.Aquamarine;
            Total = 0;
            LastReset = DateTime.Now;
            RemainingTime = TimeSpan.FromSeconds(60);
        }

        public Quest(string title, int[] statsBonus, int[] statsMalus, string description)
        {
            Title = title;
            Description = description;
            StatsBonus = (int[])statsBonus.Clone();
            StatsMalus = (int[])statsMalus.Clone();

            Total = 0;
            Color = Color.Aquamarine;
            LastReset = DateTime.Now;
            RemainingTime = TimeSpan.FromSeconds(60);
        }
    }

a run-time i make istances of this class pressing a button, when it happens the class is added to a list

QuestList.Add(new Quest(Title, Bonus, Malus, Description));

then i save the list of quests and what i need in json, using a class GameData

        public static void SaveGame()
        {
            var saveData = new GameData
            {
                QuestList = new List<Quest>(QuestMaker.QuestList),
                Stats = (int[])QuestManager.Stats.Clone(),
                DoneRectanglesData = new List<int[]>(QuestManager.DoneRectanglesData),
                QuestRectanglesData = new List<int[]>(QuestManager.QuestRectanglesData)
            };

            File.WriteAllText(SaveFilePath, JsonSerializer.Serialize(saveData));
        }

        public void LoadGame()
        {
            if (ResetSaveData || !File.Exists(SaveFilePath))
            {
                ResetGame();
            }
            else
            {
                var saveData = JsonSerializer.Deserialize<GameData>(File.ReadAllText(SaveFilePath));

                if (saveData != null)
                {
                    QuestMaker.QuestList = saveData.QuestList ?? new List<Quest>();
                    QuestManager.Stats = saveData.Stats ?? new int[4];

                    
                    UpdateManager.DoneRectangles = saveData.DoneRectanglesData?.Select(arr => new Rectangle(arr[0], arr[1], arr[2], arr[3])).ToList() ?? new List<Rectangle>();
                    UpdateManager.QuestRectangles = saveData.QuestRectanglesData?.Select(arr => new Rectangle(arr[0], arr[1], arr[2], arr[3])).ToList() ?? new List<Rectangle>();
                }
            }
        }

the game start properly after the loading, but every value of Quest istances i made a run-time are reset to zero...

for exemple, if i increase a run-time QuestList[0].Total form 0 to 4, when i load the game QuestList[0] exist, but QuestList[0].Total = 0...

is becouse the constructor of Quest is composed to complex types like Color, Array, DateTime, TimeSpan?

I know nothing about Json and just a little about MonoGame engine and C#, so i can try nothing special.. i tried asking to GPT and reading documentation, but still don't see the solution.

I expect that when i load the game, the istances inside QuestList created a run-time, save and load the values correctly without reset to default.

before inizialize quest

after inizialize quest

before close and load, completed quest 4 times

after close and load, quest exist but reset values

    internal class Quest
    {
        public int[] StatsBonus;
        public int[] StatsMalus;
        public string Title = "";
        public string Description = "";
        public Color Color;
        public int Total = 0;
        public DateTime LastReset;
        public TimeSpan RemainingTime;

        public List<Rectangle> QuestRectangles;
        public List<Rectangle> DoneRectangles;

        public Quest()
        {
            StatsBonus = new int[4];
            StatsMalus = new int[4];
            Title = "";
            Description = "";
            Color = Color.Aquamarine;
            Total = 0;
            LastReset = DateTime.Now;
            RemainingTime = TimeSpan.FromSeconds(60);
        }

        public Quest(string title, int[] statsBonus, int[] statsMalus, string description)
        {
            Title = title;
            Description = description;
            StatsBonus = (int[])statsBonus.Clone();
            StatsMalus = (int[])statsMalus.Clone();

            Total = 0;
            Color = Color.Aquamarine;
            LastReset = DateTime.Now;
            RemainingTime = TimeSpan.FromSeconds(60);
        }
    }

a run-time i make istances of this class pressing a button, when it happens the class is added to a list

QuestList.Add(new Quest(Title, Bonus, Malus, Description));

then i save the list of quests and what i need in json, using a class GameData

        public static void SaveGame()
        {
            var saveData = new GameData
            {
                QuestList = new List<Quest>(QuestMaker.QuestList),
                Stats = (int[])QuestManager.Stats.Clone(),
                DoneRectanglesData = new List<int[]>(QuestManager.DoneRectanglesData),
                QuestRectanglesData = new List<int[]>(QuestManager.QuestRectanglesData)
            };

            File.WriteAllText(SaveFilePath, JsonSerializer.Serialize(saveData));
        }

        public void LoadGame()
        {
            if (ResetSaveData || !File.Exists(SaveFilePath))
            {
                ResetGame();
            }
            else
            {
                var saveData = JsonSerializer.Deserialize<GameData>(File.ReadAllText(SaveFilePath));

                if (saveData != null)
                {
                    QuestMaker.QuestList = saveData.QuestList ?? new List<Quest>();
                    QuestManager.Stats = saveData.Stats ?? new int[4];

                    
                    UpdateManager.DoneRectangles = saveData.DoneRectanglesData?.Select(arr => new Rectangle(arr[0], arr[1], arr[2], arr[3])).ToList() ?? new List<Rectangle>();
                    UpdateManager.QuestRectangles = saveData.QuestRectanglesData?.Select(arr => new Rectangle(arr[0], arr[1], arr[2], arr[3])).ToList() ?? new List<Rectangle>();
                }
            }
        }

the game start properly after the loading, but every value of Quest istances i made a run-time are reset to zero...

for exemple, if i increase a run-time QuestList[0].Total form 0 to 4, when i load the game QuestList[0] exist, but QuestList[0].Total = 0...

is becouse the constructor of Quest is composed to complex types like Color, Array, DateTime, TimeSpan?

I know nothing about Json and just a little about MonoGame engine and C#, so i can try nothing special.. i tried asking to GPT and reading documentation, but still don't see the solution.

I expect that when i load the game, the istances inside QuestList created a run-time, save and load the values correctly without reset to default.

before inizialize quest

after inizialize quest

before close and load, completed quest 4 times

after close and load, quest exist but reset values

Share Improve this question edited Mar 23 at 22:32 asdrubale cazzituoi asked Mar 23 at 21:57 asdrubale cazzituoiasdrubale cazzituoi 11 bronze badge 3
  • Set a breakpoint in LoadGame then step through to find out what the problem is. Is it loading the file? Does the file have the right Json in it? What happens when you try to load the Json? What does saveData look like? – stuartd Commented Mar 23 at 23:30
  • Welcome to Stack Overflow. Beside your question, I wanna suggest read FAQ and How to Ask couple of times.. – Soner Gönül Commented Mar 24 at 12:12
  • upper there is everything about class GamData and Load/Save method, but yes.. i added some logs for the output panel, the game is loaded correctly, if a run-time i add 2 quests, QuestList contain correctly 2 quests, but these quests are resetted to zero.. – asdrubale cazzituoi Commented Mar 24 at 13:08
Add a comment  | 

1 Answer 1

Reset to default 0

SOLVED.

Json convert automatically every type correctly, also Color, DateTime, TimeSpan..
The problem was that you have to declair { get; set; } inizializing the variables of your class

TO REMEMBER: json IGNORE the public variables without { get; set; }

BEFORE:

public int[] StatsBonus;
public int[] StatsMalus;
public string Title = "";
public string Description = "";
public Color Color;
public int Total = 0;
public DateTime LastReset;
public TimeSpan RemainingTime;

BEFORE RESULT IN JSON:


{ "QuestList":[{},{}] }

LATE CORRECTION:

public int[] StatsBonus { get; set; }
public int[] StatsMalus { get; set; }
public string Title { get; set; } = "";
public string Description { get; set; } = "";
public Color Color { get; set; }
public int Total { get; set; }
public DateTime LastReset { get; set; }
public TimeSpan RemainingTime { get; set; }

CORRECT RESULT IN JSON:


{ "QuestList":[{"StatsBonus":[2,2,0,0],"StatsMalus":[2,2,0,0],"Title":"quest_1","Description":"text uno","Color":{"B":212,"G":255,"R":127,"A":255,"PackedValue":4292149119},"Total":3,"LastReset":"2025-03-24T14:34:40.7261879+01:00","RemainingTime":"00:00:33"},{"StatsBonus":[0,0,2,2],"StatsMalus":[0,0,2,2],"Title":"quest_2","Description":"text due","Color":{"B":212,"G":255,"R":127,"A":255,"PackedValue":4292149119},"Total":2,"LastReset":"2025-03-24T14:34:38.73394+01:00","RemainingTime":"00:00:35"}] }

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744267835a4565950.html

相关推荐

  • c# - SaveLoad Class into Json - Monogame - Stack Overflow

    internal class Quest{public int[] StatsBonus;public int[] StatsMalus;public string Title = "";

    7天前
    70

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信