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

Table In An Api

Started by grand_mind1, 10 October 2013 - 10:43 PM
grand_mind1 #1
Posted 11 October 2013 - 12:43 AM
Hey! Just a real quick question!
I was just wondering if there is a way to access a table which is inside an API that the program has loaded.
I tried to blindly put the API's handle in front of the table like this:

test = table.hello["hi"]
However, this did not work the way I had hoped.
Help is appreciated!
Thanks! :D/>
jay5476 #2
Posted 11 October 2013 - 01:18 AM
if you use dofile(filename) then if the variable is global you can use it but i guess you could also load the file and then put all variables into a table
Engineer #3
Posted 11 October 2013 - 02:01 AM
The table in the api must be global, it mustnt have the local keyword.
If you load it with os.loadapi, then you can do:
apiname.tablename.key
AgentE382 #4
Posted 11 October 2013 - 11:17 AM
If your API looks like this:
settings = {}
function doStuff()
	-- Use settings in API
end
Asuming the API is saved in a file named "myapi", you can do:
os.loadAPI "myapi"
myapi.settings["option1"] = true
myapi.doStuff()

If your API looks like this:
local settings = {}
-- <rest of API here>

Then you can't directly access the table.
grand_mind1 #5
Posted 11 October 2013 - 06:55 PM
Hmmm ok well I tried what you've told me however I can't seem to get it to work. I'm trying to use this for my button API which you can view here:
http://pastebin.com/mhrJyz1X
When I first posted the topic I did have "local" in front of my table however removing it has made no difference.
I'm working on a potion shop with this code:
http://pastebin.com/aPteuSAP
And I'm trying to access the table on line 55 like this:

color = btn.button["name"]["color"]
Color being defined as a variable for the function.
I get the following error:

PS:55: attempt to index ? (a nil value)
I'm sure I'm just messing something up so if someone could tell me what it is, that would be great.
Engineer #6
Posted 11 October 2013 - 07:17 PM
That is because your function only goes to the "name" index.
Here, seek the difference! :P/>

local function potion(name)
        color = btn.button["name"]["color"]
        if name ~= "speed" and name ~= "poison" then

        end
end


local function potion(name)
        color = btn.button[name]["color"] -- NOTHING TO SEE HERE :D/>
        if name ~= "speed" and name ~= "poison" then

        end
end
grand_mind1 #7
Posted 11 October 2013 - 07:54 PM
Oh! oops! Thanks so much!