rolopogo / ValheimMods

MIT License
20 stars 24 forks source link

Fix basement prefab being destroyed on scene change #32

Closed jsza closed 3 years ago

jsza commented 3 years ago

Hopefully fixes #30

This also fixes basement prefab not loading sometimes when logging out and back in.

jsza commented 3 years ago

For more information:

Seems like the basement prefab is getting destroyed for some reason (maybe by https://docs.unity3d.com/ScriptReference/Resources.UnloadUnusedAssets.html) on logout and the entry in hammerPieceTable.m_pieces becomes null. EpicLoot throws an exception trying to call null.GetComponent(...) here.

[Error  : Unity Log] NullReferenceException
Stack trace:
UnityEngine.GameObject.GetComponent[T] () (at <51725b129ecd4967a9a213c1ae65b295>:0)
EpicLoot.EpicLoot+<>c.<TryRegisterItems>b__57_0 (UnityEngine.GameObject x) (at <c2581fb3cb7b49c6b741dafa6fdd4d5d>:0)
System.Linq.Enumerable+WhereSelectListIterator`2[TSource,TResult].MoveNext () (at <351e49e2a5bf4fd6beabb458ce2255f3>:0)
System.Collections.Generic.List`1[T].InsertRange (System.Int32 index, System.Collections.Generic.IEnumerable`1[T] collection) (at <eae584ce26bc40229c1b1aa476bfa589>:0)
System.Collections.Generic.List`1[T].AddRange (System.Collections.Generic.IEnumerable`1[T] collection) (at <eae584ce26bc40229c1b1aa476bfa589>:0)
EpicLoot.EpicLoot.TryRegisterItems () (at <c2581fb3cb7b49c6b741dafa6fdd4d5d>:0)
EpicLoot.ObjectDB_CopyOtherDB_Patch.Postfix () (at <c2581fb3cb7b49c6b741dafa6fdd4d5d>:0)
(wrapper dynamic-method) ObjectDB.DMD<ObjectDB::CopyOtherDB>(ObjectDB,ObjectDB)
FejdStartup.SetupObjectDB () (at <64e3050735a54dbb8762d6e3432a1162>:0)
(wrapper dynamic-method) FejdStartup.DMD<FejdStartup::Start>(FejdStartup)
jsza commented 3 years ago

Right, so turns out all that was needed here is https://docs.unity3d.com/ScriptReference/Object.DontDestroyOnLoad.html

Sorry about the earlier mess; I've pushed a new commit with minimal changes and this seems to work fine.

jsza commented 3 years ago

@rolopogo In case you haven't seen this, no stress if you're busy.