|
|
Line 1: |
Line 1: |
− | | + | FUCK YOUR FACE |
− | == MAKING AN API - a tutorial by TheVarmari ==
| + | |
− | '''NOTE:''' There is a better tutorial in the [http://www.computercraft.info/forums2/ forums] located [http://www.computercraft.info/forums2/index.php?/topic/145-tutorial-creating-an-api/ 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.
| + | |
− | <!--NOTE: If it exists, call it something else-->
| + | |
− | | + | |
− | === The code ===
| + | |
− | Now, we need to put some functions in it!
| + | |
− | Why don't we start with the basics:
| + | |
− | | + | |
− | <pre>
| + | |
− | -- this is the file exampleAPI
| + | |
− | -- our first function:
| + | |
− | function printMessage(message)
| + | |
− | print(message)
| + | |
− | end
| + | |
− | </pre>
| + | |
− | 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 exampleAPI.
| + | |
− | Now, we include the API in it by typing this to the '''first''' row:
| + | |
− | <pre>
| + | |
− | os.loadAPI("exampleAPI")
| + | |
− | </pre>
| + | |
− | This loads the API into the file's memory.<br />
| + | |
− | '''NOTE:''' If you want your API to be loaded on every computer, copy your file to ''.minecraft\mods\ComputerCraft\lua\rom\apis\''.
| + | |
− | | + | |
− | === 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
| + | |
− | <code>
| + | |
− | exampleAPI.printMessage("message")
| + | |
− | </code>
| + | |
− | 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!
| + | |
− | [[User:TheVarmari|TheVarmari]] 16:59, 22 February 2012 (UTC)
| + | |
− | | + | |
− | [[Category:Tutorials]]
| + | |