So I’m not sure how to even explain this error so the title of this post will probably need to change once someone can identify the issue.
But basically I have two game managers (Monobehaviours) that both load data from a save file.
The PopulationManager creates an list of Residents using the data from the save file (resident IDs, resident stats, etc.)
The StructureManager creates an array of Structures using the data from the save file (structure IDs, structure stats, array of Resident IDs etc.)
Then the SM gets each resident by associated ID from the PM to put them in the corresponding structure.
Now all of this works as intended on the FIRST time pressing “Play Mode” after scripts recompile.
But if I stop the run and press “Play Mode” again, the two managers load their data correctly but when it comes time for the SM to access the resident list from the PM, the list is now empty.
This will then happen on every subsequent run of “play mode”.
So the PM is saving the data correctly every time (and the list is full), but somehow the list is being reset before being accessed by the SM, but NOT on the first time hitting “play mode”.
Now I have checked that there aren’t multiple instances of the managers and that SM is accessing the same PM instance that saved the list of residents.
First time hitting play mode gives the following:
Every subsequent 'play mode" gives the following.
I’m not sure how this is even possible. Any help would be greatly appreciated.
If I enable Reload Domain under Enter Play Mode Options it works fine every time. But I feel like that is not really solving the problem.
Alright so I solved it.
Basically it came down to an incorrect execution order between a static repository of the managers and when those said managers got their local references to the other managers from the repository.
I thought I should explain it here in case someone has this problem in the future.
Essentially what was happening in order was:
- SaveLoadManager uses static repository to get references to other managers which it stores locally - as the repo has no cached references when first started it finds the managers and caches the references.
- A GameInitilizer monobehaviour would then clear the static repository.
- Then other managers would use the static repository to get references to other mangers - again the repo has no cached references as it just got cleared, it finds the managers and caches the references.
This weird order doesn’t cause a problem the first time it runs as the repo is empty at step 1 and 3.
But then on subsequent runs, during step 1 the SaveLoadManager will use the static repo to get references to the OLD managers from the previous run that are still stored in the static repo before it gets cleared and finds the new references.
So to prevent this kind of problem in the future, I manually set the GameInitilizer to have an earlier ExecutionOrder than the GameManagers so the static repo is cleared before it is ever used.
Also, no manager will keep local references to the other managers which might become out of date. Instead it will always use the static repo which should have the most up to date references.