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

parallel.watForAny problem

Started by danielsv03, 18 June 2017 - 06:54 PM
danielsv03 #1
Posted 18 June 2017 - 08:54 PM
Hello so i'm making a console for practice as for a BIGGER project i want it to run multiple functions at the same time with parallel.waitForAny api but the thing is that is just glitches it seems to pause the functions before they have finished why?
i want them to pause the function where it says
--DO SOMETHING HERE TO GO BACK TO BLA BLA BLA
then continue the function when it gets back to it

here is the code
and a brief explanation to what it should do
so the display function will always look if an variable like his changes if so then print the suggested variable and the prompt will take in user commands and send it to the display so the display show it and the last thing is the handler with checks if it founds a peripheral attached to it if so send that to the display


code


local w,h = term.getSize()

uis = true
ui = "Startup"
his = false
hi = "none"
comm = none
drive = false
drivepos = "none"
function setup()
  term.setBackgroundColor(colors.lightGray)
  print("cleaning")
  term.setCursorPos(1, 1)
  term.clear()
  gui()
end

function gui()
paintutils.drawFilledBox(1, 1, 90, 2, colors.orange)
term.setCursorPos(math.floor(w-string.len("Console OS 2.1"))/2, 2)
term.setTextColor(colors.white)
print("Console OS 2.1")
end

function display()
buf = window.create(term.current(),1,3,51,17)
buf.setBackgroundColor(colors.lightGray)
oldterm = term.redirect(buf)
term.clear()
while true do
sleep(0.5)
print("Ye")
if uis == true then uis = false
  print("User: "..ui)

  --coroutine.yield()
  --HERE DO SOMETHING TO GO BACK TO PROMPT OR HANDLER
elseif his == true then his = false
  print(hi)
  --coroutine.yield()
  --HERE DO SOMETHING TO GO BACK TO PROMPT OR HANDLER
else
  --coroutine.yield()
  --HERE DO SOMETHING TO GO BACK TO PROMPT OR HANDLER
end
end

end
function prompt()
--sleep(0.5)
buff = window.create(term.current(),1,17,51,1)
buff.setBackgroundColor(colors.gray)
oldterm = term.redirect(buff)
while true do
term.setCursorPos(1, 1)
term.clear()
write(">> ")
local com = read()
ui = com
--coroutine.yield()
--HERE DO SOMETHING TO GO BACK TO DISPLAY OR HANDLER
end
end
function handler()
while true do
sleep(0.5)
left = peripheral.isPresent("left")
right = peripheral.isPresent("right")
bottom = peripheral.isPresent("bottom")
top = peripheral.isPresent("top")
front = peripheral.isPresent("front")
back = peripheral.isPresent("back")
if left == true then
  his = true
		drivepos = "left"
		drive = true
elseif right == true then
  his = true
		drivepos = "right"
		drive = true
		hi = ("Detected drive "..drivepos)
else
		drive = false
end
--HERE DO SOMETHING TO GO BACK TO PROMPT OR DISPLAY
end
end


setup()
parallel.waitForAny(display, prompt, handler)
--display()
--prompt()



Any help is useful
Edited on 18 June 2017 - 06:55 PM
KingofGamesYami #2
Posted 18 June 2017 - 08:58 PM
Thats… not how parallel works, nor is it the way to accomplish what you want. You dont want to run functions simultaneously, especially when said functions interact with the user.
danielsv03 #3
Posted 18 June 2017 - 09:02 PM
Thats… not how parallel works, nor is it the way to accomplish what you want. You dont want to run functions simultaneously, especially when said functions interact with the user.


Well what would a good way to do this?
Bomb Bloke #4
Posted 19 June 2017 - 03:05 AM
A little formatting would make that much easier to read, but glancing over it, the main problem I see is that you're expecting the different functions to render to different windows. ComputerCraft won't "remember" which window each function last redirected to - it's up to you to ensure that the correct window is selected at the time each coroutine wishes to start rendering.
danielsv03 #5
Posted 19 June 2017 - 10:04 AM
A little formatting would make that much easier to read, but glancing over it, the main problem I see is that you're expecting the different functions to render to different windows. ComputerCraft won't "remember" which window each function last redirected to - it's up to you to ensure that the correct window is selected at the time each coroutine wishes to start rendering.


