Skip to content

Commit 56167f5

Browse files
committed
Changed save file handling
Warnings are pushed when save can't be deserialized or found. Losing battle deletes save.
1 parent 8492756 commit 56167f5

3 files changed

Lines changed: 19 additions & 5 deletions

File tree

Globals/StageProducer.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,7 @@ public bool LoadGame()
6161
SaveSystem.SaveFile sv = SaveSystem.LoadGame();
6262
if (sv == null)
6363
{
64-
GD.PushError(
65-
"StageProducer.LoadGame(): Can't load game, either file 404 or invalid file."
66-
);
64+
GD.PushWarning("Can't load game, either file 404 or invalid file.");
6765
return false;
6866
}
6967
GlobalRng.Seed = sv.RngSeed;
@@ -144,7 +142,7 @@ public void TransitionStage(Stages nextStage, int nextRoomIdx = -1)
144142
GetTree().Quit();
145143
return;
146144
default:
147-
GD.Print($"Error Scene Transition is {nextStage}");
145+
GD.PushError($"Error Scene Transition is {nextStage}");
148146
break;
149147
}
150148

SaveData/SaveSystem.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using System.Collections.Generic;
23
using System.IO;
34
using System.Linq;
@@ -202,9 +203,23 @@ public static SaveFile LoadGame()
202203
GD.Print(json);
203204

204205
file.Close();
205-
SaveFile sv = JsonSerializer.Deserialize<SaveFile>(json);
206+
SaveFile sv;
207+
try
208+
{
209+
sv = JsonSerializer.Deserialize<SaveFile>(json);
210+
}
211+
catch (JsonException)
212+
{
213+
GD.PushWarning("Cannot deserialize save file, returning null.");
214+
return null;
215+
}
206216
return sv;
207217
}
208218

219+
public static void ClearSave()
220+
{
221+
DirAccess.RemoveAbsolute(UserSavePath);
222+
}
223+
209224
#endregion
210225
}

scenes/BattleDirector/scripts/BattleDirector.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ private void BattleWon()
224224
private void BattleLost()
225225
{
226226
Audio.StreamPaused = true;
227+
SaveSystem.ClearSave();
227228
AddChild(GD.Load<PackedScene>("res://scenes/UI/EndScreen.tscn").Instantiate());
228229
GetTree().Paused = true;
229230
}

0 commit comments

Comments
 (0)