Jump to content




A couple of questions about APIs


16 replies to this topic

#1 samdeman22

  • Members
  • 101 posts
  • LocationScotland!

Posted 16 October 2012 - 09: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...

#2 Luanub

    Lua Nub

  • Members
  • 1,135 posts
  • LocationPortland OR

Posted 16 October 2012 - 09: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.

#3 samdeman22

  • Members
  • 101 posts
  • LocationScotland!

Posted 16 October 2012 - 09:14 PM

thanks, so if I call the table "table_G" it will work?

#4 Luanub

    Lua Nub

  • Members
  • 1,135 posts
  • LocationPortland OR

Posted 16 October 2012 - 09:16 PM

To access the global table it is just _G{}

#5 Ditto8353

  • New Members
  • 138 posts

Posted 16 October 2012 - 09:17 PM

View Postsamdeman22, on 16 October 2012 - 09:06 PM, said:

...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.

#6 samdeman22

  • Members
  • 101 posts
  • LocationScotland!

Posted 16 October 2012 - 09:19 PM

"_G{}" has to have underline and all?.. I'm a bit simple.

View PostDitto8353, on 16 October 2012 - 09:17 PM, said:

View Postsamdeman22, on 16 October 2012 - 09:06 PM, said:

...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.

#7 Luanub

    Lua Nub

  • Members
  • 1,135 posts
  • LocationPortland OR

Posted 16 October 2012 - 09: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.

#8 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 16 October 2012 - 09:23 PM

No, no, no. His second example works perfectly well.

#9 faubiguy

  • Members
  • 213 posts

Posted 16 October 2012 - 09: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).

#10 samdeman22

  • Members
  • 101 posts
  • LocationScotland!

Posted 16 October 2012 - 09: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?

#11 samdeman22

  • Members
  • 101 posts
  • LocationScotland!

Posted 16 October 2012 - 09: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.)

#12 Doyle3694

  • Members
  • 815 posts

Posted 16 October 2012 - 09: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/>

#13 MysticT

    Lua Wizard

  • Members
  • 1,597 posts

Posted 16 October 2012 - 10:32 PM

The function is fine, just check how you're calling it, maybe you passed a number as first argument.

View PostDoyle3694, on 16 October 2012 - 09:58 PM, said:

and also, it's string.len(str) :P/>
It's easier to use #str, does the same with less typing :D/>

#14 Doyle3694

  • Members
  • 815 posts

Posted 16 October 2012 - 10:34 PM

oh, Someone told me # only worked on tables :S

#15 samdeman22

  • Members
  • 101 posts
  • LocationScotland!

Posted 16 October 2012 - 11:06 PM

orright. thanks, that works now.

#16 samdeman22

  • Members
  • 101 posts
  • LocationScotland!

Posted 16 October 2012 - 11:07 PM

View PostDoyle3694, on 16 October 2012 - 09:58 PM, said:

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. :/

#17 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 17 October 2012 - 12:00 AM

View Postsamdeman22, on 16 October 2012 - 09:33 PM, said:

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"]).





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users