ohh so i will just have to redirect them at the correct time
Lupus590 #6
Posted 19 June 2017 - 11:05 AM
You may want to look at how multishell handles it's window switching
danielsv03 #7
Posted 19 June 2017 - 03:04 PM
You may want to look at how multishell handles it's window switching
Where can i find that?
danielsv03 #8
Posted 19 June 2017 - 03:18 PM
Okay guys so this is what i have done so far but still it prints them in the prompt window how should i fix that? if you remove prompt from the parallel.wairForAny
then it prints like it should be doing but the the prompt of course is gone so do an know why?
code

local w,h = term.getSize()

uis = true
ui = "Startup"
his = false
hi = "none"
comm = none
drive = false
drivepos = "none"
function setup()
  term.setBackgroundColor(colors.lightGray)
  print("cleaning")
  term.setCursorPos(1, 1)
  term.clear()
  gui()
end

function gui()
paintutils.drawFilledBox(1, 1, 90, 2, colors.orange)
term.setCursorPos(math.floor(w-string.len("Console OS 2.1"))/2, 2)
term.setTextColor(colors.white)
print("Console OS 2.1")
end

function display()
buf = window.create(term.current(),1,3,51,17)
buf.setBackgroundColor(colors.lightGray)
oldterm = term.redirect(buf)
term.clear()
--print("ye")
while true do

sleep(0.5)
--oldterm = term.redirect(buf)
--print("Ye")
if uis == true then uis = false
  print("User: "..ui)

  --coroutine.yield()
  --HERE DO SOMETHING TO GO BACK TO PROMPT OR HANDLER
  --print("Boi")
  term.redirect(oldterm)
elseif his == true then his = false
  print(hi)
  --coroutine.yield()
  --HERE DO SOMETHING TO GO BACK TO PROMPT OR HANDLER
  --print("bois")
  term.redirect(oldterm)
else
  --coroutine.yield()
  --HERE DO SOMETHING TO GO BACK TO PROMPT OR HANDLER
  --print("bo")
  term.redirect(oldterm)
end
end

end
function prompt()
--sleep(0.5)
buff = window.create(term.current(),1,17,51,1)
buff.setBackgroundColor(colors.gray)
while true do
oldtermm = term.redirect(buff)
term.setCursorPos(1, 1)
term.clear()
write(">> ")
--term.redirect(oldterm)
local com = read()
ui = com
uis = true
--coroutine.yield()
--HERE DO SOMETHING TO GO BACK TO DISPLAY OR HANDLER
oldterm = term.redirect(buf)
end
end
function handler()
while true do
sleep(0.5)
left = peripheral.isPresent("left")
right = peripheral.isPresent("right")
bottom = peripheral.isPresent("bottom")
top = peripheral.isPresent("top")
front = peripheral.isPresent("front")
back = peripheral.isPresent("back")
if left == true then
  his = true
	    drivepos = "left"
	    drive = true
	    hi = ("Detected drive "..drivepos)
	   oldterm = term.redirect(buf)
elseif right == true then
  his = true
	    drivepos = "right"
	    drive = true
	    hi = ("Detected drive "..drivepos)
	    oldterm = term.redirect(buf)
else
	    drive = false
end
--HERE DO SOMETHING TO GO BACK TO PROMPT OR DISPLAY
end
end


setup()
parallel.waitForAny(display, prompt, handler)
--display()
--prompt()
Bomb Bloke #9
Posted 19 June 2017 - 04:55 PM
You may want to look at how multishell handles it's window switching

Where can i find that?

Here, though I suspect that the chat script would be a much more suitable example for what you're trying to do here.

Okay guys so this is what i have done so far but still it prints them in the prompt window how should i fix that? if you remove prompt from the parallel.wairForAny
then it prints like it should be doing but the the prompt of course is gone so do an know why?

Because your "prompt" function redirects to "buff" every time it is resumed, whereas your "display" function only redirects to "buf" once - when it's very first started. Once the terminal has been redirected away again, it never comes back.

If you're not sure what causes your functions to yield and resume, then you're missing important fundamentals, and I recommend reading through all of this lot.

But speaking of buf / buff, localising your variables would mean you wouldn't need to use unique names for everything all throughout the script.
danielsv03 #10
Posted 19 June 2017 - 06:23 PM
Okay thanks i will read it now
Edited on 19 June 2017 - 04:23 PM