diff --git a/docs/tips/logic.md b/docs/tips/logic.md new file mode 100644 index 0000000..9c44441 --- /dev/null +++ b/docs/tips/logic.md @@ -0,0 +1,57 @@ +# Executing your own logic + +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 + +````lua +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](http://www.computercraft.info/wiki/Parallel_(API)) what exactly parallel.waitForAll() does + +## Number 2: +Using threads + +````lua +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 + +````lua +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 \ No newline at end of file