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

Creating and Loading custom API's

Started by Cloud Ninja, 25 July 2016 - 11:54 PM
Cloud Ninja #1
Posted 26 July 2016 - 01:54 AM
Seeing as the recent tutorial on the loading of custom API's and that a lot of people don't know about THIS wiki article, I'll take it upon myself to try to write a comprehensive tutorial on:


The Creation and Loading of Custom API's


Creation

SpoilerIn order to create API's you'll need to declare some functions inside of a file, like this:


function FunctionName(arguments to pass in)
Code to run based on the arguments
end
So, with that being said, i can create a function like such:

function add(a,c)
return a+c
end

add(8,2)

returns: 10
Now, lets say you need to create a function only to be used internally, not by a user.

local function internalAdd(1,2)
return 1+2
end
This would now make it so you can only use it inside of the API itself.

When creating an API, you should name it something relevant to the usage. If its an API to write to a file, you could make it, 'fileWrite'

Note: You can have more than one function in an API. You can have as many as you can think of!


Loading + Usage

SpoilerSo you have an API that you or someone else has created, and want to use it. Lets go back to our previous example:


function add(a,c)
return a+b
end
Lets say this API's name was "adder."
What i could do inside of my program or in the lua prompt is now run:

os.loadAPI("adder")
adder.add(8,10)
return: 18
Now, i have full access to this API and any functions inside of it.

I hope this helps someone!
Edited on 26 July 2016 - 03:14 AM
The Logo Maker #2
Posted 30 July 2016 - 07:23 PM
better
Cloud Ninja #3
Posted 30 July 2016 - 07:25 PM
better
thank