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

[lua][Problem] My code doesn't work and i can't see why.

Started by jetbooster, 12 December 2012 - 01:55 PM
jetbooster #1
Posted 12 December 2012 - 02:55 PM

function Refuel()
  if turtle.getFuelLevel < 20 then
	shell.run("refuel",1)
  end
end
local tArgs={...}
if #tArgs == 1 then
  if tArgs[1] == "base" then
	xT = -24 yT = 64 zT = -117
  else
	print("unknown location")
  end
elseif #tArgs == 3 then
  local XT=tArgs[1]
  local xT=tonumber(XT)
  local YT=tArgs[2]
  local yT=tonumber(YT)
  local ZT=tArgs[3]
  local zT=tonumber(ZT)
  print(XT,",",YT,",",ZT)
  print("XT:"..type(XT))
  print("xT:"..","..xT..type(xT))
  print("xc:"..xc..type(xc))
else
  print("Incorrect Usage")
  print("Use goto base or goto <x> <y> <z>")
  print("use central computer for more complex goto's")
end
print("target:",xT,",",yT,",",zT)
local xShift = {0,-1,0,1}
local zShift = {1,0,-1,0}
local function checkDir()
  facing = facing + 1
  look = facing
  facing = facing -1
end
local function forward()
  while turtle.forward() == false do
	turtle.dig()
  end
  xc = xc+xShift[look]
  zc = zc+zShift[look]
end
local function left()
  turtle.turnLeft()
  facing = facing - 1
  if facing == -1 then facing = 3 end
  checkDir()
end
local function right()
  turtle.turnRight()
  facing = facing + 1
  if facing == 4 then facing = 0 end
  checkDir()
end
function up()
  while turtle.up() == false do
	turtle.digUp()
  end
  yc = yc +1
end
local function down()
  while turtle.down() == false do
	turtle.digDown()
  end
  yc = yc - 1
end
-----------------------------------------------------------
if xc==-24 and yc==60 and zc==-117 then
  rednet.send(5,"release")
  sleep(3)
end
for i=0, 12 do
up()
end
if xT<xc then
  repeat
	left()
  until facing == 1
  while xT<xc do
	forward()
  end
elseif xT>xc then
  repeat
	left()
  until facing == 3
  while xT>xc do
	forward()
  end
end
if zT<zc then
  repeat
	left()
  until facing == 2
  while zT<zc do
	forward()
  end
elseif zT>zc then
  repeat
	left()
  until facing == 0
  while zT>zc do
	forward()
  end
end
while yT>yc do
up()
end
while yT<yc do
down()
end
while facing ~= 3 do
left()
end
if tArgs[1] == "base" then
  rednet.send(5,"release")
  sleep(3)
  for i=1,4 do
	down()
  end
end
if xc==-24 and yc==64 and zc==-117 then
  rednet.send(5,"release")
  sleep(3)
  for i=1,4 do
	down()
  end
end
problem is:
if i run this program from the turtle, specifically with "goto base" it runs absolutely fine.
however when i send the same command over rednet, (i remembered to tostring() and then undo), even though checking with print(type()) xT and xc are both numbers, and neither are nil, ( in this case both are -24) i keep getting an error on line 84 "if xT<xc then" saying goto:84 attempt to compare _ _lt on nil and number

i can't figure it out (this programming has taken much debugging already, this one i just can't find the flaw)

sorry for the n00b coding, any other tips would be great also.
edit: xc,yc and zc are defined by a global variable in the startup
i_am_th0r #2
Posted 12 December 2012 - 04:23 PM
Where u have I=0,4 do u don't have a space from the comma for te two last one that have i=0 bcuz I see it other codes have a space from the comma that could be the error but I'm not quite sure bcuz I heard lua ignored white lines (empty space,like HTML)
Lyqyd #3
Posted 12 December 2012 - 04:30 PM
Where u have I=0,4 do u don't have a space from the comma for te two last one that have i=0 bcuz I see it other codes have a space from the comma that could be the error but I'm not quite sure bcuz I heard lua ignored white lines (empty space,like HTML)

What? You are way off and what you're talking about makes no difference whatsoever.

OP, let's see the rednet code you're talking about to start the script.
jetbooster #4
Posted 12 December 2012 - 11:33 PM
Derped so hard….

woke up this morning and realised what i'd done, xT ,yT and zT are local variables, which was theriginal intention, but then i stuck an if statement around it. doing "goto base" on the turtle itself sets xT,yT and zT as global variables…
afaik, that should fix it

thanks for your help anyway.
sasuke5342 #5
Posted 13 December 2012 - 10:51 AM
i have a problem with mine i was watching Guude's vid and copied his code and it wont work canu find why? this worked on his vid though.
– Variables
turtle.select(1)
local tArgs = ( … )
togo = tonumber(tArgs[1])
togo = togo or 1
– Starting Out
t = 0
for i = 1, togo do
if togo >= 10 then
t = t + 1
if t == 7 then
turtle.placeUp()
t = 0
end
end
if turtle.detect() then
repeat
turtle.dig()
sleep(0.25)
until turtle.detect() == false
turtle.forward()
if turtle.detectUp() then
turtle.digUp()
end
else
turtle.forawrd()
if turtle.detectUp() then
turtle.digUp()
end
end
end
– Lets assume that worked and he made i
turtle.turnRight()
turtle.turnRight()
for i = 1, togo do
turtle.forward()
end