An API (Application Programming Interface) is a collection of code that, rather than being run directly by the user as a program, is meant to be used by other programs. This is different from a function in that an API may contain many functions that perform related tasks; the API groups them together.
There are two reasons why one might want to use an API:
- The API may provide a function that you could write yourself if you had to, but using the one from the API means you don't have to do that work.
- The API may provide something it's impossible to do yourself because it has to be done in Java rather than Lua.
ComputerCraft itself ships with a collection of APIs of both types. For example:
- The colour API provides names for the various numbers used in RedPower bundled cable, and functions that manipulate those numbers. The names for the numbers are just variables, and the functions are written in Lua (you can find this in "rom/apis/colors"). You could write the same variables and functions in your own code if you wanted, but using the API saves you from doing that.
- The fs API provides functions to work with files. You couldn't write the functions from this API in Lua because they have to access the corresponding files in your real computer (the one running Minecraft), and the only way to do that from Lua is… through the fs API (or the io API, but that's just a wrapper around fs).
All the stock APIs of the first kind, those that are implemented in Lua, can be found in your "mods/ComputerCraft/lua/rom/apis" directory. Inside Minecraft, on a ComputerCraft computer, these appear in the directory "rom/apis". All the built-in APIs of both kinds (Lua and Java) are automatically loaded, so you don't have to use "os.loadAPI" on them.
ComputerCraft mods, for example mods that add custom peripherals, might also add APIs that help you work with those peripherals.
Finally, you might find APIs written by other people that you can download, or you might write APIs yourself if you'll be using a function in many different programs and don't want to keep rewriting it. If you're playing single-player or you're running the multiplayer server, you can add those APIs to "rom/apis" and they will automatically be loaded on every computer in the world. Normally, though, you would leave the APIs somewhere else and use "os.loadAPI" to make them available to your program.
You can see a list of APIs by typing "apis" in the terminal.
All items (14)