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

Api help

Started by ComputerCraftFan11, 07 March 2012 - 04:57 AM
ComputerCraftFan11 #1
Posted 07 March 2012 - 05:57 AM
I made a api and at my force function, i get a error at all turtle.*'s in my API

Force:

function force(direction)
if direction == forward then
  if(turtle.detect())then
   turtle.dig()
   turtle.forward()
  else
   turtle.forward()
  end
end
if direction == up then
  if(turtle.detectUp())then
   turtle.digUp()
   turtle.up()
  else
   turtle.up()
  end
end
if direction == down then
  if(turtle.detectDown())then
   turtle.digDown()
   turtle.down()
  else
   turtle.down()
  end
end
if direction == left then
  turtle.turnLeft()
  if(turtle.detect())then
   turtle.dig()
   turtle.forward()
  else
   turtle.forward()
  end
end
if direction == right then
  turtle.turnRight()
  if(turtle.detect())then
   turtle.dig()
   turtle.forward()
  else
   turtle.forward()
  end
end
end

Program:
tortue.force(forward)
Advert #2
Posted 07 March 2012 - 06:02 AM
I made a api and at my force function, i get a error at all turtle.*'s in my API

Force:

function force(direction)
if direction == forward then
  if(turtle.detect())then
   turtle.dig()
   turtle.forward()
  else
   turtle.forward()
  end
end
if direction == up then
  if(turtle.detectUp())then
   turtle.digUp()
   turtle.up()
  else
   turtle.up()
  end
end
if direction == down then
  if(turtle.detectDown())then
   turtle.digDown()
   turtle.down()
  else
   turtle.down()
  end
end
if direction == left then
  turtle.turnLeft()
  if(turtle.detect())then
   turtle.dig()
   turtle.forward()
  else
   turtle.forward()
  end
end
if direction == right then
  turtle.turnRight()
  if(turtle.detect())then
   turtle.dig()
   turtle.forward()
  else
   turtle.forward()
  end
end
end

Program:
tortue.force(forward)

Unless forward, left, down, etc is defined, you'll probably want them as a string: "forward", "left", etc.
ComputerCraftFan11 #3
Posted 07 March 2012 - 06:28 AM
I made a api and at my force function, i get a error at all turtle.*'s in my API

Force:

function force(direction)
if direction == forward then
  if(turtle.detect())then
   turtle.dig()
   turtle.forward()
  else
   turtle.forward()
  end
end
if direction == up then
  if(turtle.detectUp())then
   turtle.digUp()
   turtle.up()
  else
   turtle.up()
  end
end
if direction == down then
  if(turtle.detectDown())then
   turtle.digDown()
   turtle.down()
  else
   turtle.down()
  end
end
if direction == left then
  turtle.turnLeft()
  if(turtle.detect())then
   turtle.dig()
   turtle.forward()
  else
   turtle.forward()
  end
end
if direction == right then
  turtle.turnRight()
  if(turtle.detect())then
   turtle.dig()
   turtle.forward()
  else
   turtle.forward()
  end
end
end

Program:
tortue.force(forward)

Unless forward, left, down, etc is defined, you'll probably want them as a string: "forward", "left", etc.

I changed it to:


function dig()
turtle.dig()
end
function forward()
turtle.forward()
end
function up()
turtle.up()
end
function down()
turtle.down()
end
function turnLeft()
turtle.turnLeft()
end
function turnRight()
turtle.turnRight()
end

function force(direction)
if direction == forward then
  if(turtle.detect())then
   dig()
   forward()
  else
   forward()
  end
end
if direction == up then
  if(turtle.detectUp())then
   digUp()
   up()
  else
   up()
  end
end
if direction == down then
  if(turtle.detectDown())then
   digDown()
   down()
  else
   down()
  end
end
if direction == left then
  turnLeft()
  if(turtle.detect())then
   dig()
   forward()
  else
   forward()
  end
end
if direction == right then
  turnRight()
  if(turtle.detect())then
   dig()
   forward()
  else
   forward()
  end
end
end
But now everytime i say something like:

force(forward)
or any other direction, it turns left and goes straight
Advert #4
Posted 07 March 2012 - 06:42 AM
I made a api and at my force function, i get a error at all turtle.*'s in my API

Force:

function force(direction)
if direction == forward then
  if(turtle.detect())then
   turtle.dig()
   turtle.forward()
  else
   turtle.forward()
  end
end
if direction == up then
  if(turtle.detectUp())then
   turtle.digUp()
   turtle.up()
  else
   turtle.up()
  end
