0

I have been writing a lot of turtle programs lately and find that I am constantly rewriting the same functions. I would like to put all these functions into their own file and then include them at the top of my other turtle programs. Looking through the lua documentation it seems I need to use require. However I can't seem to get it to work. Here is a basic example I tried:

program1:

 print("Hello World")

program2

require program1 

This resulted in the following error:

bios:366: [string "program2"]:1: '=' expected

I then tried setting the package path to the current directory before making the require statement like this:

package.path = package.path .. ';./?.lua;'

That also didn't work. It seem that turtles don't use the same syntax as lua for the require statement (if they support it at all)

Is there a way to do something like this with the turtles? If it is using 'require' then what is the proper syntax? (I don't have access to the files of the server I play on so simply editing them is not an option)

Universal Electricity
  • 775
  • 1
  • 12
  • 26
metamilo
  • 213
  • 2
  • 5

2 Answers2

1

require is a function; you need to call it using function syntax:

require("foo")
-- or
require "foo"

...but it doesn't look like ComputerCraft supports the require function. Try os.loadAPI("path/to/file").

(Dear developers wanting to sandbox Lua: require supports configuring how it loads modules; please use that instead of replacing require!)

Colonel Thirty Two
  • 23,953
  • 8
  • 45
  • 85
0

You need to put program1 in quotes:

require "program1"
Paul Kulchenko
  • 25,884
  • 3
  • 38
  • 56
  • Thanks for the prompt reply but I did try that and it gave this error: (program1:1: attempt to call nil) which I assume just means the require failed. – metamilo Mar 05 '15 at 06:17
  • This means `require` is not available in the ComputerCraft environment. See @ColonelThirtyTwo's answer for the alternative. – Paul Kulchenko Mar 05 '15 at 16:54