This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Sorcelator's profile picture

Learning Openperipheral - Some Questions

Started by Sorcelator, 12 September 2013 - 04:01 PM
Sorcelator #1
Posted 12 September 2013 - 06:01 PM
I've been playing around with OpenPeripheral and I'm really excited about what its capable of. My main goal right now is to build a system that can A.) Modify my inventory with a PIM to unload items after mining B.) Modify items contained in a chest to either be auto-crafted or macerated / smelted.

Currently I'm working on getting the auto macerate / smelting going. I'm kind of stabbing blindly so I'm sure there are posts about how to do exactly this. By doing this my self though I hope to have a better understanding of what is capable with OP.

Right now I'm simply exploring what I can do with items in a chest. I've created a simple script that will run through each slot in a chest and dump its meta data into a table and print it.

slotnum = chest.getSizeInvantory()
chest.condense()
for i=,slotnum do
local tableInfo = chest.getStackInSlot(i)
for key, value in pairs(tableInfo) do
   print(key .. " = " .. tostring(value))
end
print()
end
By running this I get a big dump of information, and I obviously can't read it all. However, it also bombs out when it hits an empty slot in a container with the error "script:5: bad argument: table expected, got nil"

Is there a way to avoid this hitch and have it chug along with identifying the rest of the items?
What would be your preferred method of dumping this information and making it a readable file? I know that you can use fs.open to create a file and write to it, but I haven't messed with it just yet.

Thanks!
immibis #2
Posted 12 September 2013 - 06:24 PM
Don't run the for loop if tableInfo is nil?
Sorcelator #3
Posted 12 September 2013 - 09:47 PM
Don't run the for loop if tableInfo is nil?

Yeah I didn't realize that right away. This is what I have so far.


chest.condense() --Organize the Chest
local slotnum = chest.getSizeInventory() --Use the Max Invantory Size to calculate the Slot number and Loop amount
for i=1,slotnum do -- LOOP!
local data = fs.open("data.db", "w")
local tableInfo = chest.getStackInSlot(i)
print(i)
print(slotnum)
if tableInfo ~= nil then
  for key, value in pairs(tableInfo) do
   data.writeLine(key .. " = " .. tostring(value)) -- Dump table data in to the Data.db
  end
   data.writeLine(" ") -- Add a line to make it pritty.
   data.close() -- Save the file.
  else
end
end

I'm trying to get it to write all the data it creates to a file… but right now it never seems to close out the file. I've tried it at the end of the code, and everywhere else inbetween. There must be something obvious I'm missing.

EDIT – I was missing something obvious!

chest.condense() --Organize the Chest
local slotnum = chest.getSizeInventory() --Use the Max Invantory Size to calculate the Slot number and Loop amount
for i=1,slotnum do -- LOOP!
	    local tableInfo = chest.getStackInSlot(i)
	    if tableInfo ~= nil then
			    for key, value in pairs(tableInfo) do
					    local data = fs.open("data.db", "a")
					    data.writeLine(key .. " = " .. tostring(value)) -- Dump table data in to the Data.db
					    print(key .. " = " .. tostring(value))
					    data.close() -- Save the file.
					  
			    end
					    local data = fs.open("data.db", "a")
					    data.writeLine("") --Add a line brake to make it pretty.
					    data.close() -- Save the file.
					    else
	    end
end

I needed to set the file to Append instead of Write…. DERP.
Lyqyd #4
Posted 13 September 2013 - 12:53 AM
Dear goodness, no. Open the file in write mode before the loop, and close it after the loop. There's definitely no need to keep opening and closing the file like that.
jay5476 #5
Posted 13 September 2013 - 02:11 AM
try doing a.)change chest.getSizeInvantory to chest.getSizeInventory and b.) change
for i = , num do 
--to 
for i = 1, num do