1

So without the context, I have a method that returns a table. Trying to print it out, it seems that the table mainly contains methods to be called. However not being very knowledgeable in LUA, i have no idea how to properly get some information about these methods i should call.

I tried to get documentation from the creator of the thing, but there is none as far as i know. And since this is inside computercraft (minecraft mod) i don't have a lot of features to rely on either.

So knowing only that it is a table with methods that i can call, how do i properly figure out how to call them, and with what parameters etc. ?

Smileynator
  • 677
  • 8
  • 24
  • As other have answered, as far as Lua is concerned a function is value that you can call. Each time you call it, you can pass any number of parameters of any type. It's up to the function to make sense of or reject what it is passed. It can then, each time it is called, return any number of values of any type, based on what it was passed or outside state (sibling value in the same table, day of week, existence of a file, …). It's up to the caller to make sense of the return values. It can also alter outside state. The only way to know how to use a function and what it does is documentation. – Tom Blodget Feb 27 '16 at 23:34

3 Answers3

2

Just knowing the names of the methods is not enough to figure how to call them.

Their names may be a clue, but there is no guarantee.

If the methods are written in Lua, you could use the debug API to discover the names of parameters, which again may just be an indication of how to call the methods.

Bottom line: you need documentation or example code.

lhf
  • 70,581
  • 9
  • 108
  • 149
2

Generally speaking, modules/ libraries always come with docs, or a method to print the docs.

But if this is not the case, here's what you can do:

  • You can print everything in the table! This is a must, the names of the methods can be very useful
  • You can seek out help! Find people who have used the same module, and ask them how it works. Why solve something others have already figured out?
  • Use debug.getinfo and other hacky functions for the debug library! They can provide more info than anything else in the Lua standard libraries!
  • C-Side coding can reveal what Lua cannot! If you have access to the C-Side you can see exactly what the code is doing (or at least I think so)
  • Check out the source code! This shows you what the code does and how it does it
  • And above all else, experiment! Try the methods on different parameters, different values, and identify what it does through continuous testing!
warspyking
  • 3,045
  • 4
  • 20
  • 37
0

I knew this existed, didn't know how it worked. So for future reference: You can dump your peripherals and methods by doing /op_dump in the minecraft chat.

This generates an XML which writes out all methods it has found in peripherals OR objects/tables.

This means that you have to call every interesting method once, which generates the table as return. And then calling /op_dump will include that newly encountered object with all information about there methods/parameters etc.

Smileynator
  • 677
  • 8
  • 24