202 posts
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/>
331 posts
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
1522 posts
Location
The Netherlands
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
131 posts
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.
202 posts
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/mhrJyz1XWhen 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/aPteuSAPAnd 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.
1522 posts
Location
The Netherlands
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
202 posts
Posted 11 October 2013 - 07:54 PM
Oh! oops! Thanks so much!