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

Tutorial: Creating An Api

Started by Advert, 20 February 2012 - 02:16 PM
Advert #1
Posted 20 February 2012 - 03:16 PM
Creating an API in computercraft is actually very simple:
Just define the functions you would like to expose in your API, save it as a file, then os.loadAPI it.

PROTIP:
Reboot your Computer (CTRL-R/CTRL-S/reboot/shutdown) if your API errors, as the computer will think it's still being loaded if it errors.

For example: Save this as SPD (semi-persistent data)

-- Example semi-persistent data (does not save to files; you could add that, of course) API
local data = {}

-- Return this API's version! This can be very useful for developers, especially for complex APIs
function getVersion()
return 0.2
end

-- A function to get data from the API
function getData(key)
return data[key]
end

-- A function to set data in the API
function setData(key, value)
local oldData = data[key]
data[key] = value
return oldData -- It's always useful to return the old data when doing this.
end

This API would expose the following functions: SPD.getVersion(), SPD.getData(key), SPD.setData(key, value), and allow you to get/set data between programs, as long as the computer has not rebooted.

I think this is about it; If you still aren't sure, you can always take a look at rom/apis/*, or make a post!
luza #2
Posted 21 February 2012 - 02:25 PM
German translation:

Es ist ziemlich einfach ein API in Computercraft zu erstellen.
Definiere einfach die Funktionen die du in deinem API haben möchtest und speichere sie in einer Datei. Um das API zu laden benutze os.loadAPI(apiname).

Beispiel: speichere das als HBW (Halbbeständige Werte)

-- Beispiel halbbeständige Werte (speichert die werte nicht, das könntest du natürlich hinzufügen) API
local data = {}

-- Gibt die Versionsnummer dieses API zurück. Das kann für Entwickler sehr nützlich sein; besonders bei komplizierten APIs.
function getVersion()
return 0.2
end

-- eine Funktion um Werte vom API zu bekommen.
function getData(key)
return data[key]
end

-- eine Funktion um Werte im API festzulegen.
function setData(key, value)
local oldData = data[key]
data[key] = value
return oldData -- es ist beim Festlegen neuer Werte immer sinnvoll die alten Werte zurückzugeben.
end
DarkNinja2462 #3
Posted 24 February 2012 - 12:08 AM
I have an error
Moved here:
http://www.computercraft.info/forums2/index.php?/topic/185-api-help/
Advert #4
Posted 24 February 2012 - 12:55 AM
I made an api to add some extra turtle functions. I also made a program to run it.
Currently the only function is to see if the turtle's inventory is full.
I got the error:

isFull:3: attempt to index ? (a nil value)
The program isFull:

os.loadAPI("turtle2")
state = turtle2.isFull()
print(state)
The API turtle2:

if not turtle then
error( "Cannot load turtle API on computer" )
end
function isFull()
if turtle.getItemCount(1) == 64 and turtle.getItemCount(2) == 64 and turtle.getItemCount(3) == 64 and turtle.getItemCount(4) == 64 and turtle.getItemCount(5) == 64 and turtle.getItemCount(6) == 64 and turtle.getItemCount(7) == 64 and turtle.getItemCount(8) == 64 and turtle.getItemCount(9) == 64 then
  return true
else
  return false
end
end
I followed the API tutorial to the letter.
Plz tell me whats wrong?

This is the wrong forum; Post in Ask A Pro instead.

I can't tell from a glance what's wrong, and I can't get in minecraft at the moment; so asking there would be your best shot.
DarkNinja2462 #5
Posted 24 February 2012 - 01:09 AM
Ok moved it here:
http://www.computercraft.info/forums2/index.php?/topic/185-api-help/
sancarn #6
Posted 24 July 2012 - 02:11 PM
can you create APIs in game? - This may be the wrong section?
Tiin57 #7
Posted 24 July 2012 - 09:46 PM
can you create APIs in game? - This may be the wrong section?
Yes.
nateracecar5 #8
Posted 08 January 2013 - 09:54 AM
In my API, I have functions that use strings. I need to know, say, if the function is:

function postUpdate(arg1)
and the user doesn't put in an argument, then how would I make the program say something like "Usage: testAPI.postUpdate( [string] ).
I would also like to know how to make a help file for my API. Thanks!
Nateracecar5
anonimo182 #9
Posted 10 January 2013 - 06:29 AM
1.
 if not arg1 then
print(usage)
return
end

Where usage is how you need to use it

2.
function help()
print(help)
end

Where help is the help string
Galactica4 #10
Posted 14 July 2013 - 02:24 AM
NOT GETTING IT
Kingdaro #11
Posted 14 July 2013 - 05:23 AM
This bump is older than Christopher Columbus' milk. Late help, but for future readers I suppose.

In my API, I have functions that use strings. I need to know, say, if the function is:

function postUpdate(arg1)
and the user doesn't put in an argument, then how would I make the program say something like "Usage: testAPI.postUpdate( [string] ).
I would also like to know how to make a help file for my API. Thanks!
Nateracecar5
You would simply check if arg1 is a string. If arg1 is not given, it will evaluate to nil (undefined), and the function won't run. Then print the usage otherwise. Of course, as the user could easily clear the screen and print something else as the function is used, it's better practice to have your API error out.

function postUpdate(arg1)
  if type(arg1) == 'string' then
    -- do your thing
  else
    error("Invalid argument for function testAPI.postUpdate( [string] )")
  end
end

1.
 if not arg1 then
print(usage)
return
end

Where usage is how you need to use it

2.
function help()
print(help)
end

Where help is the help string
You need to define both of those yourself.


if not arg1 then
  print("Example usage")
  return
end


function help()
  print("Example help")
end

NOT GETTING IT
It's very unhelpful when you just say you're confused and not say what confuses you.
Apfeldstrudel #12
Posted 14 July 2013 - 08:31 AM
Help cant be a string… Its a function… Functions are variables
theeboris #13
Posted 17 July 2013 - 05:23 AM
Dutch translation

Het maken van een API in computercraft is eigenlijk vrij simpel.
Zet de functies die je in je API wilt zien er gewoon in, sla het op en gebruik dan os.loadAPI().


PROTIP

Herstart je computer als je foutmelding krijgt van je API.

Voorbeeld: Sla dit bestand op als SPD (semi-pesistent data)


-- Semi-persistent date (word niet opgeslagen in bestanden, ja kan dat uiteraard wel toevoegen) API
local data = {}

-- Geeft de versie van de API. Dat kan handig zijn voor ontwikelaars, vooral voor complexe APIs
function getVersion()
return 0.2
end

-- Een functie om data van de API te krijgen
function getData(key)
return data[key]
end

-- Een functie om de data dat opgeslagen is in de API aan te passen
function setData(key, value)
local oldData = data[key]
data[key] = value
return oldData -- Het is altijd handig om de oude data te behouden
end

Deze API heeft de volgende functie's: SPD.getVersion, SPD.getData(key), SPD.setData(key, value). Je kan dit gebruiken om gegevens tussen programma's uit te wisselen. De data blijft bewaard totdat de computer opnieuw word opgestart.

Als je niet zeker bent over iets (of ik een vertaal foutje heb gemaakt) kan je altijd nog kijken in rom/apis of een bericht maken. (in het engels) Je kan mij ook altijd een berichtje sturen. Dan kan ik je mischien helpen.
UMayBleed #14
Posted 17 July 2013 - 03:28 PM
Ok that is new, I haven't seen anyone translate for others xD