This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
TehTub's profile picture

Advice on achieving "multi-threaded" effect?

Started by TehTub, 24 March 2015 - 09:28 AM
TehTub #1
Posted 24 March 2015 - 10:28 AM
Hi,

The stickied post advises that I should provide a good over view of what I want/expect my code to do but I'm by no means great at programming and even worse at explaining it, but I'll try.

The generalitys of the situation is this. I have some deep experience with another language (C#) and while most of my experience doesn't transfer over to LUA (in fact some of it causes flat-forehead moments) mostly what I'm strugling with is the lack of (or percieved lack of) threading. To get to the point I have a computer that I wish to display a bar graph, a couple updating labels (Current stored RF, Run time, ect) and two buttons (On and Off). Writing the API needed for the actual displaying of these elements was realtivly easy (with the guiding hand of various Button API's). The issue I have is that the code that will be waiting for input (monitor_touch event) will block the rest of the elements displayed on the monitor from updating. In C# I'd handle this by spinning off a thread to hanlde the updating of the GUI so the processing code could still function but as far as my google-fu and sreaching of these forums has availed me I can't seem to find anything like that for LUA or CC. Of course this is 6AM so I'm more then likely missing something obvious but on the off chance I am not I'd like some advice.

I don't really need any hard code examples, just a push in the right direction. Is there something I am missing that can be used like the Thread class in C#? Something that will allow one code path to wait for user input while the rest of the code path continues as normal?

Thanks for any help.
DannySMc #2
Posted 24 March 2015 - 03:13 PM
This is a simple fix, if you are wanting to wait for more than one event, then you don't need co routines at all! You need a simple pull event. Like so:

while true do
    local args = { os.pullEvent() }
    if args[1] == "monitor_touch" then
        -- Do the touch screen part here
    elseif args[1] == "timer" then
        -- This will allow you to update your time on your clock
    end
end

So there you have it, you can use something that will wait for multiple events. If you need to figure out the events to wait for then look here

Please also note I add all my return arguments into a table, so in the table args you will find:
args[1] -> This will be the first return which is the name of the event, example would be monitor_touch,
args[2] -> This is the second return data would be the X of where they clicked
args[3] -> This is the 3rd and would be the Y of where they clicked :D/>

Need help in depth look here on how to make a touch screen as It helps massively with waiting for multiple events:
Click here
KingofGamesYami #3
Posted 24 March 2015 - 03:52 PM
As Danny pointed out, this doesn't require the use of "threads" at all. However, if, sometime in the future, you want to create something that uses multitasking and therefor cannot make use of a simple event loop, you'll want to look into coroutines. There's a really good tutorial on coroutines in the tutorial section (too lazy to look it up) by Bubba.
CrazedProgrammer #4
Posted 24 March 2015 - 04:14 PM
There is no multithreading in lua, but you can use coroutines to pause in the middle of a function and then resume it later on.
You can even pass values when pausing and resuming.
You can replicate some kind of multithreading by making a coroutine manager.
There is a really good tutorial by Bubba on coroutines.

But for your purpose, you can catch multiple events like DannySMc pointed out.
If you want to do stuff frequently (up to 20 times per second), then you can use a timer:

local timer = os.startTimer(0.05)  --1/20 of a second
while true do
  local e = {os.pullEvent()}
  if e[1] == "timer" and e[2] == timer then
    timer = os.startTimer(0.05)
    --Do stuff frequently
  end
end
TehTub #5
Posted 24 March 2015 - 08:03 PM
Thank you all very much, thats just the functionality I was looking for (and I feel rather goofy for missing, I blame mornings). And here I thought I would need something much more complicated to achieve the same effect. I will look into those handy links you all provided though just in case in the furture something cant be solved so easily.