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

Api Basics

Started by Yevano, 02 September 2013 - 08:14 AM
Yevano #1
Posted 02 September 2013 - 10:14 AM
This is a tutorial in the Ask a Pro Renewal Project series.

In this tutorial, you will learn why APIs are used, how to use them, and how to create your own API. It is helpful to first read Lqyd's series on Computer Basics (start here) before reading this tutorial. You should also have at least basic knowledge of Lua. For the Programming in Lua manual, go here.

The API (Application Programming Interface) system in ComputerCraft provides a standard way to load libraries from separate files. Without the API system, you would find yourself making programs which contain the same code to do common things. For example, suppose you write a custom text editor, and you write many GUI functions for it inside the program. Later, you might want to do something else with these GUI functions, like create a file explorer. The problem is, you would have to copy and paste all of those functions from your old program into the new one. You'll end up spending more time and writing more code than you bargained for! A common solution is to separate your common functions into an API. This way, any program you make which needs to use those GUI functions can just call them from the API, and you can make improvements to your API in one file, instead of having to make changes across multiple files.

Let's first have a look at the help message for the apis command. Enter help apis into the shell prompt.



To restate what the picture says, the apis command lists all of the currently loaded APIs. We're also told that we can run help <api> (e.g. help os) to get help for a specific API, and the message tells us how we can load an API using os.loadAPI. Let's first try running apis and see what the console spits at us.



What we have here is a complete list of all the APIs that are automatically loaded from the /rom/apis/ folder when CraftOS starts. You don't have to load these yourself, since they're automatically made available. Let's try using some of these APIs in the Lua prompt. Open the Lua prompt by entering lua into the shell prompt, and then enter these lines into the Lua prompt.


math.floor(22.2)
string.sub("Hello", 2, 4)
textutils.serialize({a=2, b={"b"}, c="c"})




Exit the Lua prompt by entering exit() into the Lua prompt. By example, you can see that you can call an API function using the form someAPI.someFunction(…). Now let's look at how we can create and use our own API. Start off by creating the file that will hold the API functions. Enter edit testAPI into the shell prompt. Then, enter the code into the editor as shown below.

function sayHello()
    print("Hello from testAPI!")
end



Press Ctrl and select exit to leave the editor. The sayHello function will now be available to any program that loads this API. Let's see that in action. Go back to the Lua prompt and enter os.loadAPI("testAPI"). This will load our API so that any program can use it. Note that the argument given to os.loadAPI must be the precise path to the API file. The name of the file determines the name of the API in code. To make sure it's loaded, run the apis command again.



If you look closely, you can see that our API is now listed! Since it's loaded, let's try calling that function we defined inside of it. Go back to the Lua prompt and enter testAPI.sayHello().



Now that we're done with it, let's unload the API. Enter os.unloadAPI("testAPI") into the Lua prompt. Just like os.loadAPI, the argument must be the precise path to the API you want to unload. Exit the Lua prompt and run the apis command again so we can verify the API has been unloaded.



Our API is no longer listed, since it has been unloaded.

Let's backtrack. In this tutorial you learned:
  • Why APIs are used.
  • How to call API functions.
  • How to create, load, and unload your own simple API.
That's it, I'll be working on another API tutorial within probably the next few days. There will be a link here when it's done. Feedback is appreciated.
Zudo #2
Posted 02 September 2013 - 01:18 PM
This is a nice tutorial :)/>
MudkipTheEpic #3
Posted 02 September 2013 - 02:49 PM
You never explained what functions are.
Yevano #4
Posted 02 September 2013 - 04:14 PM
You never explained what functions are.

I don't think an explanation on functions would really be appropriate for this tutorial, since that has more to do with the actual language than how APIs work. Now that you mention it, though, I will go ahead and link to the PIL website for those who may not know Lua.
theoriginalbit #5
Posted 02 September 2013 - 04:22 PM
I don't think an explanation on functions would really be appropriate for this tutorial, since that has more to do with the actual language than how APIs work. Now that you mention it, though, I will go ahead and link to the PIL website for those who may not know Lua.

I was originally going to side with you, stating that it would be out of the scope, but then when I read the AaP Renewal thread again, I found that there aren't really any "Lua basics/intermediate" threads that would explain things like functions. For yours however it did say this
API basics - purpose for using, functions in APIs, simple example API
So it says functions in APIs, so it may be worth putting a basic explanation, Lyqyd will ultimately have the overall say as to whether you should or not though.

Also I suggest you put up the top about how this is part of the Renewal series, like Lyqyd has on his two.

