跟踪Lua“ io”库异常的ComputerCraft

时间:2018-07-25 21:26:56

标签: lua automation minecraft computercraft

我目前正在尝试调试具有功能和API功能的基于Minecraft的龟养殖脚本。
当我执行时,我得到一个'io'异常(这是可以理解的,因为我试图为每个循环写一个日志),但是它没有告诉我它在哪个脚本中或行号。除了在整个程序执行过程中进行打印以外,还有其他解决方案吗?

我得到的异常: io:107: index expected, got nil

主要的乌龟养殖脚本:

--[[main script for farming procedure]]

--load functions responsible for positioning/repositioning/farming
os.loadAPI("APIS/positioners"); 
os.loadAPI("APIS/farmers");

--action turtle start farming condition
assert(positioners.check_start_pos(), "Could not initialise the turtle for the farming procedure\nCould not identify it's starting location\n(are there birch planks directly above and beneath it?");

--check Netherrack can be found from start block, declare 'dir' also as local
local init_result, dir = positioners.find_next_neth()
assert(init_result, "Could not find the appropriate direction to travel in from\n the turtle starting position, is the farm set up correctly?");


local loop_vars = {["count"] = 0, ["blockId"] = nil, ["dir"] = nil, ["prevManvr"] = nil, ["action"] = ""};

--**input** assertion tests for required fuel level and seed requirements
assert(turtle.getFuelLevel() > 50, "Fuel level is below required level to start farming, - terminating");

--given 'result' == true can start farming
turtle.forward(); 

--open log file (overwrite) and save to io class handle
log = io.open("logs/proc_log", "w");

