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) --- src/StardewModdingAPI/Inheritance/SGame.cs | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) (limited to 'src/StardewModdingAPI/Inheritance/SGame.cs') 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