Difference between revisions of "Os.loadAPI"

From ComputerCraft Wiki
Jump to: navigation, search
(Created the page)
 
m
 
(2 intermediate revisions by 2 users not shown)
Line 8: Line 8:
 
|desc=Loads a user created API from a file located at <var>path</var>.
 
|desc=Loads a user created API from a file located at <var>path</var>.
 
<br/>
 
<br/>
This function runs the file, collects all global (non-localized) variables from it and puts them into a [[table_(type)|table]] into the global (<var>_G</var>) environment. The name of the table becomes the name of the file.
+
This function runs the file, collects all global (non-localized) variables from it and puts them into a [[table_(type)|table]] into the global (<var>_G</var>) environment. The name of the table in the _G table becomes the name of the file.
 
|examples={{Example
 
|examples={{Example
 
  |desc=Loads an API saved at "''test/myAPI''" and runs the global function "''hello''" from it.
 
  |desc=Loads an API saved at "''test/myAPI''" and runs the global function "''hello''" from it.
Line 31: Line 31:
 
* By default APIs '''cannot''' access [[shell_(API)|shell API]] and [[multishell_(API)|multishell API]].
 
* By default APIs '''cannot''' access [[shell_(API)|shell API]] and [[multishell_(API)|multishell API]].
 
}}
 
}}
 +
[[Category:API Functions]]

Latest revision as of 07:59, 4 August 2020


Grid Redstone.png  Function os.loadAPI
Loads a user created API from a file located at path.


This function runs the file, collects all global (non-localized) variables from it and puts them into a table into the global (_G) environment. The name of the table in the _G table becomes the name of the file.
Syntax os.loadAPI(string path)
Returns boolean did the API load successfully?
Part of ComputerCraft
API OS

Examples

Grid paper.png  Example
Loads an API saved at "test/myAPI" and runs the global function "hello" from it.
Code
--test/myAPI:

local text = "Hello World!" -- this won't be seen from outside of the API because it is localized

function hello () -- this will be seen from outside the API because it is not localized
  print(text)
end
--The program that will use "myAPI"

os.loadAPI("test/myAPI") -- load the API

myAPI.hello() -- runs the function "hello" from the API
Output Hello World!

Additional Notes

  • path must be an absolute path.
  • All variables which will be needed to be accessed through <apiname>.<variable/function> must be global.
  • By default APIs cannot access shell API and multishell API.