How would I start? Would this count as a coroutine manager and buffer?
So when I change tab it will always store what was on the other tab and when I come back it is still there?
local co1 = couroutine.create(function ()
print("Hello World!")
end)
tProcesses = {}
table.insert(tProcesses, co1)
tProcesses[1]
--or
tProcesses.co1()
[/SPOILER]
The example means that I want to make it dynamic not just make a few coroutines for set functions, I want to be able to make many functions like:
[CODE]
thread.new(nameofprocess)
or something like that.
local myTable = {"first"}
table.insert(myTable, "second")
-- same as:
myTable[#myTable + 1] = "second"
myTable #--> {"first", "second"}
table.insert(myTable, 2, "third")
-- same as:
myTable[3] = myTable[2]
myTable[2] = "third"
myTable #--> {"first", "third", "second"}
That tutorial I linked actually explains how multitasking works in CC. After you read that, I would suggest to first examine the parallel API code, it's a bit shorter and simpler than Multishell. Once you get that, move on to Multishell.
After looking at your example with putting something into a table, do you know exactly how tables and the table library works?local myTable = {"first"} table.insert(myTable, "second") -- same as: myTable[#myTable + 1] = "second" myTable #--> {"first", "second"} table.insert(myTable, 2, "third") -- same as: myTable[3] = myTable[2] myTable[2] = "third" myTable #--> {"first", "third", "second"}
local program_function = loadfile( "program" )
--#now it's a function; you can use it
Ahh that's awesome thank you! Any idea on the other parts?local program_function = loadfile( "program" ) --#now it's a function; you can use it
Perhaps you could use the spawnBackground function on line 222?
Edit: Oh, also it'll return to the parent any time it yields, ei pulls an event. (I think, it's not my API)
Do you really expect anyone to reply in one hour? Wait a day, well, at least 12 hours. I think that you should have just posted all these recent questions about coroutines in one thread, because you're addressing basically the same problem in each of the thread. I'm not aware of any coroutine manager and buffer which works together as an API on the forums.
You can make a very simple coroutine manager by making a buffer and an update function.No I seriously did read it, I understand it I just am not completely sure how to make like a manager? If you get what I mean? I shall have a look at the parallel API.
buffer = { }
function update()
local i = 1
while i <= #buffer do
local ok, err = coroutine.resume(buffer[i])
-- if not ok then error(err) end
if coroutine.status(buffer[i]) == "dead" then
table.remove(buffer, i)
else
i = i + 1
end
end
end
-- Test program
function test()
print("test 1")
coroutine.yield()
print("test 2")
end
table.insert(buffer, coroutine.create(test)) -- Adds the coroutine to the buffer
update() -- Prints test 1
os.sleep(1) -- Waits 1 second
update() -- Prints test 2
Threads merged.
We'd like to help you, but you keep jumping around. Let's start with the coroutine tutorial Bubba wrote, linked earlier in the thread. What, specifically, did you not understand in it? Feel free to ask a lot of questions in this thread, but do please try to make them as specific as you can. Maybe start from the beginning of the tutorial and post questions here about things you aren't grasping until we've gotten you through the whole tutorial. We can't help you unless we know specifically what parts of the system you don't understand.
coroutine.resume(processes["desktop"])
? processes[yourProcess.name]=yourProcess
. coroutine.resume(prcoesses["test"],...)
re: my APIs, mentioned earlier in the thread (in one of the previous threads before the merge?) and in the current thread title… I actually had a version of multishell which used my buffer and coroutine APIs, which itself is an example of how to use my coroutine api, goroutines, to handle multiple programs running at once. It didn't have a taskbar at all, instead using the ctrlkeys api to switch on ctrl+1, ctrl+2, etc, but it sounded like you'd figured that part out? It also included a pair of shell programs that, when run under my version of multishell, would let you launch programs in the foreground (so you could switch to them) and in the background (which effectively redirected them to nil, so you could NOT switch to them, or see their output, they just ran in the background) The multishell program is linked from the end of the first post in my APIs thread, and there's a separate thread in programs for my multishell, which I think is also linked in that first post, think you'd have to go to that thread to find the fg and bg companion programs.
Having said that, my multishell and goroutines api should probably be used as a reference rather than a foundation.The gorouitnes api was never tested very thoroughly, and even if people start testing and reporting issues now, I'm not likely to be working on fixing it up any time soon - it's an abandoned 3-year-old project, and not something I have even tried to use myself in years. Multishell is just entirely dependant on goroutines, so not recommended on that basis. I did use it myself for a while, and don't remember encountering serious issues with it.
Ahh okay thank you that was quite helpful, and will have a look, do you know of any good buffers that are already made?I think you should settle down a bit and try to accomplish what you are looking for step by step.
1. You can simply use the name of the coroutine as the index like so :.processes[yourProcess.name]=yourProcess
That way you can call your coroutine as following (assuming you called your routine "test" )coroutine.resume(prcoesses["test"],...)
3. As far as I understand it a buffer is simply a table(object) you can redirect the output to just as you would do with other redirect objects like monitors. This means you have to implement all functions a redirect object needs (like print, write, setCursorPos,etc…). Just search for it on the forums and you will surely find some examples.
4. You can´t kill /stop a coroutine while it is executing code. Your kill() function can only remove the coroutine from the process table as soon as it has yielded and hence make it unusable.
Regarding your other questions about buffers : Maybe try to read through the code of some other buffers here on the forum ;)/>
If you don´t know how to use an Api use the wiki!
local runningProcesses = {}
local runningProcesses = {}
local process = {
name = "desktop",
thread = coroutine.create(function() return shell.run("desktop") end),
buffer = buffer.new() --assuming a buffer API for now
}
table.insert(runningProcesses, process)
function process.getCount()
return #runningProcesses
end
--# or maybe this:
function process.getCount()
local names = {}
for _, proc in ipairs(runningProcesses) do
table.insert(names, proc.name)
end
return names
end
while true do
local event = {os.pullEventRaw()}
for _, proc in ipairs(runningProcesses) do
local _old = term.redirect(proc.buffer)
coroutine.resume(proc.thread, unpack(event))
term.redirect(_old)
proc.buffer.draw()
end
end
function process.kill(name)
for i = 1, #runningProcesses do
if runningProcesses[i].name == name then
table.remove(runningProcesses, i)
return true
end
end
return false
end