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

Parallel API, read functions with parameters?

Started by bbaayyeerr, 05 July 2014 - 11:39 AM
bbaayyeerr #1
Posted 05 July 2014 - 01:39 PM
Hello, once again!
I'm currently trying to make desktop on my computer, and I like the time to constantly update at the same time as I do other things on the computer. It has come to my mind that the Parallel API could help me with that. But if I understand it correctly parallel can't handle functions with parameters ex. "printTime(x,y)", or as the matter of facts it can but it will just run the function with parameters not the other one. (Depending on with function declared first). So my question is: is there any way to make it read parameters and still run both functions, without interacting with the "parallel API" code.

This is what i want to do:

function printTime(x,y)
while true do
  term.setTextColor(colors.lime)
  term.setCursorPos(x,y)
  print(textutils.formatTime(os.time(), true))
  sleep(0)
end
end

As you can see "x" and "y" are used to set the time's place on the screen. If I make a different GUI on the "next" page I may want to change the placing of the displayed time. That's also why i made this particular function. But now lets say I run the parallel API:

parallel.waitForAny(DeskTop,printTime(7,2))
This wont work, but this does: (If you change the "printTime" function a bit, replace "x" and "y" with values)

parallel.waitForAny(DeskTop,printTime)

I also attached the "Desktop" function, if anyone find it useful:
Spoiler

function DeskTop()
function simpleMenu()
  running = true
  while running do
term.setTextColor(colors.lightBlue)
   if selectedItem == 1 then
term.setCursorPos(3,4)
  print("[  START:  ]")
term.setCursorPos(19,4)
  print("   FUNCTIONS:   ")
   elseif selectedItem == 2 then
term.setCursorPos(3,4)
  print("   START:   ")
term.setCursorPos(19,4)
  print("[  FUNCTIONS:  ]")
   end
	event, key = os.pullEvent("key")
   if key == keys.down then
	 running = false
if selectedItem == 1 then
  menu1()
elseif selectedItem == 2 then
  menu2()
end
   elseif key == keys.left then
if selectedItem == 2 then
  selectedItem = selectedItem - 1
end
   elseif key == keys.right then
if selectedItem == 1 then
  selectedItem = selectedItem + 1
end
   end
  end
end
function menu1()
  local x = 5
  running2 = true
  while running2 do
   if x == 4 then
y = x + 1
term.setCursorPos(3,y)
print("  ")
simpleMenu()
   end
	term.setTextColor(colors.orange)
	if x == 5 then
  term.setCursorPos(3,x)
   print("->")
	elseif x > 4 then
  y = x - 1
  term.setCursorPos(3,x)
   print("->")
  term.setCursorPos(3,y)
   print("  ")
	end
	if x < 8 then
   y = x + 1
  term.setCursorPos(3,x)
   print("->")
  term.setCursorPos(3,y)
   print("  ")
	end
	 event, key = os.pullEvent("key")
	if key == keys.up then
  if x > 4 then
   x = x - 1
  end
	elseif key == keys.down then
  if x < 8 then
   x = x + 1
  end
	elseif key == keys.enter then
  if x == 5 then
   os.shutdown()
  elseif x == 6 then
   os.reboot()
  elseif x == 7 then
   clear()
   running2 = false
   parallel.waitForAny(DeskTop,printTime)
  elseif x == 8 then
   clear()
   running2 = false
  end
	end -- END IF-STATEMENT
  end -- END LOOP
end -- END FUNCTION menu1
function menu2()
  local x = 5
  running2 = true
  while running2 do
   if x == 4 then
  y = x + 1
  term.setCursorPos(19,y)
  print("  ")
  simpleMenu()
   end
	term.setTextColor(colors.orange)
   if x == 5 then
  term.setCursorPos(19,x)
  print("->")
   elseif x > 4 and x <= 9 then
  y = x - 1
  term.setCursorPos(19,x)
  print("->")
  term.setCursorPos(19,y)
  print("  ")
   end
   if x < 9 and x >= 4 then
  y = x + 1
  term.setCursorPos(19,x)
  print("->")
  term.setCursorPos(19,y)
  print("  ")
   end
	event, key = os.pullEvent("key")
   if key == keys.up then
  if x > 4 then
  x = x - 1
  end
   elseif key == keys.down then
  if x < 9 and x >= 4 then
  x = x + 1
  end
   elseif key == keys.enter then
  if x == 5 then
	running2 = false
  ControllPanel()
  elseif x == 6 then
  Lock()
  elseif x == 7 then
  Mail()
  elseif x == 8 then

  elseif X == 9 then

  end
   end -- END IF-STATEMENT
  end -- END LOOP
end -- END FUNCTION menu2
-- RUN ORDER --
clear()
BigBox()
simpleMenu()
end --[[ END DESKTOP ]]--

Suggestions anyone? ;)/>
Edited on 05 July 2014 - 12:09 PM
Lignum #2
Posted 05 July 2014 - 02:46 PM
You can do this:

parallel.waitForAny(Desktop, function()
     printTime(7, 2)
end)

But I don't recommend using parallel here in the first place.
Don't use sleep but instead use os.pullEvent. By not passing any arguments to it, it will pull anything. Now you can start a timer of one second duration and have it pull a timer event. Every time the event is received, update your clock.
Bomb Bloke #3
Posted 05 July 2014 - 03:47 PM
That is to say, allow the event pullers in the "Desktop" function to retrieve any event, use "if" statements to see if your timer went off, and if so then call the printTime function. That time-printing function would have its sleep and loop removed.

This all falls apart if you start using other functions that pull events (read() being the most likely example), as you'd need to re-write those to co-operate.
bbaayyeerr #4
Posted 07 July 2014 - 08:50 AM
Hello! Sorry for a late reply! Computer craft forums has been down… Well I'm not sure that I quite follow!? What exactly should I do? ;)/> I would consider my self as a noob, but I didn't understand that! ;)/>
Bomb Bloke #5
Posted 07 July 2014 - 12:39 PM
Say you've got a basic key event listener that looks like this:

while true do
	event, key = os.pullEvent("key")
	if key == keys.down then
		...
	elseif key == keys.left then
		...
	elseif key == keys.right then
		...
	end
end

You want to run that while doing other things. So what you do is, you turn it into a generic event listener, and start throwing timers into the mix:

myTimer = os.startTimer(1)  -- Rig a timer to throw an event one second from now.

while true do
	event, par1 = os.pullEvent()
	if event == "key" then
		if par1 == keys.down then
			...
		elseif par1 == keys.left then
			...
		elseif par1 == keys.right then
			...
		end
	elseif event == "timer" and par1 == myTimer then  -- A second has passed!
		printTime(7,2)
		myTimer = os.startTimer(1)  -- Prepare another timer.
	end
end

You'd need to take the sleep out of the printTime function, but you can hopefully see how this sort of structure allows you to deal with all kinds of different events within the one loop.
Edited on 07 July 2014 - 10:40 AM
bbaayyeerr #6
Posted 07 July 2014 - 02:18 PM
Wow that's really clever, thank you! ;)/>