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

How do I find if an API is loaded without forcing location to load from

Started by Termanater13, 16 July 2013 - 01:29 PM
Termanater13 #1
Posted 16 July 2013 - 03:29 PM
I need a way to detect if a API is loaded. I'm not looking forward to force loading if it is not loaded I just need to detect if it is loaded.
for exaple

if (api1 isloaded) then
   some code if api1 is present
else
   print("api1 not found I cant do watever till its there. enable it or download it here:...")
end
I know I can forceload it but if it not there it will throw an error. I have looked at the wiki but I cant find anything to see if it is loaded.
theoriginalbit #2
Posted 16 July 2013 - 04:33 PM
In Lua when a nil value is present in a conditional it is evaluated as false, just the same as if a value is present it is evaluated to true (unless the value is false of course)

This means we can check for APIs in this way

if http then
  print( "The HTTP API is loaded" )
else
  print( "The HTTP API is not loaded" )
end
Termanater13 #3
Posted 16 July 2013 - 05:17 PM
so if I use the API name it will return true if it's loaded otherwise it won't do anything?
immibis #4
Posted 16 July 2013 - 05:24 PM
The API name returns the API itself if it's loaded, or nil if it's not. Same as any variable.

In an if statement, anything that's not nil or false is considered the same as true:

x = nil
if x then
   -- x is nil or false so this won't run
   print("You won't see this")
else
   -- which means this will (because that's how else works)
   print("You will see this")
end


x = "asdfjkl"
if x then
   -- x isn't nil or false so this will run
   print("You will see this")
else
   -- which means this won't (because that's how else works)
   print("You won't see this")
end