0
votes

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)

2

2 Answers

1
votes

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!)

0
votes

You need to put program1 in quotes:

require "program1"