Jump to content
  • Sky
  • Blueberry
  • Slate
  • Blackcurrant
  • Watermelon
  • Strawberry
  • Orange
  • Banana
  • Apple
  • Emerald
  • Chocolate
  • Charcoal
  • 0
OnyxFox

Beginner Help

Question

So I've started off trying to write a tree chopping program with a robot I put together. I can get the robot to do the tasks using the interactive Lua script but when I put it into a program file I start having issues. I am pretty familiar with the Lua from CC and have played with it a good bit. 

 

First question:

Why must I have this at the beginning of the code to call commands?

local robot = require("robot")

Issue:

When I start off with a simple

robot.swing()
robot.forward()

the robot will not move forward after it finishes swinging. However if it says to move back it will move backwards.

Link to post
Share on other sites

4 answers to this question

Recommended Posts

  • 0

First question:

Why must I have this at the beginning of the code to call commands?

local robot = require("robot")

Unlike in ComputerCraft, not all APIs are loaded into the global namespace by default. Lua itself provides a few standard functions and libraries in the global environment, but anything other than that must be loaded using require. In the case of robot the library is actually loaded from a file /lib/robot.lua, like many other of OpenOS's libraries. It would make no sense to automatically load all libraries in /lib when the computer starts. Not only would it make the OS take a lot longer to load, it would also be quite messy.

 

Issue:

When I start off with a simple

robot.swing()
robot.forward()

the robot will not move forward after it finishes swinging. However if it says to move back it will move backwards.

Most robot commands won't generate a Lua error and stop the script when they fail for some reason - not every failed robot command is a programming error. Instead they return true if the command succeeded, and false plus an error message if it failed. To see the return value, wrap the robot command in a print call:

robot.swing()
print(robot.forward())
Link to post
Share on other sites
  • 0

Thanks! I tried it with the print added and it returned as "nil solid" even though it had just broken the block. Am I doing something wrong? Do I need to add a delay? I should also note that my robot doesn't have a tool so it takes a second to break the block.

 

EDIT: I tried the program using different axes. Although the speed was increased with each tier, the robot will only move forward after the break with the diamond axe. Every other time it returns as "nil solid".

 

Do I need to post about this on Github? I'm not sure if this is a bug or not. 

Link to post
Share on other sites
  • 0
  • Solution

Ok I figured a solution to make sure this works no matter what. I realize most of you on the forum will already know this but for those who don't, here is my workaround for this issue. 

local robot = require("robot")

local function move()
  while robot.forward()==nil do
    robot.swing()
  end
end

move()
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use and Privacy Policy.