This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
CCNerd a.k.a CCNoob's profile picture

how to make an API?

Started by CCNerd a.k.a CCNoob, 15 July 2014 - 06:12 AM
CCNerd a.k.a CCNoob #1
Posted 15 July 2014 - 08:12 AM
I want to make an API (not air pollution index, application programming interface).
How?
theoriginalbit #2
Posted 15 July 2014 - 08:52 AM
An API is just like any other program, except that you use os.loadAPI instead of shell.run (well you can use shell.run, but you shouldn't).
The main difference is that instead of like in a normal program where you should define all your variables and functions as local (See Programming in Lua 4.2). In an API you define certain functions or variables as global so that they're accessible from outside the API to any programs that wish to use them.

Example API (named `things`)

--# these will not be accessible outside your API, only internally will they be accessible
local stuff = "Did stuff!"
local function doStuff()
  print( stuff )
end

--# these will be accessible to any program after your API has been loaded with os.loadAPI
starter = "Hello World!"
function foo()
  print( starter )
end

Example program

os.loadAPI( "things" )

things.foo()
print( "Starter is: ", things.starter )
Edited by
CCNerd a.k.a CCNoob #3
Posted 15 July 2014 - 10:21 AM
So you mean…
If I make a API that is located at /APIs/api

local name = "api"
function api(str)
print("The API called "..name.."is printing "..str.." out.")
end
do I call the API with:

os.loadAPI("/APIs/api")
api.api("hello")
theoriginalbit #4
Posted 15 July 2014 - 10:47 AM
Yes you're correct.