EDIT: Apparently I hit post instead of "more reply options", gimme a second done
Edited on 02 September 2013 - 02:26 PM
Yevano #6
Posted 02 September 2013 - 04:34 PM
I don't think an explanation on functions would really be appropriate for this tutorial, since that has more to do with the actual language than how APIs work. Now that you mention it, though, I will go ahead and link to the PIL website for those who may not know Lua.

I was originally going to side with you, stating that it would be out of the scope, but then when I read the AaP Renewal thread again, I found that there aren't really any "Lua basics/intermediate" threads that would explain things like functions. For yours however it did say this
API basics - purpose for using, functions in APIs, simple example API
So it says functions in APIs, so it may be worth putting a basic explanation, Lyqyd will ultimately have the overall say as to whether you should or not though.

Also I suggest you put up the top about how this is part of the Renewal series, like Lyqyd has on his two.

EDIT: Apparently I hit post instead of "more reply options", gimme a second done

I assumed there weren't supposed to be any Lua tutorials since there are already many on the Internet, which is why I linked to PIL. I could be interpreting that wrong, though. And yeah I'll put the AAP bit at the top of OP.
Lyqyd #7
Posted 02 September 2013 - 04:39 PM
We are generally steering away from trying to tackle Lua language constructs ourselves, since they are so well-documented elsewhere. A quick synopsis and link to other documentation of a newly-introduced concept is probably a good thing to have, though. I will be sure to add some links to the PIL, reference manual, and Lua-users wiki to Computer Basics 1 and possibly 2 as well.

However, if we find that there is no documentation or tutorials written at an appropriate level for what we are going for (for instance, the Reference Manual is often too dense and technical), we can come up with a Lua series highlighting a concept at a time. I hope this doesn't turn out to be necessary.
Lyqyd #8
Posted 03 September 2013 - 02:04 PM
Double posting to add feedback:

This is definitely a good API basics tutorial, a solid foundation. I would suggest that we add the code for our sayHello function in a code block on the forum post rather than solely existing in the image, in case the image were to go down or be unavailable for other reasons.
Yevano #9
Posted 03 September 2013 - 04:39 PM
Double posting to add feedback:

This is definitely a good API basics tutorial, a solid foundation. I would suggest that we add the code for our sayHello function in a code block on the forum post rather than solely existing in the image, in case the image were to go down or be unavailable for other reasons.

Thanks for the feedback. Added a code block above the image.
itsme #10
Posted 06 September 2013 - 07:42 AM
Hello!
Which directory should I copy my custom API file to, so that it can be loaded automatically (without using the os.loadAPI() function). The wiki states: "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." but there is no such directory. The SP &amp; MP ComputerCraft mod resides in a zip file. I'm using the latest Tekkit Lite.

Thanks
Yevano #11
Posted 06 September 2013 - 05:46 PM
Hello!
Which directory should I copy my custom API file to, so that it can be loaded automatically (without using the os.loadAPI() function). The wiki states: "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." but there is no such directory. The SP &amp; MP ComputerCraft mod resides in a zip file. I'm using the latest Tekkit Lite.

Thanks

It's inside the lua folder which resides in your computercraft mod zip. Another option, however, is to create a startup file in your computer which loads the API. You could even do this for all computers automatically by using resource packs (Do some research on those. I've never used them.).
itsme #12
Posted 07 September 2013 - 11:01 AM
Hello!
Which directory should I copy my custom API file to, so that it can be loaded automatically (without using the os.loadAPI() function). The wiki states: "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." but there is no such directory. The SP &amp; MP ComputerCraft mod resides in a zip file. I'm using the latest Tekkit Lite.

Thanks

It's inside the lua folder which resides in your computercraft mod zip. Another option, however, is to create a startup file in your computer which loads the API. You could even do this for all computers automatically by using resource packs (Do some research on those. I've never used them.).

Thank you for your reply!
bologna #13
Posted 16 September 2013 - 03:33 AM
This is a great tutorial. Coming from very very limited programming experience i have just learned what functions are and was trying to figure out how i can make functions available for different programs i design for the turtle without having to rewrite the code for each one. I found this and it was very clear, step by step, with no typing errors (us beginner programmer's worst nightmares), and had clear percise goals it achieved. Thank you for this great tutorial that will save me a lot of time and code.
Yevano #14
Posted 16 September 2013 - 04:05 PM
This is a great tutorial. Coming from very very limited programming experience i have just learned what functions are and was trying to figure out how i can make functions available for different programs i design for the turtle without having to rewrite the code for each one. I found this and it was very clear, step by step, with no typing errors (us beginner programmer's worst nightmares), and had clear percise goals it achieved. Thank you for this great tutorial that will save me a lot of time and code.

I'm glad it helped you!

Edit:

I was searching for the same topic/ question but this page really satisfied me with the details which are very much important as well as will help others also.

Glad it helped you, too!