From 7946eade95f890300934c3bc145c29e60216c32c Mon Sep 17 00:00:00 2001
From: HylianFreddy <82058772+HylianFreddy@users.noreply.github.com>
Date: Mon, 5 Aug 2024 23:55:25 +0200
Subject: [PATCH] Fix stale storedObjId [OSH token crash]

When spawning a model, the storedObjId should always be updated even if the object is already loaded.
---
 code/source/rnd/models.cpp | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/code/source/rnd/models.cpp b/code/source/rnd/models.cpp
index 558c74a..48a90f2 100644
--- a/code/source/rnd/models.cpp
+++ b/code/source/rnd/models.cpp
@@ -60,9 +60,12 @@ namespace rnd {
   void Model_GetObjectBankIndex(Model* model, game::act::Actor* actor, game::GlobalContext* globalCtx) {
     s32 objectBankIdx = ExtendedObject_GetIndex(&globalCtx->object_context, model->itemRow->objectId);
     if (objectBankIdx < 0) {
-      storedObjId = model->itemRow->objectId;
       objectBankIdx = ExtendedObject_Spawn(&globalCtx->object_context, model->itemRow->objectId);
     }
+    storedObjId = model->itemRow->objectId;
+#if defined ENABLE_DEBUG || defined DEBUG_PRINT
+    rnd::util::Print("%s: storedObjId is %#04x\n", __func__, storedObjId);
+#endif
     model->objectBankIdx = objectBankIdx;
   }