1522 posts
Location
The Netherlands
Posted 13 March 2013 - 09:30 AM
Hi there, the other the day I was thinking about this topic and now I have some questionmarks.
First of all I went of course to the wiki ( doesn't do everyone that? ):
os.loadAPI( name )
Loads a Lua script as an API in it's own namespace (see example). It will be available to all programs that run on the terminal.
So note the variable must be
name not
path. So let's say we are in the same folder as the API then we use:
os.loadAPI( "myfunction" )
print( myfunction.printsomething() )
os.unloadAPI( "myfunction" )
And if I am not in the same folder, how would I use it then?
I think this is wrong:
os.loadAPI( "somedir/myfunction" )
print( somedir/myfunction.printsomething() ) -- This is where it confuses me
os.unloadAPI( "somedir/myfunctions" )
So if you guys can help me with this, that would be very appreciated :)/>
Thanks, Engineer
758 posts
Location
Budapest, Hungary
Posted 13 March 2013 - 09:45 AM
In bios.lua:
function os.loadAPI( _sPath )
local sName = fs.getName( _sPath )
...
So no, it'd be myfunction either way.
1522 posts
Location
The Netherlands
Posted 13 March 2013 - 09:54 AM
In bios.lua:
function os.loadAPI( _sPath )
local sName = fs.getName( _sPath )
...
So no, it'd be myfunction either way.
Thank you! They really should change it in the wiki to path if you ask me. Just to prevent confusion, or a better description will do. Maybe my English isn't good enough to understand it quite but these are my thoughts.
Thanks again :)/>
1604 posts
Posted 13 March 2013 - 10:03 AM
You have to pass the full path to the api file as the argument, since it doesn't use the shell directory. It will use only the file name as the api name, so you can put it in any folder and it should work.
148 posts
Posted 13 March 2013 - 10:15 AM
os.loadAPI("path/name")
os.unloadAPI("name")
(As far as I remember…) loadAPI will use loadfile to load the api (so you need the path for it to find the right file) and inserts it into the global envirment as the last component of the path, the name.
unloadAPI will delete the index in the global enviroment and because the index was set to the name by loadAPI unloadAPI only needs the name.
7508 posts
Location
Australia
Posted 13 March 2013 - 11:17 AM
(As far as I remember…) loadAPI will use loadfile to load the api
Nope its more than just a
loadfile, its actually functionally closer to
dofile.
From bios.lua
Spoiler
local tAPIsLoading = {}
function os.loadAPI( _sPath )
local sName = fs.getName( _sPath )
if tAPIsLoading[sName] == true then
printError( "API "..sName.." is already being loaded" )
return false
end
tAPIsLoading[sName] = true
local tEnv = {}
setmetatable( tEnv, { __index = _G } )
local fnAPI, err = loadfile( _sPath )
if fnAPI then
setfenv( fnAPI, tEnv )
fnAPI()
else
printError( err )
tAPIsLoading[sName] = nil
return false
end
local tAPI = {}
for k,v in pairs( tEnv ) do
tAPI[k] = v
end
_G[sName] = tAPI
tAPIsLoading[sName] = nil
return true
end
1604 posts
Posted 13 March 2013 - 11:51 AM
(As far as I remember…) loadAPI will use loadfile to load the api
Nope its more than just a
loadfile, its actually functionally closer to
dofile.
From bios.lua
Spoiler
local tAPIsLoading = {}
function os.loadAPI( _sPath )
local sName = fs.getName( _sPath )
if tAPIsLoading[sName] == true then
printError( "API "..sName.." is already being loaded" )
return false
end
tAPIsLoading[sName] = true
local tEnv = {}
setmetatable( tEnv, { __index = _G } )
local fnAPI, err = loadfile( _sPath )
if fnAPI then
setfenv( fnAPI, tEnv )
fnAPI()
else
printError( err )
tAPIsLoading[sName] = nil
return false
end
local tAPI = {}
for k,v in pairs( tEnv ) do
tAPI[k] = v
end
_G[sName] = tAPI
tAPIsLoading[sName] = nil
return true
end
He ment that it uses loadfile (and it does), and loadfile needs a full path, so os.loadAPI also needs it.
1522 posts
Location
The Netherlands
Posted 13 March 2013 - 12:11 PM
Thanks for clearifying that up. All I need to know if I needed to be in the same folder ^^
Thanks though :)/>