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

[Lua] [API] [Help]API Help

Started by ChaddJackson12, 22 October 2012 - 10:52 PM
ChaddJackson12 #1
Posted 23 October 2012 - 12:52 AM
Please help me, I have this error: Attempt to index ? (A boolean value)

I have this type of code:
API File: (Name: test)

printText = "Text"

The Actual File:

os.loadAPI("test")

print(test.printText())

I have alaso tried just test.printText, and without the Print() around it, nothing works, but that doesn't work. I get the error on line 3 of the actual file. Please help me. (I am using ComputerCraft 1.45)
ChunLing #2
Posted 23 October 2012 - 01:23 AM
"test.printText" isn't a function. It's a table entry in test indexed under ["printText"]… or would be if you'd given it local scope within test. Try print(printText) and see what happens, cause I think that you declared it globally.
ChaddJackson12 #3
Posted 23 October 2012 - 01:59 AM
"test.printText" isn't a function. It's a table entry in test indexed under ["printText"]… or would be if you'd given it local scope within test. Try print(printText) and see what happens, cause I think that you declared it globally.
When you load an api, with os.loadAPI("filename") you should be able to do: filename.[function/value]. Also, I tried that, and it was nil.
ChunLing #4
Posted 23 October 2012 - 02:05 AM
Try making it local in test, still.
Orwell #5
Posted 23 October 2012 - 02:58 AM

os.loadAPI("test")
print(test.printText)

works for (without the brackets after test.printText, it's a string not a function). Declaring it as local actually wouldn't work, that's the point of the local scope.
ChunLing #6
Posted 23 October 2012 - 05:38 PM
But all the functions in the regular APIs are local to the API.

Oh, here's a thought, load API only puts the functions into the table, it doesn't put any of the normal variables in. So maybe if printText were a function rather than a string, it would be there?
Lyqyd #7
Posted 23 October 2012 - 05:45 PM
ChunLing, you are completely wrong in several posts in this topic. If you don't know what you're talking about, please don't try to help.

OP, did Orwell's suggestion work for you?
ChunLing #8
Posted 23 October 2012 - 11:07 PM
Okay, but where am I completely wrong? The standard APIs that are written in lua do define their functions as local. And printText does need to be a function rather than a string if it is to be called as a function.
Lyqyd #9
Posted 24 October 2012 - 12:02 AM
Okay, but where am I completely wrong? The standard APIs that are written in lua do define their functions as local. And printText does need to be a function rather than a string if it is to be called as a function.

If you actually read through the APIs included with ComputerCraft, you will find that functions which are declared as local are not available outside of the API. Suggesting making the value local will not help fix the problem.
ChunLing #10
Posted 24 October 2012 - 04:18 AM
Oh…Huh. I never noticed that before. So my instinct of just making functions available by declaring them globally wasn't wrong.

And I worked so hard to suppress that urge too. Oh well, I like putting functions into the global space for later use.
Cloudy #11
Posted 24 October 2012 - 10:12 AM
Oh…Huh. I never noticed that before. So my instinct of just making functions available by declaring them globally wasn't wrong.

And I worked so hard to suppress that urge too. Oh well, I like putting functions into the global space for later use.

A global is fine in an API - you should avoid in your program though. But if you need to use it you need to use it.
ChunLing #12
Posted 24 October 2012 - 11:22 AM
Yeah…I was kinda thinking of my program as somewhere in between a program and an API loader, it's full of stuff that I probably should put in an API since I love it so much, but I wanted to have it all in a single file for ease of use. Then I went to the effort of getting it all local, and I guess my brain got stuck that way.