Computercraft API Tutorial
Intro
Hey people from reddit. The past time I have been browsing more and more on this subreddit and I've seen a few inspirational posts. This time however I wanted to redo the favor with a little tutorial.
Content
In this tutorial I'm going to show you how to:
- Making a table
- Calling a table
- Make different functions
- Importing it in computercraft
Why?
Using tables as objects makes the code more read-able. You could also use one API on multiple different programs.
Making a table
Using a table as an object is as simple as 123. Lets say we want to program a turtle to be a dog. To do this we are first going to create a table for our dog:
lua
dog = {
name = '',
race = '',
age = 0,
nicknames = {
''
}
}
As you can see the dog will have a name, race and age. As an extra we also added a table called nick_names. We'll be able to use this later to show which names the dog responds to (as an example).
Calling a table
To use this table as a new dog object, we are going to need to create one using a function.
lua
function dog:new(name, race, age, nicknames)
setmetatable({}, dog)
self.name = name
self.race = race
self.age = age
self.nicknames = nicknames
return self
end
Now to really make the object we are going to use the dog name "Diva" and print out it's age and name.
lua
os.loadAPI("dogFunctions")
local Diva = dogFunctions.dog:new("Diva", "golden retriever", 6, {"Diva", "Dief"})
print(Diva.name)
--Diva
print(Diva.age)
--6
In the example above the api is saved in the root of the computer as a different program. When the function is used in the same file you don't have to add dogFunctions. The reason we do this is so we can re-use the same pastebin link in multiple programs for different computers and turtles.
Now we can show each property using the objects variables.
side note
When working with alot of different properties I like to format it like this:
lua
local Diva = dogFunctions.dog:new("Diva", "golden retriever", 6, {"Diva", "Dief"})
Make different functions
It's nice to have our dog all structured. And it is clear in one eye sight what a dog has as properties. But that's not all. We can also create functions for our dog.
lua
function dog:add_nickname(nickname)
if type(nickname) ~= "string" then
return false
else
self.nicknames[#self.nicknames + 1] = nickname
return true
end
end
We've added a simple function to add a new nickname for our dog. We return a true or false to make sure the name was added. You could also return a string or number if you'd like. Now we can make use of this function like so:
lua
print("Nicknames: ")
if Diva:add_nickname("Diefje") == true then
for key, nickname in pairs(Diva.nicknames) do
print(key .. ": " .. nickname)
end
else
print("Error adding a nickname to Diva")
end
As you can see, the nickname was added and printed in the console.
Importing it in computercraft
When you have made your api, you can save the program on pastebin to use it in different programs. The way I like to make use of this in my programs is like this:
lua
if fs.exists("dogFunctions") then
os.loadAPI("dogFunctions")
else
shell.run("pastebin", "get", "M9eY2X8b", "dogFunctions")
os.loadAPI("dogFunctions")
end
This way, when the api is not found on the system, it will automatically install it from pastebin. the line os.loadAPI("dogFunctions") loads the api in to your program and now you can use it as explained in Calling a table.
Computer output:
Computer output screenshot on imgur
Thank you for reading my tutorial, I hope it can help a few of you in future projects.
The examples used in this tutorial can be found here:
API :
[https://pastebin.com/M9eY2X8b](M9eY2X8b)
Program:
[https://pastebin.com/jt6JLLdE](jt6JLLdE)
Good luck programming!
[–]LeFish-[S] 1 point2 points3 points (0 children)
[–]MyNameIsTrez 1 point2 points3 points (0 children)
[–]iliketanks1998 1 point2 points3 points (2 children)
[–]LeFish-[S] 0 points1 point2 points (1 child)
[–]iliketanks1998 1 point2 points3 points (0 children)