99 posts
Location
Scotland!
Posted 16 October 2012 - 10:06 PM
firstly - when you want to use a function from the api IN the api do you have to add the file prefix? i.e ( I made centerPrint() in the sm api, when using it again, within the function do I have to call it sm.centerPrint() ?
second - can I create a table in the sm api that can be called in other programs? i.e. (a table like
cost = { [dirt] = 1, [cobblestone] = 1, [diamond] = 8192 }
to then be called in another program like:
cost = sm.cost[itemname]
thanks guys…
1111 posts
Location
Portland OR
Posted 16 October 2012 - 10:10 PM
So for the first, No you do not need the file prefix in the function names in the API. Just do that as you normally would, only don't make them local.
For the second question I think you can place them in the global table _G and access them from any program. I've never had a need to do that so I'm not 100% sure.
99 posts
Location
Scotland!
Posted 16 October 2012 - 10:14 PM
thanks, so if I call the table "table_G" it will work?
1111 posts
Location
Portland OR
Posted 16 October 2012 - 10:16 PM
To access the global table it is just _G{}
136 posts
Posted 16 October 2012 - 10:17 PM
…can I create a table in the sm api that can be called in other programs? i.e. (a table like
cost = { [dirt] = 1, [cobblestone] = 1, [diamond] = 8192 }
to then be called in another program like:
cost = sm.cost[itemname]
Provided that the API 'sm' has been loaded, you should be able to do this.
As per the wiki article on the os API, once an API has been loaded, it will be available to all programs running on that computer.
99 posts
Location
Scotland!
Posted 16 October 2012 - 10:19 PM
"_G{}" has to have underline and all?.. I'm a bit simple.
…can I create a table in the sm api that can be called in other programs? i.e. (a table like
cost = { [dirt] = 1, [cobblestone] = 1, [diamond] = 8192 }
to then be called in another program like:
cost = sm.cost[itemname]
Provided that the API 'sm' has been loaded, you should be able to do this.
As per the wiki article on the os API, once an API has been loaded, it will be available to all programs running on that computer.
ok, I guess I should just test then.
1111 posts
Location
Portland OR
Posted 16 October 2012 - 10:21 PM
Yeah I think you would do something like this
_G = { [dirt] = 1, [cobblestone] = 1, [diamond] = 8192 }
EDIT: Another option if neither method works is to serialize the table and save it in a file then have the other program read the file and unserialize it.
8543 posts
Posted 16 October 2012 - 10:23 PM
No, no, no. His second example works perfectly well.
231 posts
Posted 16 October 2012 - 10:27 PM
You shouldn't need to mess with _G directly. os.loadAPI puts the API in _G for you. Any objects or values in the api are put into the sm namespace (which is really just a table, but that doesn't matter).
99 posts
Location
Scotland!
Posted 16 October 2012 - 10:33 PM
ok, when I try to load the api with the table in it I get "sm:3: table index expected, got nil"
the table looks exactly like this
cost = { [dirt] = 1, [plank] = 8, [diamond] = 8192 }
is that how you use strings as the index or am I missing something?
99 posts
Location
Scotland!
Posted 16 October 2012 - 10:45 PM
its a success! I can call the table just by using "sm.cost[input]"
but now something else is wrong… in sm.centerPrint( str, ypos )
the function goes like this
local w,h = term.getSize()
function centerPrint( str, ypos )
term.setCursorPos( w / 2 - (#str / 2), ypos )
term.write( str )
end
when I try to use it it says " sm:8: attempt to get length of number" whats wrong here? ( I've tried it without the brackets on "#str / 2" it still doesn't work.)
818 posts
Posted 16 October 2012 - 10:58 PM
put the
local w,h = term.getSize()
inside the function first. then make sure when you call teh function you pass it a string. and also, it's string.len(str) :D/>/>
1604 posts
Posted 16 October 2012 - 11:32 PM
The function is fine, just check how you're calling it, maybe you passed a number as first argument.
and also, it's string.len(str) :P/>/>
It's easier to use #str, does the same with less typing :D/>/>
818 posts
Posted 16 October 2012 - 11:34 PM
oh, Someone told me # only worked on tables :S
99 posts
Location
Scotland!
Posted 17 October 2012 - 12:06 AM
orright. thanks, that works now.
99 posts
Location
Scotland!
Posted 17 October 2012 - 12:07 AM
put the local w,h = term.getSize() inside the function first. then make sure when you call teh function you pass it a string. and also, it's string.len(str) :D/>/>
thanks that works, but for some reason using #str ( which I also thought did the same thing ) doesn't work. :/
8543 posts
Posted 17 October 2012 - 01:00 AM
ok, when I try to load the api with the table in it I get "sm:3: table index expected, got nil"
the table looks exactly like this
cost = { [dirt] = 1, [plank] = 8, [diamond] = 8192 }
is that how you use strings as the index or am I missing something?
That's how you'd use variable values as the indices. Try removing the square brackets or putting quotes inside them (either dirt or ["dirt"]).