问题
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. ?
回答1:
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!
回答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.
回答3:
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.
来源:https://stackoverflow.com/questions/35670456/table-with-methods-how-to-handle-inspect-them