--start main farming process loop
while turtle.getFuelLevel() > 0 do

    --reset action variable
    loop_vars["action"] = "";

    --sort log variables
    loop_vars["count"] = loop_vars["count"] + 1;

    --identify the block above (returns inv slot(1-4) it matches with or false if it doesn't)
    loop_vars["blockId"] = positioners.id();

    if blockId == 1 then
         loop_vars["action"] = "Netherrack above ";

         if loop_vars["dir"] == nil then
             print("Netherrack found, planting");

             farmers.plant();

         --shift turtle forward such that it can evaluate a new block. Ensure that moves forward in case of obstruction
         assert(turtle.forward(), "The turtle is impeded from moving forwards in farming route");

         --log statement
         loop_vars["action"] = loop_vars["action"].."plant and move forward";

       else
         --initiate change in direction procedure
         assert(positioners.change_direction(loop_vars["dir"]), "Failed to complete turtle change direction procedure");

         --reset dir
         loop_vars["dir"] = nil;

         loop_vars["action"] = loop_vars["action"].." actioned 'positioners.change_position(dir)'.";
       end;

   elseif blockId == 3 then

       turtle.forward();    
       --log statement
       loop_vars["action"] = "Proceeding past marble block";

  elseif blockId == false then
       print("Finding next Netherrack block.");

       --retrace back to the most recent 'legitimate' block");
       turtle.back();

       --call function to reposition turtle for next farming line
       result, loop_vars["dir"] = positioners.find_next_neth(loop_vars["prevMan"]);
       assert(result, "A new netherrack block could not be found in any direction.");

       --assign dir result to prevMan (information for next positioners.change_position() call
       loop_vars["prevMan"] = loop_vars["dir"];

       --log statement
       loop_vars["action"] = "No 'legitimate' block above, 'positioners.find_next_neth() successful,\nassigning dir accordingly.";

 else
       --log statement
       log.write("Unhandled exception in farm_proc main loop");
       log.close();

       error("positioners.find_next_neth() did not find an appropriate block above it.");
  end;

  print("farm_proc loop");

  --write current turn to log file
  log.write("Turn no: "..loop_vars["count"].."\n    Id()== "..tostring(loop_vars["blockId"]).."\n    Action: "..loop_vars["action"].."\n    Previous maneuver: "..loop_vars["prevMan"].."\n\n");
end;

--updating log
log.write("Error - turtle ran out of fuel\n");
log.close();

error("Turtle ran out of fuel whilst planting - terminated");

“定位器” api(基于运动/检测的任务):

function find_next_neth(prevMan)
    --[[Find direction of the next overhead block of Netherrack from turtle's current pos,
Order;
N
E
W
S
Error (no direction found)]]

--define numbers corresponding to directions
local dirs = {"Back", "Left", "Right", "Forward"};

--create a loop counter to ensure does not exceed full checking loop
local counter = 4;

--loop through movement proc. until netherrack (slot 1) is found above it (if at all)
while true do
    print("Looping, current loop counter is: ", counter);

    local idAhead = id_ahead();

    if counter == 4 then
        --placeholder, script should never need to find forwards
    elseif counter == 3 then
        turtle.turnLeft();  
    elseif counter == 2 then
         turtle.turnRight();
         turtle.turnRight();
    elseif counter == 1 then
         turtle.turnRight();
    --decremented counter has reached it's minimum
    elseif counter < 1 then
        return false, nil;
    else
        error("For find_next_neth iteration counter out of range");
    end;

    --decrement counter variable
    counter = counter - 1;

    --boolean condition to determine whether function is complete, incomplete or broken. 'prevMan' as a logical statement will return true if var has value, and false if not
    local testPrev = (not prevMan == dir[(math.abs(counter - 4) + 1))] or not prevMan);
    local validBlock = (idAhead == 1 or idAhead == 3)

    if testPrev and validBlock then
        --all tests are valid - break loop to return
        break;
    end;

    --report success and return result
    print("Nether found!\n");
    return true, dirs[counter];
  end;
end;

function change_direction(dir)
  --intialise a base error string
  local errMsg = "Could not action positioners.change_direction(dir, prevMan) ";

  if dir == "East" then
     assert(turtle.forward(), errMsg.."Obstruction to route");
     --this should finalise a 180deg turn to the left
     turtle.turnLeft()
  elseif dir == "West" then
      assert(turtle.forward(), errMsg.."Obstruction to route");
      --180deg turn left
      turtle.turnRight()
  elseif dir == "North" then
      --no action
  elseif dir == "South" then
      error("Return to start not yet implemented");
  else
      error("positioners.change_direction() parameter not as expected");
  end;
return true;
end;

function id(slot)
  --[[Determines if netherrack is currently above turtle.
  Return false means the block
  above does not match slots 1-4 of the
  turtle's inv. Takes optional int
  for specific block slot index
  in turtle compare]]

  --checking argument
  if slot ~= nil then
    --select slot given as argument
    turtle.select(slot);
    if turtle.compareUp() then
      --don't return int because specific material requested
      return true;
    else
      return false;
    end;
  end;

  counter = 1
  while counter < 5 do
    turtle.select(counter);
    --identify whether the current selected slot item matches the blovk overhead
    if turtle.compareUp() then
       --return block above's corresponding slot index
       return counter
    end;
  counter = counter + 1;
  end;
  --return number indicating unidentfied block
  return false;
end;

function id_ahead(slot)
  if turtle.forward() then
    --save value of id call for later return
    local rtn = id(slot)
    turtle.back();
    return rtn
  else
    error("turtle could not move forwards in id_ahead(slot)");
  end;
end;

function check_start_pos()

  --set selected slot to birch planks (no currently selected method implemented so cannot revert this change internally)
  turtle.select(2);

  --save boolean variables for comparison of blocks above and below position
  local up = turtle.compareUp();
  local down = turtle.compareDown();

  --test if above boolean values are true (turtle in designated start position)
  if up and down then
    return true;
  else
    print("Not in appropriate farming start position");
    return false;
  end;
end;

(“农民” API只是检查当前的种子,然后通过Farmers.plant()种植农作物

1 个答案:

答案 0 :(得分:0)

我相信我已经找到了解决方案-调试库,特别是debug.traceback()允许您在捕获错误时更仔细地检查调用堆栈

debug library index