mimgui добавление кнопки

KmAuArJo850

Новичок
Автор темы
20
2
Версия MoonLoader
Другое
как сделать такую кнопку, которая создает другие и чтобы они сохранялись?
1707592853452.png
 

MLycoris

Режим чтения
Проверенный
1,830
1,886
простой пример с добавлением элементов и жсон
Lua:
local imgui = require 'mimgui'
local ffi = require 'ffi'
local encoding = require 'encoding'
encoding.default = 'CP1251'
local u8 = encoding.UTF8

function json(filePath)
    local filePath = getWorkingDirectory()..'\\config\\'..(filePath:find('(.+).json') and filePath or filePath..'.json')
    local class = {}
    if not doesDirectoryExist(getWorkingDirectory()..'\\config') then
        createDirectory(getWorkingDirectory()..'\\config')
    end
    function class:Save(tbl)
        if tbl then
            local F = io.open(filePath, 'w')
            F:write(encodeJson(tbl) or {})
            F:close()
            return true, 'ok'
        end
        return false, 'table = nil'
    end
    function class:Load(defaultTable)
        if not doesFileExist(filePath) then
            class:Save(defaultTable or {})
        end
        local F = io.open(filePath, 'r+')
        local TABLE = decodeJson(F:read() or {})
        F:close()
        for def_k, def_v in next, defaultTable do
            if TABLE[def_k] == nil then
                TABLE[def_k] = def_v
            end
        end
        return TABLE
    end
    return class
end
local testJSON = json('testJSON.json'):Load({})
function save()
    json('testJSON.json'):Save(testJSON)
end

local WinState = imgui.new.bool()
local whatSave = {
    name = imgui.new.char[256](),
}

imgui.OnFrame(function() return WinState[0] end, function(player)
    imgui.SetNextWindowPos(imgui.ImVec2(500,500), imgui.Cond.FirstUseEver, imgui.ImVec2(0.5, 0.5))
    imgui.SetNextWindowSize(imgui.ImVec2(500, 500), imgui.Cond.Always)
    imgui.Begin('##Window', WinState, imgui.WindowFlags.NoResize)
    imgui.InputText('Name',whatSave.name,256)
    if imgui.Button(u8'Добавить') then
        table.insert(testJSON, {
            name = u8:decode(ffi.string(whatSave.name)),
        }) save()
        whatSave = {
            name = imgui.new.char[256](),
        }
    end
    imgui.SameLine()
    if imgui.Button(u8'Очистить') then
        whatSave = {
            name = imgui.new.char[256](),
        }
    end
    imgui.Separator()
    for index, data in pairs(testJSON) do
        if imgui.Button(u8(data.name)..'##'..index) then
            sampAddChatMessage('Ты нажал кнопку '..data.name,-1)
        end
        imgui.SameLine()
        if imgui.Button(u8'Удалить##'..index) then
            table.remove(testJSON,index)
            save()
        end
    end
    imgui.End()
end)

function main()
    sampRegisterChatCommand('cmd', function() WinState[0] = not WinState[0] end)
    wait(-1)
end
 
  • Нравится
  • Грустно
Реакции: Anti... и qdIbp

KmAuArJo850

Новичок
Автор темы
20
2
Script.lua: json parse: Invalid value. (at 1)
stack traceback:
[C]: in function 'decodeJson'
Script.lua:27: in function 'Load'
Script.lua:38: in main chunk

чет не очень сохраняется

простой пример с добавлением элементов и жсон
Lua:
local imgui = require 'mimgui'
local ffi = require 'ffi'
local encoding = require 'encoding'
encoding.default = 'CP1251'
local u8 = encoding.UTF8

function json(filePath)
    local filePath = getWorkingDirectory()..'\\config\\'..(filePath:find('(.+).json') and filePath or filePath..'.json')
    local class = {}
    if not doesDirectoryExist(getWorkingDirectory()..'\\config') then
        createDirectory(getWorkingDirectory()..'\\config')
    end
    function class:Save(tbl)
        if tbl then
            local F = io.open(filePath, 'w')
            F:write(encodeJson(tbl) or {})
            F:close()
            return true, 'ok'
        end
        return false, 'table = nil'
    end
    function class:Load(defaultTable)
        if not doesFileExist(filePath) then
            class:Save(defaultTable or {})
        end
        local F = io.open(filePath, 'r+')
        local TABLE = decodeJson(F:read() or {})
        F:close()
        for def_k, def_v in next, defaultTable do
            if TABLE[def_k] == nil then
                TABLE[def_k] = def_v
            end
        end
        return TABLE
    end
    return class
end
local testJSON = json('testJSON.json'):Load({})
function save()
    json('testJSON.json'):Save(testJSON)
end

local WinState = imgui.new.bool()
local whatSave = {
    name = imgui.new.char[256](),
}

imgui.OnFrame(function() return WinState[0] end, function(player)
    imgui.SetNextWindowPos(imgui.ImVec2(500,500), imgui.Cond.FirstUseEver, imgui.ImVec2(0.5, 0.5))
    imgui.SetNextWindowSize(imgui.ImVec2(500, 500), imgui.Cond.Always)
    imgui.Begin('##Window', WinState, imgui.WindowFlags.NoResize)
    imgui.InputText('Name',whatSave.name,256)
    if imgui.Button(u8'Добавить') then
        table.insert(testJSON, {
            name = u8:decode(ffi.string(whatSave.name)),
        }) save()
        whatSave = {
            name = imgui.new.char[256](),
        }
    end
    imgui.SameLine()
    if imgui.Button(u8'Очистить') then
        whatSave = {
            name = imgui.new.char[256](),
        }
    end
    imgui.Separator()
    for index, data in pairs(testJSON) do
        if imgui.Button(u8(data.name)..'##'..index) then
            sampAddChatMessage('Ты нажал кнопку '..data.name,-1)
        end
        imgui.SameLine()
        if imgui.Button(u8'Удалить##'..index) then
            table.remove(testJSON,index)
            save()
        end
    end
    imgui.End()
end)

function main()
    sampRegisterChatCommand('cmd', function() WinState[0] = not WinState[0] end)
    wait(-1)
end
не идет сохранение
 
Последнее редактирование:

MLycoris

Режим чтения
Проверенный
1,830
1,886
Script.lua: json parse: Invalid value. (at 1)
stack traceback:
[C]: in function 'decodeJson'
Script.lua:27: in function 'Load'
Script.lua:38: in main chunk

чет не очень сохраняется


не идет сохранение
я хз как ты вставляешь в свой код, конкретно то что я скинул работает и сохраняет, мб ты ещё для монетлоадера пишешь?
 

KmAuArJo850

Новичок
Автор темы
20
2
Нет, все
я хз как ты вставляешь в свой код, конкретно то что я скинул работает и сохраняет, мб ты ещё для монетлоадера пишешь?
нет, просто пытаюсь вставить код, он работает но после сохранения кнопки больше не запускается, только если удалить json