[mimgui] os.clock() or os.time()

Arenes

Новичок
Автор темы
7
0
Версия MoonLoader
Другое
Салют. Несколько дней ломаю голову, как реализовать данную функцию. Опишу: При активации Checkbox выполняется функция, которая через 15 секунд выключается и через 5 секунд включается(не отключая Checkbox). Голова кипит до такой степени, что начинаю извращаться над кодом так, что глаза болят.
 
Решение
Lua:
local imgui = require 'mimgui'
local encoding = require 'encoding'
encoding.default = 'CP1251'
u8 = encoding.UTF8

local renderWindow = imgui.new.bool(true)
local FuncTimer = {
    enabled = imgui.new.bool(false),
    actve = false,
    start_time = -1
}

local newFrame = imgui.OnFrame(
    function() return renderWindow[0] end,
    function(player)
        local resX, resY = getScreenResolution()
        local sizeX, sizeY = 300, 300
        imgui.SetNextWindowPos(imgui.ImVec2(resX / 2, resY / 2), imgui.Cond.FirstUseEver, imgui.ImVec2(0.5, 0.5))
        imgui.SetNextWindowSize(imgui.ImVec2(sizeX, sizeY), imgui.Cond.FirstUseEver)
        imgui.Begin('Main Window', renderWindow)

        if imgui.Checkbox('TimeFunction'...

chapo

🫡 В армии с 17.10.2023. В ЛС НЕ ОТВЕЧАЮ
Друг
8,777
11,228
Lua:
local imgui = require 'mimgui'
local encoding = require 'encoding'
encoding.default = 'CP1251'
u8 = encoding.UTF8

local renderWindow = imgui.new.bool(true)
local FuncTimer = {
    enabled = imgui.new.bool(false),
    actve = false,
    start_time = -1
}

local newFrame = imgui.OnFrame(
    function() return renderWindow[0] end,
    function(player)
        local resX, resY = getScreenResolution()
        local sizeX, sizeY = 300, 300
        imgui.SetNextWindowPos(imgui.ImVec2(resX / 2, resY / 2), imgui.Cond.FirstUseEver, imgui.ImVec2(0.5, 0.5))
        imgui.SetNextWindowSize(imgui.ImVec2(sizeX, sizeY), imgui.Cond.FirstUseEver)
        imgui.Begin('Main Window', renderWindow)

        if imgui.Checkbox('TimeFunction', FuncTimer.enabled) then
            FuncTimer.active = FuncTimer.enabled[0]
            if FuncTimer.enabled[0] then
                FuncTimer.start_time = os.clock()
            end
        end
        if FuncTimer.active then
            imgui.Text('Disable in '..tostring(math.floor(FuncTimer.start_time + 16 - os.clock())))
        end

        imgui.End()
    end
)

function main()
    while not isSampAvailable() do wait(0) end
    sampRegisterChatCommand('mimgui', function()
        renderWindow[0] = not renderWindow[0]
    end)
    while true do
        wait(0)
        ----------------------------------------
        if FuncTimer.active then
            local time_left = math.floor(FuncTimer.start_time + 16 - os.clock()) -- тут прибавляем 16 вместо 15 так как math.floor округляет до меньшего
            if time_left < 1 then
                FuncTimer.active = false
                FuncTimer.start_time = os.clock()
            end
        else
            if FuncTimer.enabled[0] then
                local time_left = math.floor(FuncTimer.start_time + 5 - os.clock())
                if time_left < 1 then
                    FuncTimer.active = true
                    FuncTimer.start_time = os.clock()
                end
            end
        end
        ----------------------------------------

        if FuncTimer.active then
            printStringNow('ENABLED', 50)
        end
    end
end
в мейне я наговнокодил, но хотя бы работает
 

chapo

🫡 В армии с 17.10.2023. В ЛС НЕ ОТВЕЧАЮ
Друг
8,777
11,228
Lua:
...
lua_thread.create(function()
    if imgui.Button(...) then
        func1 = true
        wait(15000)
        func1 = false
        wait(5000)
        func1 = true
    end
end)
хуйня
1. кто-то писал что в имгуи нельзя создавать поток (не помню почему)
2. время будет меняться в зависимости от фпса
 

Arenes

Новичок
Автор темы
7
0
Lua:
local imgui = require 'mimgui'
local encoding = require 'encoding'
encoding.default = 'CP1251'
u8 = encoding.UTF8

local renderWindow = imgui.new.bool(true)
local FuncTimer = {
    enabled = imgui.new.bool(false),
    actve = false,
    start_time = -1
}

local newFrame = imgui.OnFrame(
    function() return renderWindow[0] end,
    function(player)
        local resX, resY = getScreenResolution()
        local sizeX, sizeY = 300, 300
        imgui.SetNextWindowPos(imgui.ImVec2(resX / 2, resY / 2), imgui.Cond.FirstUseEver, imgui.ImVec2(0.5, 0.5))
        imgui.SetNextWindowSize(imgui.ImVec2(sizeX, sizeY), imgui.Cond.FirstUseEver)
        imgui.Begin('Main Window', renderWindow)

        if imgui.Checkbox('TimeFunction', FuncTimer.enabled) then
            FuncTimer.active = FuncTimer.enabled[0]
            if FuncTimer.enabled[0] then
                FuncTimer.start_time = os.clock()
            end
        end
        if FuncTimer.active then
            imgui.Text('Disable in '..tostring(math.floor(FuncTimer.start_time + 16 - os.clock())))
        end

        imgui.End()
    end
)

function main()
    while not isSampAvailable() do wait(0) end
    sampRegisterChatCommand('mimgui', function()
        renderWindow[0] = not renderWindow[0]
    end)
    while true do
        wait(0)
        ----------------------------------------
        if FuncTimer.active then
            local time_left = math.floor(FuncTimer.start_time + 16 - os.clock()) -- тут прибавляем 16 вместо 15 так как math.floor округляет до меньшего
            if time_left < 1 then
                FuncTimer.active = false
                FuncTimer.start_time = os.clock()
            end
        else
            if FuncTimer.enabled[0] then
                local time_left = math.floor(FuncTimer.start_time + 5 - os.clock())
                if time_left < 1 then
                    FuncTimer.active = true
                    FuncTimer.start_time = os.clock()
                end
            end
        end
        ----------------------------------------

        if FuncTimer.active then
            printStringNow('ENABLED', 50)
        end
    end
end
в мейне я наговнокодил, но хотя бы работает
Спасибо за решение!
 

abbv

Потрачен
121
41
Обратите внимание, пользователь заблокирован на форуме. Не рекомендуется проводить сделки.
хуйня
1. кто-то писал что в имгуи нельзя создавать поток (не помню почему)
2. время будет меняться в зависимости от фпса
так я написал то, что попросил человек
он спросил как сделать переключатель функции, чтобы она меняла своё значение на протяжении какого то времени