Jump to content




Table In An Api


  • You cannot reply to this topic
6 replies to this topic

#1 grand_mind1

  • Members
  • 207 posts

Posted 10 October 2013 - 11:43 PM

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

#2 jay5476

  • Members
  • 289 posts

Posted 11 October 2013 - 12: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

#3 Engineer

  • Members
  • 1,378 posts
  • LocationThe Netherlands

Posted 11 October 2013 - 01: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

#4 AgentE382

  • Members
  • 119 posts

Posted 11 October 2013 - 10: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.

#5 grand_mind1

  • Members
  • 207 posts

Posted 11 October 2013 - 05: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.

#6 Engineer

  • Members
  • 1,378 posts
  • LocationThe Netherlands

Posted 11 October 2013 - 06: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


#7 grand_mind1

  • Members
  • 207 posts

Posted 11 October 2013 - 06:54 PM

Oh! oops! Thanks so much!





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users