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

Looping problem!

Started by LeDark Lua, 13 June 2015 - 03:37 PM
LeDark Lua #1
Posted 13 June 2015 - 05:37 PM
Ok my problem is this:

  while true do
	ev, btn, x, y = os.pullEvent()
  end
I want to loop forever and dont wait for the button to press or mouse to do something!

I could use this:

  while true do
    --Code here
    os.sleep(0.1)
  end
But this will not look for buttons…
Edited on 13 June 2015 - 03:40 PM
KingofGamesYami #2
Posted 13 June 2015 - 05:46 PM
You can't loop forever without yielding. CC will automatically either error your code or shut off the computer.
LeDark Lua #3
Posted 13 June 2015 - 05:51 PM
So thats why I use os.sleep(). I want to do like, if im not pressing anything I need to sleep otherwise read the button or key.
Dustmuz #4
Posted 13 June 2015 - 06:03 PM


local function button()
  while true do
	local event = {t:handleEvents()}
	if event[1] == "button_click" then
	  t:toggleButton(event[2])
	end
  end
end

this is how i handle what you are looking for :)/>
and then calling the function like this

should be said im using a custom touchapi
to handle my buttons

while true do
   parallel.waitForany(button, #rest of my programs here)
end
Edited on 13 June 2015 - 04:04 PM
LeDark Lua #5
Posted 13 June 2015 - 06:09 PM
Dustmuz what is 't' ?
Edited on 13 June 2015 - 04:14 PM
Yevano #6
Posted 13 June 2015 - 06:16 PM
It sounds like you're wanting to run something else on a timed interval while also capturing your input events. To do this, you could just run a timer using os.startTimer. However, it depends on what you actually want to do when you're not getting input. Are you trying to update the screen, get input from a peripheral, etc.?


local int = 0.05 -- update every 1/20 of a second (every game tick)
local tid = os.startTimer(int)
while true do
    local e, p1, p2 = os.pullEvent()
    if e == "timer" and tid == p1 then
        tid = os.startTimer(int)
        update() -- call whatever update routine you need to do
    else
        -- Process input
    end
end
LeDark Lua #7
Posted 13 June 2015 - 06:20 PM
Thanks Yevano!
TheOddByte #8
Posted 13 June 2015 - 06:21 PM
If you're trying to handle events in that loop, and don't want it to constantly wait for a keypress or something you could use a timer to update every second or so.
Example



local function redraw( text )
	text = text or ""
	term.clear()
	term.setCursorPos( 1, 1 )
	print( "Time: " .. textutils.formatTime( os.time(), true ) .. "\n" )
	write( "> " .. text )
end


local text			= ""
local refreshRate = 1
local timer = os.startTimer( refreshRate ) --# Start the timer for the first time
while true do
	redraw( text )
	local e = { os.pullEvent() }
	if e[1] == "timer" and e[2] == timer then --# The "timer" event was pulled
		timer = os.startTimer( refreshRate ) --# Now we want to restart the timer
	end

	if e[1] == "key" then
		if e[2] == 14 then
			text = text:sub( 1, #text - 1 )
		end

	elseif e[1] == "char" then
		text = text .. e[2]
	end
end

Edit: Ninja'd <_</>
Edited on 13 June 2015 - 04:22 PM
LeDark Lua #9
Posted 13 June 2015 - 06:35 PM
Don't worry TheOddByte your code is helpfull too!