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

API/Library errors (a function value) error - RESOLVED

Question

So, from what I am understanding this "should" work but I keep getting the the following error: attempt to index local 'test' (a function value) error.

 

here is the "test" (main) file

local test = require("testAPI")
local term = require("term")

term.write("This is NOT from the api!")
test.fred("And this is!")

And here is the "testAPI.lua" file:

local term = require("term")

function fred(text)
  term.write(text)
end

function george()
  term.write("should not see this unless george was called!")
end

and when I run it I get:

/# test
This is NOT from the API!
/test:5: attempt to index local 'test' (a function value)
stack traceback:
        /test:5: in main chunk
       (...tail calls...)

Basically, I would like to create an API/Library with various functions in them so I can call the separate functions as needed. What am I doing wrong to prevent this error?

 

p.s., I did look at this tutorial and tried to use a table with a different error altogether.

Link to post
Share on other sites

3 answers to this question

Recommended Posts

  • 0
  • Solution

Will do, and thank you for a reply!

 

I guess the issue I was having when I tried using them in a table is that I just couldn't get the functions to work (I.E. threw an error while trying to call them).

 

When they are in a table like that, how would you call them? would it be:

local test = require("testAPI")
local term = require("term")

test.fred("Some Text!")

Or would it be:

local test = require("testAPI")
local term = require("term")

fred("Some Text!")

That was the issue I was having when trying to put them into a table.

 

EDIT:  Ugh, I really need to just look at what I am doing to answer my own questions. It is the first option. Sorry for being noobish and again, thank you for all the help!

Link to post
Share on other sites
  • 0

Even though I do love OC, it seems either the boards are either not very active or simply nobody knew the answer. Nonetheless, I have stumbled across the answer through experimentation.

 

It seems that with OC, once the library is loaded in the "main" script (the 'local api = require("api")' bit), you don't need to make calls to your library directly, but simply just call the function itself. Using my example 'test' above:

 

test.fred("And this is!") -- calling the 'testAPI.lua' this way will throw the error but....

fred("And this is!")  -- properly calls the function 'fred' from the 'testAPI.lua' file.

 

So, I hope this helps others out that had this issue and thus it should go without saying but if you create and use multiple API's/Libraries to make sure each function name is unique!

 

On a side note, it is important that if you make changes to the API/Library file, you MUST reboot the computer for the changes to take effect...now if you will excuse me, I have a bald spot to tend to before I figured this out....

Link to post
Share on other sites
  • 0

I would really reread that tutorial if I were you. Now your library cannot be unloaded, since it is placed into the global environment. That is why you need to use a table and return that at the end, like this:

local term = require("term")

local test = {}

function test.fred(text)
  term.write(text)
end

function test.george()
  term.write("should not see this unless george was called!")
end

return test
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
Answer this question...

×   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.