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

Controllable turtle help?

Started by Zukamimozu, 19 November 2012 - 03:59 PM
Zukamimozu #1
Posted 19 November 2012 - 04:59 PM
So I am trying to make a program where when you press " w " it goes forward " s " it goes back and " a " turns left and " d " turns right then the SpaceBar mines the block in front of it. I don't know what mistake I'm making but I can only move forward…

while true do
local event, param = os.pullEvent()
if event == "key" and param == 17 then
turtle.forward()
else
if event == "key" and param == 30 then
turtle.turnLeft()
else
if event == "key" and param == 31 then
turtle.back()
else
if event == "key" and param == 32 then
turtle.turnRight()
else
if event == "key" and param == 57 then
turtle.dig()
  end
end
Edited on 20 November 2012 - 12:04 PM
remiX #2
Posted 19 November 2012 - 06:13 PM
You didn't lose it, the program gets saved onto that special turtle. Go into world/computer/<id of that turtle> - The ID you probably didn't know so use the search function and search for it, I'm sure you can remember what it was called. And then show us what you have done so far.
etopsirhc #3
Posted 19 November 2012 - 10:13 PM
also
 label set <labelName> 
Zukamimozu #4
Posted 20 November 2012 - 11:38 AM
Oh thanks found it :(/>/>
This is what I had.

while true do
local event, param = os.pullEvent()
if event == "key" and param == 17 then
turtle.forward()
else
if event == "key" and param == 30 then
turtle.turnLeft()
else
if event == "key" and param == 31 then
turtle.back()
else
if event == "key" and param == 32 then
turtle.turnRight()
else
if event == "key" and param == 57 then
turtle.dig()
  end
end
Lyqyd #5
Posted 20 November 2012 - 02:05 PM
That code wouldn't have even run.

Try this:


while true do
    local event, param = os.pullEvent()
    if event == "char" and param == "w" then
        turtle.forward()
    elseif event == "char" and param == "a" then
        turtle.turnLeft()
    elseif event == "char" and param == "s" then
        turtle.back()
    elseif event == "char" and param == "d" then
        turtle.turnRight()
    elseif event == "key" and param == 57 then
        turtle.dig()
    end
end
Kingdaro #6
Posted 20 November 2012 - 02:11 PM
May I offer an alternate solution? You could also have a table of mapped controls to specified functions, instead of a list of elseifs.


local controls = {
  [keys.w] = turtle.forward;
  [keys.s] = turtle.back;
  [keys.a] = turtle.turnLeft;
  [keys.d] = turtle.turnRight;
  [keys.space] = turtle.dig;
}

while true do
  local _, k = os.pullEvent('key')
  if controls[k] then
    controls[k]()
  end
end

But for simplicity's sake, your solution does work better, Lyqyd.
Zukamimozu #7
Posted 20 November 2012 - 05:00 PM
For whatever reason when I press w a s or d nothing happens… but no errors appear when I run it.
ChunLing #8
Posted 20 November 2012 - 05:21 PM
Have you tried the fixed code posts? Does neither of them work?

The problem with your code can be illustrated by a bit of proper indenting

while true do
  local event, param = os.pullEvent()
  if event == "key" and param == 17 then
    turtle.forward()
  else
    if event == "key" and param == 30 then
      turtle.turnLeft()
    else
      if event == "key" and param == 31 then
        turtle.back()
      else
        if event == "key" and param == 32 then
          turtle.turnRight()
        else
          if event == "key" and param == 57 then
            turtle.dig()
          end
        end
How exactly you're getting this to run I don't know and don't really want to know.
remiX #9
Posted 21 November 2012 - 02:17 AM
For whatever reason when I press w a s or d nothing happens… but no errors appear when I run it.

Lyqyd and Kingdaro's code will work.
Zukamimozu #10
Posted 21 November 2012 - 12:13 PM
I feel dumb… I forgot to put fuel in it lol sorry…
ChunLing #11
Posted 22 November 2012 - 02:36 AM
:(/>/> and yet you could only move forward (which requires fuel) and not turn or dig (which don't require fuel) :(/>/>