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

All key presses seem to do the same thing in this code now

Started by CCJJSax, 29 March 2013 - 12:45 PM
CCJJSax #1
Posted 29 March 2013 - 01:45 PM
This is a function to make the turtle select a slot using the arrows / wasd keys. At one point I had every button as a separate elseif, earlier today I made all the buttons that did the same thing into the same elseif. Now, with this code, every button (even the ones that aren't defined) make the slot move up one….

the elseif to or change I did was with the last one, and I got that to work. but after I moved the rest to that system, everything did the same thing. it's probably pretty obvious what is the problem, but, let's just say I need some sleep lol.

(i don't know how to do spoiler so the following spoiler attempt is a guess :)/> )

Spoilerinfoget and lastbutton you can think of as print()
movementdisplay() just shows a display, they shouldn't really effect my problem.


local function slotselection()
infoget = "Select a slot with arrows"
movementdisplay()
x = 1
turtle.select(x)
while true do
  event, pl = os.pullEvent("key")
  if pl == 200 or 17 then
   x=x-4
   if x<1 then
    x=x+16
   end
   lastbutton = "am selecting slot "..x
   movementdisplay()
   turtle.select(x)
  elseif pl == 203 or 30 then
   x=x-1
   if x<1 then
    x=x+16
   end
   lastbutton = "am selecting slot "..x
   movementdisplay()
   turtle.select(x)
  elseif pl == 208 or 31 then
   x=x+4
   if x>16 then
    x=x-16
   end
   lastbutton = "am selecting slot "..x
   movementdisplay()
   turtle.select(x)
  elseif pl == 205 or 32 then
   x=x+1
   if x>16 then
    x=x-16
   end
   lastbutton = "am selecting slot "..x
   movementdisplay()
   turtle.select(x)
  elseif pl == 28 or 13 then
   lastbutton = "selected slot "..x
   movementdisplay()
   sleep(.01)
   shell.dir()
   break
  end
end
end

Thanks! :D/>
Kingdaro #2
Posted 29 March 2013 - 02:00 PM
Hopefully this works:


local slot = 1
turtle.select(1)

while true do
  local _, k = os.pullEvent('key')
  if k == keys.left and slot > 1 then
    slot = slot - 1
  elseif k == keys.right and slot < 16 then
    slot = slot + 1
  elseif k == keys.up and slot > 4 then
    slot = slot - 4
  elseif k == keys.down and slot < 13 then
    slot = slot + 4
  end
  turtle.select(slot)
end
CCJJSax #3
Posted 29 March 2013 - 02:55 PM
Hopefully this works:


local slot = 1
turtle.select(1)

while true do
  local _, k = os.pullEvent('key')
  if k == keys.left and slot > 1 then
	slot = slot - 1
  elseif k == keys.right and slot < 16 then
	slot = slot + 1
  elseif k == keys.up and slot > 4 then
	slot = slot - 4
  elseif k == keys.down and slot < 13 then
	slot = slot + 4
  end
  turtle.select(slot)
end

that didn't work with wasd, but without having worked with it other than that code, I'd say that's an easy fix, plus it's much smaller than my code :D/>
immibis #4
Posted 29 March 2013 - 07:08 PM

if pl == 200 or 17 then
is the same as:

if ((pl == 200) or (17)) then

This runs the code inside the if if pl == 200 is true, or 17 is true. 17 is not false or nil, so it's always true.