Skip to content

Commit e73dbfe

Browse files
committed
Use SB.delay for editor initialization like Chili does
Found the correct mechanism! Chili uses SB.delay() in main_window_panel.lua to defer editor creation until after SB.view is assigned. SB.delay(func) defers execution until the next GameFrame, ensuring: 1. View() constructor completes 2. SB.view = View() is assigned in widget.lua 3. THEN editors are created (can safely access SB.view) Changes: - Added PreCreateAllEditors() that uses SB.delay() for all editors - Called from InitializeRmlUi() to match Chili's upfront initialization - Removes lazy creation in OpenEditor() - editors should already exist - Catches errors early while avoiding SB.view nil issues This is the proper architecture, matching the original Chili approach.
1 parent 0215f40 commit e73dbfe

File tree

1 file changed

+24
-12
lines changed

1 file changed

+24
-12
lines changed

scen_edit/view/view.lua

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,30 @@ function View:InitializeRmlUi()
7373
self:BindTabEvents()
7474
self:PopulateEditorButtons(self.currentTab)
7575

76+
-- Pre-create ALL editors using SB.delay (matching Chili's approach)
77+
-- This catches errors early while avoiding SB.view nil issues
78+
self:PreCreateAllEditors()
79+
7680
-- Setup event handlers
7781
self:SetupRmlUiEvents()
7882

7983
Log.Notice("RmlUi UI initialized successfully - editors use field compatibility layer")
8084
end
8185

86+
function View:PreCreateAllEditors()
87+
-- Pre-create all registered editors using SB.delay
88+
-- This matches the original Chili approach in main_window_panel.lua
89+
-- Defers creation until after SB.view is assigned
90+
for name, editorCfg in pairs(SB.editorRegistry) do
91+
SB.delay(function()
92+
if not SB.editors[name] and editorCfg.editor then
93+
Log.Notice("Pre-creating editor: " .. name)
94+
SB.editors[name] = editorCfg.editor()
95+
end
96+
end)
97+
end
98+
end
99+
82100
function View:SetupRmlUiEvents()
83101
if not self.mainDocument then
84102
return
@@ -326,7 +344,7 @@ function View:PopulateEditorButtons(tabName)
326344
end
327345

328346
editorPanel.inner_rml = buttonsHTML
329-
347+
330348
-- Bind click events to editor buttons
331349
for _, editorCfg in ipairs(editors) do
332350
local btnId = "editor-btn-" .. editorCfg.name
@@ -342,19 +360,13 @@ end
342360
function View:OpenEditor(editorName)
343361
Log.Notice("Opening editor: " .. editorName)
344362

345-
-- Create editor lazily on first use
346-
if not SB.editors[editorName] then
347-
local editorCfg = SB.editorRegistry[editorName]
348-
if editorCfg and editorCfg.editor then
349-
Log.Notice(" Lazily creating editor: " .. editorName)
350-
SB.editors[editorName] = editorCfg.editor()
351-
else
352-
Log.Error("Editor not found in registry: " .. editorName)
353-
return
354-
end
363+
-- Editor should be pre-created by PreCreateAllEditors()
364+
local editor = SB.editors[editorName]
365+
if not editor then
366+
Log.Error("Editor not yet created: " .. editorName .. " (still initializing?)")
367+
return
355368
end
356369

357-
local editor = SB.editors[editorName]
358370
local editorCfg = SB.editorRegistry[editorName]
359371
local mainContent = self.mainDocument:GetElementById("main-content")
360372

0 commit comments

Comments
 (0)