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 |1 Answer
Reset to default 0SOLVED.
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
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 doessaveData
look like? – stuartd Commented Mar 23 at 23:30