The error you're encountering:
(error) notepad.lua: ...ads\cleaned originalSA by kichiro\moonloader\notepad.lua:490: attempt to call field 'open' (a nil value)
stack traceback:
...ads\cleaned originalSA by kichiro\moonloader\notepad.lua:490: in function 'applyCustomStyle'
...ads\cleaned originalSA by kichiro\moonloader\notepad.lua:531: in main chunk
(error) notepad.lua: Script died due to an error. (27EE711C)
indicates that there's an attempt to call a function named open, which is nil at the point of invocation. This error typically arises when a function is either undefined or shadowed by another variable with the same name.
Root Cause
Upon reviewing your script, the issue stems from a common mistake related to variable shadowing, specifically with the io module in Lua.
Here's the problematic part in your applyCustomStyle function:
local function applyCustomStyle()
local io = imgui.GetIO()
local style = imgui.GetStyle()
local colors = style.Colors
local clr = imgui.Col
local ImVec4 = imgui.ImVec4
-- [Styling code...]
-- Load a font that supports Cyrillic characters
local fontPath = "path/to/DejaVuSans.ttf" -- Укажите правильный путь к шрифту
local fontSize = 16.0 -- Задайте размер шрифта по желанию
-- Проверка существования файла шрифта
local fontFile = io.open(fontPath, "rb")
if not fontFile then
showNotification("Шрифт не найден: " .. fontPath, 0xFF0000)
else
fontFile:close()
local font = io.Fonts:AddFontFromFileTTF(fontPath, fontSize, nil, io.Fonts:GetGlyphRangesCyrillic())
if not font then
showNotification("Не удалось загрузить шрифт: " .. fontPath, 0xFF0000)
else
showNotification("Шрифт успешно загружен: " .. fontPath, 0x00FF00)
imgui.PushFont(font) -- Применение загруженного шрифта
end
end
end
Issue Explained:
-
Shadowing the io Module:
- You declared a local variable
io and assigned it the value of imgui.GetIO().
- This shadows the global
io module provided by Lua, which is responsible for file operations like io.open, io.read, and io.write.
-
Attempting to Use io.open:
- Later in the function, when you call
io.open(fontPath, "rb"), Lua tries to find the open method on the local io (which is actually imgui.GetIO()), not the global io module.
- Since
imgui.GetIO() does not have an open method, Lua throws the error attempt to call field 'open' (a nil value).
Solution
To resolve this, avoid using io as a local variable name when you need to access the global io module. Instead, use a different variable name for the ImGui IO object.
Here's how you can fix the issue:
-
Rename the Local io Variable:
- Change the local variable from
io to something else, such as imguiIO.
-
Update References Accordingly:
- Ensure that all references to
io within the applyCustomStyle function are updated to use the new local variable name.
Updated applyCustomStyle Function:
--- Applies a custom style to the ImGui interface.
local function applyCustomStyle()
local imguiIO = imgui.GetIO() -- Renamed from 'io' to 'imguiIO'
local style = imgui.GetStyle()
local colors = style.Colors
local clr = imgui.Col
local ImVec4 = imgui.ImVec4
-- Window Styling
style.WindowRounding = 5.0
style.WindowTitleAlign = imgui.ImVec2(0.5, 0.5)
style.ChildWindowRounding = 5.0
style.FrameRounding = 5.0
style.ItemSpacing = imgui.ImVec2(10.0, 10.0)
style.ScrollbarSize = 15.0
style.ScrollbarRounding = 5.0
style.GrabMinSize = 10.0
style.GrabRounding = 5.0
-- Color Scheme
colors[clr.WindowBg] = ImVec4(0.1, 0.1, 0.1, 1.0)
colors[clr.TitleBg] = ImVec4(0.2, 0.2, 0.2, 1.0)
colors[clr.TitleBgActive] = ImVec4(0.3, 0.3, 0.3, 1.0)
colors[clr.TitleBgCollapsed] = ImVec4(0.15, 0.15, 0.15, 1.0)
colors[clr.Button] = ImVec4(0.2, 0.2, 0.2, 1.0)
colors[clr.ButtonHovered] = ImVec4(0.3, 0.3, 0.3, 1.0)
colors[clr.ButtonActive] = ImVec4(0.4, 0.4, 0.4, 1.0)
colors[clr.FrameBg] = ImVec4(0.2, 0.2, 0.2, 1.0)
colors[clr.FrameBgHovered] = ImVec4(0.3, 0.3, 0.3, 1.0)
colors[clr.FrameBgActive] = ImVec4(0.4, 0.4, 0.4, 1.0)
colors[clr.CheckMark] = ImVec4(0.0, 0.5, 0.0, 1.0)
colors[clr.Header] = ImVec4(0.2, 0.2, 0.2, 1.0)
colors[clr.HeaderHovered] = ImVec4(0.3, 0.3, 0.3, 1.0)
colors[clr.HeaderActive] = ImVec4(0.4, 0.4, 0.4, 1.0)
colors[clr.ResizeGrip] = ImVec4(0.2, 0.2, 0.2, 1.0)
colors[clr.ResizeGripHovered] = ImVec4(0.3, 0.3, 0.3, 1.0)
colors[clr.ResizeGripActive] = ImVec4(0.4, 0.4, 0.4, 1.0)
colors[clr.ScrollbarBg] = ImVec4(0.1, 0.1, 0.1, 1.0)
colors[clr.ScrollbarGrab] = ImVec4(0.2, 0.2, 0.2, 1.0)
colors[clr.ScrollbarGrabHovered] = ImVec4(0.3, 0.3, 0.3, 1.0)
colors[clr.ScrollbarGrabActive] = ImVec4(0.4, 0.4, 0.4, 1.0)
-- Load a font that supports Cyrillic characters
local fontPath = "path/to/DejaVuSans.ttf" -- Укажите правильный путь к шрифту
local fontSize = 16.0 -- Задайте размер шрифта по желанию
-- Проверка существования файла шрифта
local fontFile = io.open(fontPath, "rb") -- Using global 'io.open'
if not fontFile then
showNotification("Шрифт не найден: " .. fontPath, 0xFF0000)
else
fontFile:close()
local font = imguiIO.Fonts:AddFontFromFileTTF(fontPath, fontSize, nil, imguiIO.Fonts:GetGlyphRangesCyrillic())
if not font then
showNotification("Не удалось загрузить шрифт: " .. fontPath, 0xFF0000)
else
showNotification("Шрифт успешно загружен: " .. fontPath, 0x00FF00)
imgui.PushFont(font) -- Применение загруженного шрифта
end
end
end
Steps to Fix:
-
Rename the Local io Variable:
- Change
local io = imgui.GetIO() to local imguiIO = imgui.GetIO() to avoid shadowing the global io module.
-
Update All References:
- Replace all instances of
io within the applyCustomStyle function with imguiIO. For example:
imguiIO.Fonts:AddFontFromFileTTF(...)
imguiIO.Fonts:GetGlyphRangesCyrillic()
-
Ensure Font Path is Correct:
-
Handle Missing Glyph Ranges Function:
- Note: Ensure that
imguiIO.Fonts:GetGlyphRangesCyrillic() is a valid function in your imgui Lua binding. If it's not available, you might need to define the glyph ranges manually or use a different method to include Cyrillic characters.
-
Final Corrected applyCustomStyle:
--- Applies a custom style to the ImGui interface.
local function applyCustomStyle()
local imguiIO = imgui.GetIO() -- Renamed to avoid shadowing
local style = imgui.GetStyle()
local colors = style.Colors
local clr = imgui.Col
local ImVec4 = imgui.ImVec4
-- Window Styling
style.WindowRounding = 5.0
style.WindowTitleAlign = imgui.ImVec2(0.5, 0.5)
style.ChildWindowRounding = 5.0
style.FrameRounding = 5.0
style.ItemSpacing = imgui.ImVec2(10.0, 10.0)
style.ScrollbarSize = 15.0
style.ScrollbarRounding = 5.0
style.GrabMinSize = 10.0
style.GrabRounding = 5.0
-- Color Scheme
colors[clr.WindowBg] = ImVec4(0.1, 0.1, 0.1, 1.0)
colors[clr.TitleBg] = ImVec4(0.2, 0.2, 0.2, 1.0)
colors[clr.TitleBgActive] = ImVec4(0.3, 0.3, 0.3, 1.0)
colors[clr.TitleBgCollapsed] = ImVec4(0.15, 0.15, 0.15, 1.0)
colors[clr.Button] = ImVec4(0.2, 0.2, 0.2, 1.0)
colors[clr.ButtonHovered] = ImVec4(0.3, 0.3, 0.3, 1.0)
colors[clr.ButtonActive] = ImVec4(0.4, 0.4, 0.4, 1.0)
colors[clr.FrameBg] = ImVec4(0.2, 0.2, 0.2, 1.0)
colors[clr.FrameBgHovered] = ImVec4(0.3, 0.3, 0.3, 1.0)
colors[clr.FrameBgActive] = ImVec4(0.4, 0.4, 0.4, 1.0)
colors[clr.CheckMark] = ImVec4(0.0, 0.5, 0.0, 1.0)
colors[clr.Header] = ImVec4(0.2, 0.2, 0.2, 1.0)
colors[clr.HeaderHovered] = ImVec4(0.3, 0.3, 0.3, 1.0)
colors[clr.HeaderActive] = ImVec4(0.4, 0.4, 0.4, 1.0)
colors[clr.ResizeGrip] = ImVec4(0.2, 0.2, 0.2, 1.0)
colors[clr.ResizeGripHovered] = ImVec4(0.3, 0.3, 0.3, 1.0)
colors[clr.ResizeGripActive] = ImVec4(0.4, 0.4, 0.4, 1.0)
colors[clr.ScrollbarBg] = ImVec4(0.1, 0.1, 0.1, 1.0)
colors[clr.ScrollbarGrab] = ImVec4(0.2, 0.2, 0.2, 1.0)
colors[clr.ScrollbarGrabHovered] = ImVec4(0.3, 0.3, 0.3, 1.0)
colors[clr.ScrollbarGrabActive] = ImVec4(0.4, 0.4, 0.4, 1.0)
-- Load a font that supports Cyrillic characters
local fontPath = "C:/Fonts/DejaVuSans.ttf" -- Update to a valid path
local fontSize = 16.0 -- Задайте размер шрифта по желанию
-- Проверка существования файла шрифта
local fontFile = io.open(fontPath, "rb") -- Using global 'io.open'
if not fontFile then
showNotification("Шрифт не найден: " .. fontPath, 0xFF0000)
else
fontFile:close()
local glyphRanges = imguiIO.Fonts:GetGlyphRangesCyrillic()
if not glyphRanges then
showNotification("Глифт-диапазоны для кириллицы не найдены.", 0xFF0000)
glyphRanges = imguiIO.Fonts:GetGlyphRangesDefault()
end
local font = imguiIO.Fonts:AddFontFromFileTTF(fontPath, fontSize, nil, glyphRanges)
if not font then
showNotification("Не удалось загрузить шрифт: " .. fontPath, 0xFF0000)
else
showNotification("Шрифт успешно загружен: " .. fontPath, 0x00FF00)
imgui.PushFont(font) -- Применение загруженного шрифта
end
end
end
Additional Recommendations
-
Verify Font Path:
-
Check for Glyph Ranges Function:
- If your
imgui binding does not support GetGlyphRangesCyrillic(), consider using GetGlyphRangesDefault() or define the required glyph ranges manually.
-
Error Handling:
- Consider adding more robust error handling to ensure that your script gracefully handles scenarios where the font fails to load or other unexpected issues arise.
-
Avoid Variable Shadowing:
- As a best practice, always avoid naming local variables after global modules (
io, math, table, etc.) unless intentionally overriding them. This prevents accidental bugs and makes your code more readable.
-
Logging:
- Implement additional logging to help debug future issues. For example, log when popups are opened or closed, or when notes are added/edited/deleted.
-
Font Management:
- If you're using multiple fonts or need to support various languages, manage your fonts systematically to ensure consistency across your UI.
Conclusion
By renaming the local io variable to something like imguiIO, you prevent the shadowing of Lua's global io module, thereby avoiding the attempt to call field 'open' (a nil value) error. Ensure that all references within the applyCustomStyle function are updated accordingly and verify that the font path is correct.
If you implement these changes and still encounter issues, please provide the updated error message and relevant code sections for further assistance.