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

Key Event in Parallel API

Started by greenvbuser, 15 July 2015 - 06:14 PM
greenvbuser #1
Posted 15 July 2015 - 08:14 PM
Hello together.

I'm tying to implement a class with seperate render- and update cycle. Both cycles should run at the same time.
My approach was the parralel API using
parallel.waitForAny(Update(), Render())
This seems to work fine for the render cycle which will only output data to the screen. I can confirm that the render cycle is running.
I used a frame counter which displays at the bottom of the screen to check if the rendering takes place.
The update cycle which processes the data changes and the keyboard input, however, doesn't work as expected. Basically the update cycle looks like this:

function Update()

  while true do

	local event, key = os.pullEvent("key");
  
	if key == keys.down then
	  object:setSelectedItem(object:getSelectedItem() + 1)
	end
	if key == keys.up then
	  object:setSelectedItem(object:getSelectedItem() - 1)
	end
	if key == keys.enter then
	  break
	end

  end

end
The limits for the selection are handled by the setSelectedItem()
function itself. This shouldn't be an issue.

The Problem is, that the application doesn't respond to my key events. Any idea why not?
I appreciate any help…

I've read something about blocking in the Wiki. But since I run both cycles with the parallel API this should work, right? I mean.. that's what the API is for, isn't it? Running one task independently while another thread sleeps or waits?
Edited on 15 July 2015 - 06:24 PM
HPWebcamAble #2
Posted 15 July 2015 - 08:22 PM
parallel.waitForAny(Update(), Render())

That's a common problem right there

You pass the FUNCTION, not its result

So this would be correct:

parallel.waitForAny( Update , Render )
greenvbuser #3
Posted 15 July 2015 - 08:27 PM
parallel.waitForAny(Update(), Render())

That's a common problem right there

You pass the FUNCTION, not its result

So this would be correct:

parallel.waitForAny( Update , Render )

Genius :D/>
Thank you very much :)/>