ComputerCraft 'require' equivalent

Hey all. I know I could probably do battle with google to find this out, but I think supporting conversation here would be more useful :)

Lua has the 'require' command that allows you to include/run another file in the execution of your script. Is there an equivalent in ComputerCraft? If so, does someone know what path it checks for the file name, or even if CraftOS has an understandable/traversable path structure?

Any links to definitive sources would be greatly appreciated.

Posted Apr 18, 2013, 7:49 AM UTC

811
revereddesecration•

You mean shell.run()?

Look up the ComputerCraft APIs. The CC wiki is quite good.

3
WraitholmeOP•

Hey, thanks for the reply :)

I'm not sure if it's quite what I'm looking for.

In native lua (in, I suppose a 'typical' compiler) there's a 'require' call. You can stick a bunch of functions in a file called, say myfunctionfile.lua and then in another script, say somescript.lua, you can stick in 'require(myfunctionfile)' and it finds the other file and reads it, essentially sticking the functions in the functionfile into memory as if they were part of the specific script.

Similar to 'include' in a lot of other languages, really. You can do some cool stuff with it, like building a sort of library that your scripts can then cherrypick from.

Actually, shell.run may be what I'm looking for, if it treats the called program like a block that it retains in runtime memory. I found the computercraft.info API wiki but it wasn't too clear at a quick scan, so I'll have to experiment :(

2
revereddesecration•

Sounds like you actually want os.loadAPI(). It loads a script as an API and makes all functions within that script able to be called by the main function.

10
WraitholmeOP•

Oooh, now we're talking. Awesome, thank you :D

3
revereddesecration•

You're welcome. Definitely spend a few minutes perusing through the APIs to get a general understanding of the important functions, it's worth the time.

1
erima•

Oh, nice! I was writing my scripts in a general function sort of way (GPS location, fuel checks, etc) but gave up on it because I couldn't figure out how to call them elegantly. Thanks!

3
totalgeek13•

Wow! This is a really useful piece of knowledge, especially for a person just getting into CC scripting and LUA in general.

1
dirwe•

if all you do is writing down some functions in the file you want to load, then they will be accessably globaly after running that file. Given that you don't mark them as "local".

I am using this method to add some convenience methods to the turtle API.

All I do there is having a file ("advancedturtle") with functions like this in it:

function turtle.digLeft()
function turtle.digRight()
[and so on]

In the startup-script of my turtle I do

shell.run("advancedturtle")

After that I can use the new functions in any script on the turtle.

If you are interested in it you can find the additional functions script here.

2
WraitholmeOP•

That's very useful, thank you :D

1
dirwe•

If I understand you correctly, what you are looking for ist the shell.run(...) command.

you pass a string as the parameter. it will execute the string the same way as if you typed it in the shell.

this way you can run files from within a programm.

I don't know if there are other ways to do this, though.

2
WraitholmeOP•

Thanks for the reply. I answered the same suggestion from revereddesecration in the thread, if it's interesting :)

2