Jump to content




Controllable turtle help?


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

#1 Zukamimozu

  • Members
  • 26 posts

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 by Zukamimozu, 20 November 2012 - 01:04 PM.


#2 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

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.

#3 etopsirhc

  • Members
  • 122 posts

Posted 19 November 2012 - 10:13 PM

also
 label set <labelName> 


#4 Zukamimozu

  • Members
  • 26 posts

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


#5 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

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


#6 Kingdaro

    The Doctor

  • Members
  • 1,636 posts
  • Location'MURICA

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.

#7 Zukamimozu

  • Members
  • 26 posts

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.

#8 ChunLing

  • Members
  • 2,027 posts

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.

#9 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

Posted 21 November 2012 - 02:17 AM

View PostZukamimozu, on 20 November 2012 - 05:00 PM, said:

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.

#10 Zukamimozu

  • Members
  • 26 posts

Posted 21 November 2012 - 12:13 PM

I feel dumb... I forgot to put fuel in it lol sorry...

#11 ChunLing

  • Members
  • 2,027 posts

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) :(/>





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users