Window Instances

If your editor tool can have instances (for example de Inspector tool window) you can use a helper class in lua/ge/extensions/editor/api/guiInstancer.lua.

Example:

-- we use a small helper to keep our instances array
local guiInstancer = require("editor/api/guiInstancer")

-- when the user chooses the `Window` menu item for your tool, will create a new window instance
local function onWindowMenuItem()
  -- lets add a new window instance, the returned value is the index in the .instances field
  local index = guiInstancer.addInstance()
  guiInstancer.instances[index].locked = true -- will lock the window to a particular edited object
  guiInstancer.instances[index].objectId = someIdHere -- we want this object to be edited in this window
end

local function renderMyWindow(instance)
    --   Render your widgets and logic in the window instance here
    -- the instance table can have various members pertaining to that instance.
    --   It can hold a instanceId for example, so you can identify the edited
    -- object or resource for that particular window instance.
    --   It also holds locked boolean, if true then the window is locked to that
    -- particular instance id.
end

local function onEditorGui()
    -- we go over all window instances
    for index, instance in pairs(guiInstancer.instances) do
        -- force visible true, we'll check after
        showWindow[0] = true
        -- we start a window here, we must make this window title unique (due to imgui logic),
        -- thus we add the index number to the window name
        if imgui.Begin("MyWindow #" .. tostring(index), showWindow) then
            -- it seems the user closed the window instance, so remove it
            if not showWindow[0] then
                guiInstancer.removeInstance(index)
            else
                -- render this window instance widgets/gui
                renderMyWindow(instance)
            end
        end
    end
end

local function onEditorInitialized()
    -- optional, you can spawn a default window instance on editor start up
    guiInstancer.addInstance()
    -- no need to set objectId or locked, this window instance will be global for now
    -- it all depends on your logic, for example the Scene Tree window instances will
    -- retain their scroll position, but the scene tree will be common to all instances
end

M.onEditorInitialized = onEditorInitialized
M.onEditorGui = onEditorGui

return M

For further reference on multiple instance tools you can check the code of the lua/ge/extensions/editor/sceneTree.lua.

image image

Last modified: December 7, 2021

Any further questions?

Join our discord
Our documentation is currently incomplete and undergoing active development. If you have any questions or feedback, please visit this forum thread.