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

Help with os.pullEvent()

Started by DaKillerBear1, 01 June 2015 - 08:46 AM
DaKillerBear1 #1
Posted 01 June 2015 - 10:46 AM
Well you see i'm making a game in Lua and to sense button pushes i'm using os.pullEvent("key"). But my big problem is that os.pullEvent() pauses the program. I was wondering if there is any way i can set a time limit to how long os.pullEvent() will wait? It would be a big help! But if there is any other way to take input ( that may be better) i will accept that too :)/>

A copy of my movement code:


function movement() -- This block handles movement
  a, b = os.pullEvent("key")
  if b == 30 then
	p1x = p1x - 1

  elseif b == 32 then
	p1x = p1x + 1

  elseif b == 17 then
	p1y = p1y - 1

  elseif b == 31 then
	p1y = p1y + 1

  else
  end

  if b == 203 then
	p2x = p2x - 1

  elseif b == 205 then
	p2x = p2x + 1

  elseif b == 200 then
	p2y = p2y - 1

  elseif b == 208 then
	p2y = p2y + 1

  else
  end
end
Bomb Bloke #2
Posted 01 June 2015 - 11:37 AM
Yes, see the examples for os.startTimer().
Creator #3
Posted 01 June 2015 - 02:35 PM
Use something like this:


while true do
starttimer(0.5)
event = pullevent()
if the event is timer continue the game elseif it is
a button handle the movement else ignore.
end

Very pseudo code.
Bomb Bloke #4
Posted 01 June 2015 - 02:50 PM
… bearing in mind that you wouldn't want to start a new timer every time the loop repeats.
Creator #5
Posted 01 June 2015 - 04:09 PM
Yeah, you have to cancle it at some time, sorry I forgot.
flaghacker #6
Posted 01 June 2015 - 04:24 PM
Yeah, you have to cancle it at some time, sorry I forgot.

No, you only start a new one when the previous one fired:

startTimer

while true do
  pullEvent
  if event == timer then
    startTimer
    --code
  elseif event == ...
    --code
  end
end
DaKillerBear1 #7
Posted 01 June 2015 - 06:33 PM
Thanks for the help guys! It really helped, I managed to intergrate the timer thing and guess what? My game works wonderfully :)/> By the way, Here's the fineshed product:

function movement() -- This block handles movement
 
  timer = os.startTimer(0.1)
 
  while true do 
    a, b = os.pullEvent()
 
    if a == "timer" and b == timer then
	  break
    end
   
    if a == "key" and b == 30 then
	  p1x = p1x - 1
 
    elseif a == "key" and b == 32 then
	  p1x = p1x + 1
 
    elseif a == "key" and b == 17 then
	  p1y = p1y - 1
 
    elseif a == "key" and b == 31 then
	  p1y = p1y + 1
 
    else
    end
 
    if a == "key" and b == 203 then
	  p2x = p2x - 1
 
    elseif a == "key" and b == 205 then
	  p2x = p2x + 1
 
    elseif a == "key" and b == 200 then
	  p2y = p2y - 1
 
    elseif a == "key" and b == 208 then
	  p2y = p2y + 1
 
    else
    end
  end
end