Index: source/gui/GUIManager.h =================================================================== --- source/gui/GUIManager.h +++ source/gui/GUIManager.h @@ -110,7 +110,7 @@ /** * See CGUI::FindObjectByName; applies to the currently active page. */ - IGUIObject* FindObjectByName(const CStr& name) const; + IGUIObject* FindObjectByName(const ScriptInterface* scriptInterface, const CStr& name) const; /** * See CGUI::SendEventToAll; applies to the currently active page. @@ -166,7 +166,6 @@ shared_ptr top() const; - shared_ptr m_CurrentGUI; // used to latch state during TickObjects/LoadPage (this is kind of ugly) shared_ptr m_ScriptRuntime; shared_ptr m_ScriptInterface; Index: source/gui/GUIManager.cpp =================================================================== --- source/gui/GUIManager.cpp +++ source/gui/GUIManager.cpp @@ -231,10 +231,6 @@ } } - // Remember this GUI page, in case the scripts call FindObjectByName - shared_ptr oldGUI = m_CurrentGUI; - m_CurrentGUI = page.gui; - page.gui->SendEventToAll("load"); shared_ptr scriptInterface = page.gui->GetScriptInterface(); @@ -254,8 +250,6 @@ if (scriptInterface->HasProperty(global, "init") && !scriptInterface->CallFunctionVoid(global, "init", initDataVal, hotloadDataVal)) LOGERROR("GUI page '%s': Failed to call init() function", utf8_from_wstring(page.name)); - - m_CurrentGUI = oldGUI; } Status CGUIManager::ReloadChangedFile(const VfsPath& path) @@ -358,14 +352,15 @@ return top()->GetPreDefinedColor(name, output); } -IGUIObject* CGUIManager::FindObjectByName(const CStr& name) const +IGUIObject* CGUIManager::FindObjectByName(const ScriptInterface* scriptInterface, const CStr& name) const { - // This can be called from scripts run by TickObjects, - // and we want to return it the same GUI page as is being ticked - if (m_CurrentGUI) - return m_CurrentGUI->FindObjectByName(name); - else - return top()->FindObjectByName(name); + // Figure out the relevant GUI page from the caller + for (const SGUIPage& page : m_PageStack) + if (scriptInterface == page.gui->GetScriptInterface().get()) + return page.gui->FindObjectByName(name); + + JS_ReportError(scriptInterface->GetContext(), "FindObjectByName could not find GUI page!"); + return nullptr; } void CGUIManager::SendEventToAll(const CStr& eventName) const @@ -385,11 +380,7 @@ PageStackType pageStack = m_PageStack; for (const SGUIPage& p : pageStack) - { - m_CurrentGUI = p.gui; p.gui->TickObjects(); - } - m_CurrentGUI.reset(); } void CGUIManager::Draw() @@ -407,11 +398,9 @@ for (const SGUIPage& p : pageStack) { - m_CurrentGUI = p.gui; p.gui->UpdateResolution(); p.gui->SendEventToAll("WindowResized"); } - m_CurrentGUI.reset(); } bool CGUIManager::TemplateExists(const std::string& templateName) const Index: source/gui/scripting/JSInterface_GUIManager.cpp =================================================================== --- source/gui/scripting/JSInterface_GUIManager.cpp +++ source/gui/scripting/JSInterface_GUIManager.cpp @@ -46,9 +46,9 @@ g_GUI->PopPageCB(pCxPrivate->pScriptInterface->WriteStructuredClone(args)); } -JS::Value JSI_GUIManager::GetGUIObjectByName(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), const std::string& name) +JS::Value JSI_GUIManager::GetGUIObjectByName(ScriptInterface::CxPrivate* pCxPrivate, const std::string& name) { - IGUIObject* guiObj = g_GUI->FindObjectByName(name); + IGUIObject* guiObj = g_GUI->FindObjectByName(pCxPrivate->pScriptInterface, name); if (!guiObj) return JS::UndefinedValue();