Jump to content
  • Sky
  • Blueberry
  • Slate
  • Blackcurrant
  • Watermelon
  • Strawberry
  • Orange
  • Banana
  • Apple
  • Emerald
  • Chocolate
  • Charcoal
tlf30

Library Help

Recommended Posts

Hello, I am somewhat new to openComputers and need a little help with how libraries work.

 

I have read the library tutorial and that was very helpful.

 

My library is structured like so

local lib = {}

function lib.fun1() {
    print("It worked!")
}

return lib

I placed the lib in the /lib folder under the name libname.lua, and made a program that used the lib like so:

local l0 = require("libname")
l0.fun1()

But I am getting the error:

/lib/test.lua:2: attempt to index local 'libname' (a boolean value)

 

I am not sure what I am doing wrong and would love some help.

 

Thanks,

tlf30

Link to post
Share on other sites
function lib.fun1() {
print("It worked!")
}

 

That's not valid Lua. So it'll fail loading the library - you can use

local lib, reason = require("libname")
if not lib then print(reason) return end

To print the error and "soft fail", or

local lib = assert(require("libname"))

to generate an error if the lib fails to load.

 

You may need to reboot when changing the library or manually unload it (package.loaded["libname"] = nil) or the system will re-use a cached version.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use and Privacy Policy.