end
if direction == down then
  if(turtle.detectDown())then
   turtle.digDown()
   turtle.down()
  else
   turtle.down()
  end
end
if direction == left then
  turtle.turnLeft()
  if(turtle.detect())then
   turtle.dig()
   turtle.forward()
  else
   turtle.forward()
  end
end
if direction == right then
  turtle.turnRight()
  if(turtle.detect())then
   turtle.dig()
   turtle.forward()
  else
   turtle.forward()
  end
end
end

Program:
tortue.force(forward)

Unless forward, left, down, etc is defined, you'll probably want them as a string: "forward", "left", etc.

I changed it to:


function dig()
turtle.dig()
end
function forward()
turtle.forward()
end
function up()
turtle.up()
end
function down()
turtle.down()
end
function turnLeft()
turtle.turnLeft()
end
function turnRight()
turtle.turnRight()
end

function force(direction)
if direction == forward then
  if(turtle.detect())then
   dig()
   forward()
  else
   forward()
  end
end
if direction == up then
  if(turtle.detectUp())then
   digUp()
   up()
  else
   up()
  end
end
if direction == down then
  if(turtle.detectDown())then
   digDown()
   down()
  else
   down()
  end
end
if direction == left then
  turnLeft()
  if(turtle.detect())then
   dig()
   forward()
  else
   forward()
  end
end
if direction == right then
  turnRight()
  if(turtle.detect())then
   dig()
   forward()
  else
   forward()
  end
end
end
But now everytime i say something like:

force(forward)
or any other direction, it turns left and goes straight

Let me clarify for you:
When you type:

force(forward)
You are sending the value of the variable called forward to force(). Before, this was nil; and it'd do everything.

Now, you're sending the function forward to force. Which should work (except for the cases noted below), unless you're loading this as an API:
forward would be nil, but yourapi.forward would be the value you're checking against.

The following if statements will always pass (assuming you're loading this as an API):

if direction == right then -- right is nil
if direction == left then -- left is nil

You need to put quotes "str", apostrophes 'str', or doublebrackets [[str]] around strings in Lua.
ComputerCraftFan11 #5
Posted 07 March 2012 - 06:48 AM
I made a api and at my force function, i get a error at all turtle.*'s in my API

Force:

function force(direction)
if direction == forward then
  if(turtle.detect())then
   turtle.dig()
   turtle.forward()
  else
   turtle.forward()
  end
end
if direction == up then
  if(turtle.detectUp())then
   turtle.digUp()
   turtle.up()
  else
   turtle.up()
  end
end
if direction == down then
  if(turtle.detectDown())then
   turtle.digDown()
   turtle.down()
  else
   turtle.down()
  end
end
if direction == left then
  turtle.turnLeft()
  if(turtle.detect())then
   turtle.dig()
   turtle.forward()
  else
   turtle.forward()
  end
end
if direction == right then
  turtle.turnRight()
  if(turtle.detect())then
   turtle.dig()
   turtle.forward()
  else
   turtle.forward()
  end
end
end

Program:
tortue.force(forward)

Unless forward, left, down, etc is defined, you'll probably want them as a string: "forward", "left", etc.

I changed it to:


function dig()
turtle.dig()
end
function forward()
turtle.forward()
end
function up()
turtle.up()
end
function down()
turtle.down()
end
function turnLeft()
turtle.turnLeft()
end
function turnRight()
turtle.turnRight()
end

function force(direction)
if direction == forward then
  if(turtle.detect())then
   dig()
   forward()
  else
   forward()
  end
end
if direction == up then
  if(turtle.detectUp())then
   digUp()
   up()
  else
   up()
  end
end
if direction == down then
  if(turtle.detectDown())then
   digDown()
   down()
  else
   down()
  end
end
if direction == left then
  turnLeft()
  if(turtle.detect())then
   dig()
   forward()
  else
   forward()
  end
end
if direction == right then
  turnRight()
  if(turtle.detect())then
   dig()
   forward()
  else
   forward()
  end
end
end
But now everytime i say something like:

force(forward)
or any other direction, it turns left and goes straight

Let me clarify for you:
When you type:

force(forward)
You are sending the value of the variable called forward to force(). Before, this was nil; and it'd do everything.

Now, you're sending the function forward to force. Which should work (except for the cases noted below), unless you're loading this as an API:
forward would be nil, but yourapi.forward would be the value you're checking against.

The following if statements will always pass (assuming you're loading this as an API):

if direction == right then -- right is nil
if direction == left then -- left is nil

You need to put quotes "str", apostrophes 'str', or doublebrackets [[str]] around strings in Lua.
thanks, it worked