SaveEvents.AfterLoad is triggered before GameLocation finishes loading
LeonBlade opened this issue ยท 2 comments
For mod authors that are trying to use the SaveEvents.AfterLoad
event to modify or just iterate through existing objects in the FarmHouse when the game has finished loading, the FarmHouse will not contain all objects to be loaded by the time the event is fired.
A simple test for this is to add an event listener to the SaveEvents.AfterLoad
and check the Game1.currentLocations.objects.Count
and verify it against the Count
again later after the screen has faded back from black.
For now, one way of getting around this is to add an event listener for LocationEvents.LocationObjectsChanged
in the SaveEvents.AfterLoad
and remove it once it's fired and do all initial processing in that event.
Example:
public class MyMod : Mod
{
public void Entry(IModHelper helper)
{
SaveEvents.AfterLoad += OnAfterLoad;
}
void OnAfterLoad(object sender, EventArgs e)
{
LocationEvents.LocationObjectsChanged += OnLocationObjectsChanged;
}
void OnLocationObjectsChanged(object sender, EventArgsLocationObjectsChanged e)
{
LocationEvents.LocationObjectsChanged -= OnLocationObjectsChanged;
// Run your code here
}
}