Jump to content

ComputerCraft Mod - How To Write A Computercraft Api


Recommended Posts

This tutorial is dedicated to shawnmick23, as he wanted to see the bridge from C's include directive to ComputerCraft's os.loadAPI function. Even though I don't know a whole lot of C, I know a whole lot of lua :P.

 

API's can be used for many different things! The most common uses are Effecient distribution of code packages, Efficient storing of functions, etc. So, today We will be learning one of the most useful things in the os api for computercraft. This is the function os.loadAPI()

 

The function os.loadAPI takes a parameter of a string which is the filename. e.g.

os.loadAPI("filenamehere")

Once you call this, all of the functions in that file will from then on be available from inside the file that called it. However, whenever you edit the api, you will need to call it again for the changes to take place. The best way to do this is to call the loading of the api in the first line of the program that you're editing.

 

An Example api would be a file named ccolorapi - Custom Color Api!

-- This is an api for blah blah blah blah.
-- made by your name here, please give credit!

apihelp = {
  --custom defined help dictionary, much like peripheral.getMethods()
  --function name, function desc
  {"clearColor","Makes the background color the specified color"},
  {"setColorVars","Sets all of the variables that have to do with colors"}
}

function listMethods()
  for i = 1,#apihelp-1 do
    print(apihelp[i][1]..",")
  end
  print(apihelp[#apihelp][1]..".")
end

function help(func)
  for i = 1,#apihelp do
    if apihelp[i][1] == func then
      return apihelp[i][2]
    end
  end
end

function clearColor(color) -- I can see this function being kinda useful
  term.setBackgroundColor(color)
  term.clear()
  term.setCursorPos(1,1)
end

function setColorVars(tcolor,bcolor) -- this one, not so much  
  term.setBackgroundColor(bcolor)
  term.srtTextColor(tcolor)
end

Then, to reference this api, we would do something along the lines of this

os.loadAPI("ccolorapi")
clearColor(colors.lime) -- makes the background lime
print("HELLO WORLD :D")

If you wanted to see the list of functions or the description of a function in the lua prompt or somehting, you would type in the prompt

os.loadAPI("ccolorapi")
ccolorapi.listMethods()
ccolorapi.help("setColorVars")

And that's all there is to it, folks :))

 

Thanks-a-million

-XMedders

Link to comment
Share on other sites

  • brunyman changed the title to ComputerCraft Mod - How To Write A Computercraft Api

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

By using this site you agree to the following Terms of Use, Guidelines and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.