I am adding ComputerCraft support to my machines, and I have a set of functions already callable on them. However, the calling and the received data...their formatting leaves something to be desired. Is there a better way to do this?
local rotaryMachine = peripheral.wrap("right")
local slotData = rotaryMachine.getSlot(0)
--is the return a table or a string?
for i,v in pairs(slotData) do print(i..": "..v) end
It returns a table. In the case of getSlot, it is [itemID][metadata][stackSize][displayName]. My concern is, how will a program actually parse and use this?Excited about the computercraft support for your mods!
Not sure exactly what you mean with your question so I am making an assumption. Feel free to ignore if the assumption turns out wrong (in which case what is wrong with the formatting?)
You can always wrap your peripherals like so (assuming its on the right hand side of the computer for now). Makes it easier to call peripheral functions.
Code:local rotaryMachine = peripheral.wrap("right") local slotData = rotaryMachine.getSlot(0) --is the return a table or a string? for i,v in pairs(slotData) do print(i..": "..v) end
That might not work properly inside the Lua shell (putting it in a do end block might help there though)
[/assumption]
I'm not yet familiar enough with your mods to know what sorts of programs one might make for rotarycraft blocks. But the stackSize and displayName variables will most likely be very useful. So long as the keys in the table have decent, consistent string names it will probably be really easy to use. I have used similar openperipherals methods that return a very similar table to your getSlots method, and I have had no problem using those and some of the programs I made were quite useful, so I expect there will be plenty of uses for your methods.It returns a table. In the case of getSlot, it is [itemID][metadata][stackSize][displayName]. My concern is, how will a program actually parse and use this?
Keys in the tables...? Tables are basically arrays, correct?I'm not yet familiar enough with your mods to know what sorts of programs one might make for rotarycraft blocks. But the stackSize and displayName variables will most likely be very useful. So long as the keys in the table have decent, consistent string names it will probably be really easy to use. I have used similar openperipherals methods that return a very similar table to your getSlots method, and I have had no problem using those and some of the programs I made were quite useful, so I expect there will be plenty of uses for your methods.
Yes. Something ideal for the table returned by getSlot written out in Lua would look like thisKeys in the tables...? Tables are basically arrays, correct?
slotReturn = {
["id"] = id,
["metadata"] = metadata,
["stackSize"] = stackSize,
["itemName"] = itemName,
}
How do I do that? All the CC API asks me for is to return an array of objects, in this case three integers and a string.Yes. Something ideal for the table returned by getSlot written out in Lua would look like this
Code:slotReturn = { ["id"] = id, ["metadata"] = metadata, ["stackSize"] = stackSize, ["itemName"] = itemName, }
Parsing that would be fine.
I'm sorry I don't know I haven't made a mod, just someone who likes to play. Maybe someone from the open peripherals team could help because some of their table returns have string keys.How do I do that? All the CC API asks me for is to return an array of objects, in this case three integers and a string.
This is though CC directly, but OK.I'm sorry I don't know I haven't made a mod, just someone who likes to play. Maybe someone from the open peripherals team could help because some of their table returns have string keys.
Sorry, do you mean you want to use the Lua in computercraft to look through the table in a computercraft program? If so then it is done this way. Just make a new program and do something like thisThis is though CC directly, but OK.
local rotaryCraftMachine = peripheral.wrap("right")
local slotData = rotaryCraftMachine.getSlot(0)
--print the whole table out by key and value using generic for loop. Key is stored in i and value in v
for i,v in pairs(slotData) do
print(i..": "..v) -- .. is concatenate in Lua. Will print out something that looks like this, itemName: steel, but for each key value pair in the table
end
--store the different parts of the table as variables (assuming key names are what I put in the previous post. They can obviously be corrected.) Keys go inside square brackets
local slotID = slotData["id"]
local slotMeta = slotData["metadata"]
local slotStack = slotData["stackSize"]
local slotItem = slotData["itemName"]
--You can do whatever you want with those variables.
If your just returning an array of objects.. return a 2 column arrayHow do I do that? All the CC API asks me for is to return an array of objects, in this case three integers and a string.
Now to see if CC accepts this as a valid return.If your just returning an array of objects.. return a 2 column array
{
[ "itemID", 30761],
[ "metadata", 42],
["stackSize", 64],
["displayName", "Yeast"]
}
This has the nice feature to be (almost) infinitely extensible so you dont have to revamp your API if you want to return more or less items
computercraft: Could not convert object of type [Ljava.lang.Object; to LuaValue
Again, this is CC directly. Also, the javadoc says this:Maybe it would prefer a Map of strings and doubles/strings? That's the sense I get from looking around https://github.com/OpenMods/OpenPeripheral/tree/master/src/openperipheral/converter.
Returns:
An array of objects, representing values you wish to return to the lua program.
Integers, Doubles, Floats, Strings, Booleans and null be converted to their corresponding lua type.
All other types will be converted to nil.
You may return null to indicate no values should be returned.
Yes, I know that - that is what I am doing. The problem is one of usability of the data. A pile of unlabeled numbers is not user-friendly.Is it your mod that need to return data to CC so the user can read it? Then you just make an array and in each field in the array you put the object. Your object can contain all datatypes that CC accepts. You dont need to use Lua in your mod.
Yes, I know that - that is what I am doing. The problem is one of usability of the data. A pile of unlabeled numbers is not user-friendly.
??Then you need to make one more property in your object that tells what this is (like Name or something like that). An API is not supposed to be user-friendly becuase its only data.
??
Are you suggesting alternating the data with Strings that label them? That is no more user-friendly.