Skip to content

Commit d91ab72

Browse files
committed
Added sceneid to script property
1 parent 6ef84f3 commit d91ab72

4 files changed

Lines changed: 68 additions & 26 deletions

File tree

engine/core/manager/SceneManager.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ using namespace Supernova;
1212

1313
std::vector<SceneManager::SceneEntry> SceneManager::entries;
1414
uint32_t SceneManager::currentId = 0;
15+
std::map<uint32_t, Scene*> SceneManager::scenePtrs;
1516

1617
void SceneManager::registerScene(uint32_t id, const std::string& name, std::function<void()> factory) {
1718
// Overwrite if the id already exists
@@ -88,4 +89,22 @@ std::string SceneManager::getCurrentSceneName() {
8889
void SceneManager::clearAll() {
8990
entries.clear();
9091
currentId = 0;
92+
scenePtrs.clear();
93+
}
94+
95+
void SceneManager::setScenePtr(uint32_t id, Scene* scene) {
96+
if (scene) {
97+
scenePtrs[id] = scene;
98+
} else {
99+
scenePtrs.erase(id);
100+
}
101+
}
102+
103+
Scene* SceneManager::getScenePtr(uint32_t id) {
104+
auto it = scenePtrs.find(id);
105+
return (it != scenePtrs.end()) ? it->second : nullptr;
106+
}
107+
108+
void SceneManager::removeScenePtr(uint32_t id) {
109+
scenePtrs.erase(id);
91110
}

engine/core/manager/SceneManager.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,13 @@
99
#include <string>
1010
#include <functional>
1111
#include <vector>
12+
#include <map>
1213
#include <cstdint>
1314

1415
namespace Supernova {
1516

17+
class Scene;
18+
1619
// SceneManager allows registering named scene stacks and switching between them at runtime.
1720
// A "scene stack" corresponds to an editor SceneProject: one main Scene plus zero or more
1821
// layer Scenes. The factory function is responsible for calling Engine::setScene() and
@@ -37,6 +40,7 @@ namespace Supernova {
3740

3841
static std::vector<SceneEntry> entries;
3942
static uint32_t currentId;
43+
static std::map<uint32_t, Scene*> scenePtrs;
4044

4145
public:
4246
// Register a named scene stack.
@@ -73,6 +77,15 @@ namespace Supernova {
7377

7478
// Remove all registered scenes. Intended for editor use when resetting state.
7579
static void clearAll();
80+
81+
// Store a scene pointer associated with a scene ID for cross-scene entity resolution.
82+
static void setScenePtr(uint32_t id, Scene* scene);
83+
84+
// Retrieve a stored scene pointer by ID. Returns nullptr if not found.
85+
static Scene* getScenePtr(uint32_t id);
86+
87+
// Remove a stored scene pointer.
88+
static void removeScenePtr(uint32_t id);
7689
};
7790

7891
} // namespace Supernova

engine/core/script/LuaBinding.cpp

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "System.h"
99
#include "Engine.h"
1010
#include "Scene.h"
11+
#include "SceneManager.h"
1112
#include "component/ScriptComponent.h"
1213
#include "ScriptProperty.h"
1314
#include "object/EntityHandle.h"
@@ -569,36 +570,42 @@ void LuaBinding::initializeLuaScripts(Scene* scene) {
569570
bool foundScript = false;
570571

571572
if (targetEntity != NULL_ENTITY) {
572-
ScriptComponent* targetScriptComp = scene->findComponent<ScriptComponent>(targetEntity);
573-
if (targetScriptComp) {
574-
if (!prop.ptrTypeName.empty()) {
575-
for (auto& targetScript : targetScriptComp->scripts) {
576-
if (targetScript.type == ScriptType::SCRIPT_LUA &&
577-
targetScript.className == prop.ptrTypeName &&
578-
targetScript.enabled && targetScript.instance) {
579-
int targetRef = static_cast<int>(reinterpret_cast<intptr_t>(targetScript.instance));
580-
lua_rawgeti(L, LUA_REGISTRYINDEX, targetRef);
581-
foundScript = true;
582-
break;
573+
Scene* targetScene = scene;
574+
if (prop.sceneId != 0) {
575+
targetScene = SceneManager::getScenePtr(prop.sceneId);
576+
}
577+
if (targetScene) {
578+
ScriptComponent* targetScriptComp = targetScene->findComponent<ScriptComponent>(targetEntity);
579+
if (targetScriptComp) {
580+
if (!prop.ptrTypeName.empty()) {
581+
for (auto& targetScript : targetScriptComp->scripts) {
582+
if (targetScript.type == ScriptType::SCRIPT_LUA &&
583+
targetScript.className == prop.ptrTypeName &&
584+
targetScript.enabled && targetScript.instance) {
585+
int targetRef = static_cast<int>(reinterpret_cast<intptr_t>(targetScript.instance));
586+
lua_rawgeti(L, LUA_REGISTRYINDEX, targetRef);
587+
foundScript = true;
588+
break;
589+
}
583590
}
584-
}
585-
if (!foundScript)
586-
foundScript = pushEntityHandleByType(L, scene, targetEntity, prop.ptrTypeName);
587-
} else {
588-
for (auto& targetScript : targetScriptComp->scripts) {
589-
if (targetScript.type == ScriptType::SCRIPT_LUA &&
590-
targetScript.enabled && targetScript.instance) {
591-
int targetRef = static_cast<int>(reinterpret_cast<intptr_t>(targetScript.instance));
592-
lua_rawgeti(L, LUA_REGISTRYINDEX, targetRef);
593-
foundScript = true;
594-
break;
591+
if (!foundScript)
592+
foundScript = pushEntityHandleByType(L, targetScene, targetEntity, prop.ptrTypeName);
593+
} else {
594+
for (auto& targetScript : targetScriptComp->scripts) {
595+
if (targetScript.type == ScriptType::SCRIPT_LUA &&
596+
targetScript.enabled && targetScript.instance) {
597+
int targetRef = static_cast<int>(reinterpret_cast<intptr_t>(targetScript.instance));
598+
lua_rawgeti(L, LUA_REGISTRYINDEX, targetRef);
599+
foundScript = true;
600+
break;
601+
}
595602
}
603+
if (!foundScript)
604+
foundScript = pushEntityHandleByType(L, targetScene, targetEntity, prop.ptrTypeName);
596605
}
597-
if (!foundScript)
598-
foundScript = pushEntityHandleByType(L, scene, targetEntity, prop.ptrTypeName);
606+
} else {
607+
foundScript = pushEntityHandleByType(L, targetScene, targetEntity, prop.ptrTypeName);
599608
}
600-
} else {
601-
foundScript = pushEntityHandleByType(L, scene, targetEntity, prop.ptrTypeName);
602609
}
603610
}
604611

engine/core/script/ScriptProperty.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ namespace Supernova {
6262
// Optional: Store the actual type name for editor UI/debugging
6363
std::string ptrTypeName; // e.g., "Mesh*", "Object*", "EntityHandle*"
6464

65+
// For cross-scene entity references (0 = same scene)
66+
uint32_t sceneId = 0;
67+
6568
// Helper template to get typed value
6669
template<typename T>
6770
T getValue() const {

0 commit comments

Comments
 (0)