Difference between revisions of "Making an API (tutorial)"
m |
|||
Line 7: | Line 7: | ||
== Introduction == | == Introduction == | ||
Hello, dear Computercraft Wiki user! | Hello, dear Computercraft Wiki user! | ||
− | Today I will tell you how to make | + | Today I will tell you how to make a functional API (Application Programming Interface). |
Let's get started! | Let's get started! | ||
Line 15: | Line 15: | ||
In this case, I use exampleAPI. | In this case, I use exampleAPI. | ||
Now edit the file you want your API to be called. | Now edit the file you want your API to be called. | ||
− | + | <!--NOTE: If it exists, call it something else--> | |
=== The code === | === The code === | ||
Now, we need to put some functions in it! | Now, we need to put some functions in it! | ||
− | Why don't we start with the basics | + | Why don't we start with the basics: |
− | < | + | <pre> |
-- this is the file exampleAPI | -- this is the file exampleAPI | ||
-- our first function: | -- our first function: | ||
function printMessage() | function printMessage() | ||
− | + | print("message") | |
end | end | ||
− | </ | + | </pre> |
Which basically prints "message" when excecuted. | Which basically prints "message" when excecuted. | ||
Save the file. | Save the file. | ||
Line 33: | Line 33: | ||
=== Including the API === | === Including the API === | ||
Now that we have the API done, let's include it in some file! | Now that we have the API done, let's include it in some file! | ||
− | Because we speak about APIs, we | + | Because we speak about APIs, we can't directly execute it. |
− | + | ||
Edit the file you want to include the API in. | Edit the file you want to include the API in. | ||
In my case, it is called exampleFile. | In my case, it is called exampleFile. | ||
Now, we include the API in it by typing this to the '''first''' row: | Now, we include the API in it by typing this to the '''first''' row: | ||
− | < | + | <pre> |
os.loadAPI("exampleAPI") | os.loadAPI("exampleAPI") | ||
− | </ | + | </pre> |
This loads the API into the file's memory. | This loads the API into the file's memory. | ||
''NOTE: You have to do this for every file you include it in. | ''NOTE: You have to do this for every file you include it in. |
Revision as of 20:29, 15 March 2012
Contents
MAKING AN API - a tutorial by TheVarmari
NOTE: There is a better tutorial in the forums located here. Use this if you prefer to use ccwiki.
Introduction
Hello, dear Computercraft Wiki user! Today I will tell you how to make a functional API (Application Programming Interface). Let's get started!
Making the API file
First we need to define what our API will be called. In this case, I use exampleAPI. Now edit the file you want your API to be called.
The code
Now, we need to put some functions in it! Why don't we start with the basics:
-- this is the file exampleAPI -- our first function: function printMessage() print("message") end
Which basically prints "message" when excecuted. Save the file.
Including the API
Now that we have the API done, let's include it in some file! Because we speak about APIs, we can't directly execute it.
Edit the file you want to include the API in. In my case, it is called exampleFile. Now, we include the API in it by typing this to the first row:
os.loadAPI("exampleAPI")
This loads the API into the file's memory. NOTE: You have to do this for every file you include it in. CC doesn't have long-term memory.
Using the API
Now that that's over, let's include the printMessage command from our API. Lua loads API's as tables, so you have to use api_name.function_name(function_arguments)
In my case, I use
exampleAPI.printMessage()
Ta-da! It printed out "message"!
A couple of words
Thank you for reading this tutorial, it sure was fun to make! Correct any mistakes I did! TheVarmari 16:59, 22 February 2012 (UTC)