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

Creating API handles

Started by MrScissors, 12 November 2015 - 01:49 PM
MrScissors #1
Posted 12 November 2015 - 02:49 PM
So I want to create api with handles (like in peripheral api. You wrap and create handle in variable and then call this handle). But how to do it?

Also i know i have been recently asking lotsa stuff but i am just learning :)/>
KingofGamesYami #2
Posted 12 November 2015 - 03:04 PM
Handles are just tables.


function getHandle()
  return {
    say = function() print( "Hello World!" ) end,
  }
end

local h = getHandle()
h.say()
MrScissors #3
Posted 12 November 2015 - 04:43 PM
thanks

Also, how do i return (in this function) stuff like my previous made array?
I mean like it reads file line by line in function and then i want my handler to return specific lines (handler).get(lineid)
Edited on 12 November 2015 - 03:44 PM
Bomb Bloke #4
Posted 12 November 2015 - 04:55 PM
You're better off just reading the whole file into a table and returning that.

Eg:

local function getFileLines(fileName)
  local results = {}
  for line in io.lines(fileName) do results[#results + 1] = line end
  return results
end

local fileLines = getFileLines("someFile")

print(fileLines[3])  --> Prints the 3rd line of the file.