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

Loading an API - the best way?

Started by Dave-ee Jones, 12 September 2017 - 10:50 PM
Dave-ee Jones #1
Posted 13 September 2017 - 12:50 AM
Hoi,

I'm creating an API (GUI-based) and I'm wondering how best to load it as an API. It uses OOP (metatables and object-based function calling). Because of this, 'os.loadAPI' isn't working.

I'm currently using 'dofile(apiPath)', which is working well, but it also means all the metatables and their functions need to be global which means once the API gets bigger it might start taxing on the RAM and speed of the program.

Could someone point me in the right direction of how it could be done better, making it overall faster? Or is the way I'm doing it fine and I'm just being skeptical?
Edited on 12 September 2017 - 10:51 PM
Bomb Bloke #2
Posted 13 September 2017 - 01:40 AM
It uses OOP (metatables and object-based function calling). Because of this, 'os.loadAPI' isn't working.

That won't prevent you from using os.loadAPI(). Case in point: ComputerCraft's own vector API.

Could someone point me in the right direction of how it could be done better, making it overall faster? Or is the way I'm doing it fine and I'm just being skeptical?

The current 1.80 beta builds offer require, but if you want backwards compatibility, I suggest sticking with os.loadAPI().
KingofGamesYami #3
Posted 13 September 2017 - 02:13 AM
I think the best way is to support both ways. For example, stitch can be loaded as an API or returned by dofile.
Dave-ee Jones #4
Posted 13 September 2017 - 02:53 AM
Okay. Let me explain why os.loadAPI doesn't seem to work. Whenever I load the API as, say "api" and then try calling a metatable method like so:

local api = os.loadAPI("/api")
local button1 = api.BUTTON:new()
button1:draw()
It goes pretty mental. What am I doing wrong?

EDIT: Stitch doesn't have any metatable OOPing from what I can see, either, so it doesn't really help your case..
Edited on 13 September 2017 - 12:54 AM
KingofGamesYami #5
Posted 13 September 2017 - 03:26 AM
You can't have a metatable on the API itself, b/c os.loadAPI does this. That doesn't mean you can't use metatable OOP at all; it just means the API itself cannot be assigned a metatable.

In the case of Stitch, it does not use metatables because they are very slow (compared to non), and therefor not well suited to time-sensitive graphical work.

For your specific example, I see no reason why it would not work.


button = {}

setmetatable( button, {__index = {
  new = function()
     print( "Hello, World" )
  end,
  },
} )


os.loadAPI( "api" )
api.button:new() --# outputs "Hello World"
Dave-ee Jones #6
Posted 13 September 2017 - 04:01 AM
For your specific example, I see no reason why it would not work.


button = {}

setmetatable( button, {__index = {
  new = function()
	 print( "Hello, World" )
  end,
  },
} )


os.loadAPI( "api" )
api.button:new() --# outputs "Hello World"

Huh. Well waddaya know, it actually does work now. I wonder why it didn't before..
I changed the API so much it could've been anything. :blink:/> Thanks for the help though.

I'll use the 'os.loadAPI' method, since it works now. However, I still don't like having to call things like this:

os.loadAPI("api")
local button = api.BUTTON:new(...)
button:draw()

Just makes it confusing for the people who want to use the API for their own programs.
Edited on 13 September 2017 - 02:01 AM