From b3ff0045722acec9b8c12f5f13dec4f21008d6b8 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Wed, 18 Jan 2017 21:32:38 -0500 Subject: fix PlayerEvents.LoadedGame and SaveEvents.AfterLoad being fired before the world finishes loading (#216) --- release-notes.md | 6 ++++++ src/StardewModdingAPI/Inheritance/SGame.cs | 23 +++++++++++------------ 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/release-notes.md b/release-notes.md index 6945a71a..4e91ee2e 100644 --- a/release-notes.md +++ b/release-notes.md @@ -1,5 +1,11 @@ # Release notes +## 1.7 +See [log](https://github.com/Pathoschild/SMAPI/compare/1.6...1.7). + +For mod developers: +* Fixed `PlayerEvents.LoadedGame` and `SaveEvents.AfterLoad` being fired before the world finishes initialising. + ## 1.6 See [log](https://github.com/Pathoschild/SMAPI/compare/1.5...1.6). diff --git a/src/StardewModdingAPI/Inheritance/SGame.cs b/src/StardewModdingAPI/Inheritance/SGame.cs index 8e87bac6..28c2aa42 100644 --- a/src/StardewModdingAPI/Inheritance/SGame.cs +++ b/src/StardewModdingAPI/Inheritance/SGame.cs @@ -24,8 +24,9 @@ namespace StardewModdingAPI.Inheritance /********* ** Properties *********/ - /// Whether to raise on the next tick. - private bool FireLoadedGameEvent; + /// The number of ticks until SMAPI should notify mods when is set. + /// Skipping a few frames ensures the game finishes initialising the world before mods try to change it. + private int AfterLoadTimer = 5; /// The debug messages to add to the next debug output. internal static Queue DebugMessageQueue { get; private set; } @@ -1028,17 +1029,15 @@ namespace StardewModdingAPI.Inheritance this.PreviousYearOfGame = Game1.year; } - // raise player loaded save (in the following tick to let the game finish updating first) - if (this.FireLoadedGameEvent) + // raise save loaded + if (Game1.hasLoadedGame && this.AfterLoadTimer >= 0) { - SaveEvents.InvokeAfterLoad(this.Monitor); - PlayerEvents.InvokeLoadedGame(this.Monitor, new EventArgsLoadedGameChanged(Game1.hasLoadedGame)); - this.FireLoadedGameEvent = false; - } - if (Game1.hasLoadedGame != this.PreviouslyLoadedGame) - { - this.FireLoadedGameEvent = true; - this.PreviouslyLoadedGame = Game1.hasLoadedGame; + if (this.AfterLoadTimer == 0) + { + SaveEvents.InvokeAfterLoad(this.Monitor); + PlayerEvents.InvokeLoadedGame(this.Monitor, new EventArgsLoadedGameChanged(Game1.hasLoadedGame)); + } + this.AfterLoadTimer--; } // raise mine level changed -- cgit