Skip to content

Executing own logic

Robert Jelic edited this page May 28, 2022 · 3 revisions

You question yourself how you can execute your own logic while basalt is also active? There are multiple ways of doing that:

Number 1:

Using parallel.waitForAll

local basalt = dofile("basalt.lua")

local mainFrame = basalt.createFrame("mainFrame"):show()-- lets create a frame and a button without functionality
mainFrame:addButton("aButton"):onClick(function() end):show()

local function yourCustomHandler()
    while true do
        -- add your logic here
        os.sleep(1) -- you need something which calls coroutine.yield(), yes os.sleep does that os.pullEvent() aswell
    end
end

parallel.waitForAll(basalt.autoUpdate, yourCustomHandler) -- here it will handle your function (yourCustomHandler) and basalts handlers at the time

You can read here what exactly parallel.waitForAll() does

Number 2:

Using threads

local basalt = dofile("basalt.lua")

local mainFrame = basalt.createFrame("mainFrame"):show()-- lets create a frame, a button without functionality and a thread
mainFrame:addButton("aButton"):onClick(function() end):show()
local thread = mainFrame:addThread("customHandlerExecutingThread")

local function yourCustomHandler()
    while true do
        -- add your logic here
        os.sleep(1) -- you need something which calls coroutine.yield(), yes os.sleep does that os.pullEvent() aswell
    end
end
thread:start(yourCustomHandler) -- this will create a coroutine and starts the coroutine, os.sleep does the rest, so you just have to call start once.

Number 3:

Using timers

local basalt = dofile("basalt.lua")

local mainFrame = basalt.createFrame("mainFrame"):show()-- lets create a frame, a button without functionality and a timer
mainFrame:addButton("aButton"):onClick(function() end):show()
local timer = mainFrame:addTimer("customHandlerExecutingTimer")

local function yourCustomHandler()
    -- add your logic here
end
timer:onCall(yourCustomHandler):setTime(1, -1):start() -- this will call your function every second until you :cancel() the timer

Wiki Navigation

Home
Clone this wiki locally