Jump to content




Looping problem!


  • You cannot reply to this topic
8 replies to this topic

#1 LeDark Lua

  • Members
  • 369 posts
  • LocationLeLua

Posted 13 June 2015 - 04: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 by lauriszz123, 13 June 2015 - 04:40 PM.


#2 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 13 June 2015 - 04:46 PM

You can't loop forever without yielding. CC will automatically either error your code or shut off the computer.

#3 LeDark Lua

  • Members
  • 369 posts
  • LocationLeLua

Posted 13 June 2015 - 04: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.

#4 Dustmuz

  • Members
  • 174 posts
  • LocationDenmark

Posted 13 June 2015 - 05: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 by Dustmuz, 13 June 2015 - 05:04 PM.


#5 LeDark Lua

  • Members
  • 369 posts
  • LocationLeLua

Posted 13 June 2015 - 05:09 PM

Dustmuz what is 't' ?

Edited by lauriszz123, 13 June 2015 - 05:14 PM.


#6 Yevano

  • Members
  • 376 posts
  • LocationUSA

Posted 13 June 2015 - 05: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


#7 LeDark Lua

  • Members
  • 369 posts
  • LocationLeLua

Posted 13 June 2015 - 05:20 PM

Thanks Yevano!

#8 TheOddByte

    Lazy Coder

  • Members
  • 1,607 posts
  • LocationSweden

Posted 13 June 2015 - 05: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

Edit: Ninja'd <_<

Edited by TheOddByte, 13 June 2015 - 05:22 PM.


#9 LeDark Lua

  • Members
  • 369 posts
  • LocationLeLua

Posted 13 June 2015 - 05:35 PM

Don't worry TheOddByte your code is helpfull too!





2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users