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

getAllStacks() throwing an error

Started by hogbits, 30 January 2014 - 07:16 PM
hogbits #1
Posted 30 January 2014 - 08:16 PM
Hello ComputerCraft Pros. I am having a bit of trouble with some code that Direwolf20 wrote. In his Server Play S6E9 video he walks everyone through setting up a portal system using computers. And I wanted to copy his setup on my own server.

Okay, so running his "books" code on my turtle (http://pastebin.com/zSu3xM53) produces this error:
books:17: attempt to call nill


local slots = p.getAllStacks()

getAllStacks() appears not to be a valid function. I checked it against the wiki and did not see getAllStacks() listed as a valid API function.

Is this a valid function? Is it possible that the Forgecraft server is running a CC version higher than 1.57? If so. What version?

Thanks in advance.

Hog
Bomb Bloke #2
Posted 30 January 2014 - 10:20 PM
"p" represents a peripheral, an item next to the computer. "p.getAllStacks()" is supposed to be a command available to that item (I assume it returns a table filled with information about all the different slots in the inventory of that block) - I believe OpenPeripheral added it at some point, but it may not be there in your version.

This may help you devise a workaround.
hogbits #3
Posted 01 February 2014 - 03:13 PM
Thanks for the info @lua maniac. The current version of DW20 has open peripherials installed so I suppose his code should work out of the box. The thread you referenced I have seen earlier. I tried to rewrite Dires code to use that snippet but I am getting another error. "bad argument: table expected, got nil"

Pastebin: http://pastebin.com/zR4DefSz


function checkSlots(id)
   clearInv()
   data = {}
   local slot
   local name
--   local slots = p.getAllStacks()

  local function getInventory( object )
	local slots = {}
	for i=0, object.getSizeInventory() do
	  slots[i] = object.getStackInSlot(i)
	end
	return slots
  end
   for i,j in pairs(slots) do
	  slot = i
	  name = j["destination"]
--	  print(name)
	  data[slot]=name
   end
   rednet.send(id,"done")
end

So my thought here was that the p.getAllStacks() function needed to be replaced. So I commented it out. As I understand it it just gets all the stacks from the chest below and stored it as an array called "slots" So in the referenced code, I modified it to store the items as a local variable called slots and return slots. I am guessing it's not compatable ence the error, but I am not sure how to fix it. Any suggestions?
Bomb Bloke #4
Posted 01 February 2014 - 08:23 PM
In that case, I believe you'll be on an outdated version of ComputerCraft with an outdated version of OpenPeripheral. Both are likely more up to date then the versions he wrote that script for.

Generally version doesn't matter so much with ComputerCraft (though it sounds like the coming 1.6 might change all that…), though again, OpenPeripheral's commands have been changing around recently.

Anyway, um, that's not how you'd do it. Function definitions don't do anything unless you call them, so just plonking such a definition in the middle of an existing function won't work.

I took a stab at re-writing it correctly, but the "destination" table index has proven to be a bit of a stumbling block. I've no idea which versions of OpenPeripheral support it, as OP's documentation (both in terms of the commands available to each version and in terms of a changelog) leaves… much to be desired. Hopefully the main thread on this forum will be locked and replaced shortly.

Best I can make out (and this is mostly conjecture), the "destination" field was added at some point while MineCraft 1.5.x was current and then later removed when OpenPeripheral hit MineCraft 1.6.x. Given that MystCraft (and all the other mods OpenPeripheral has to deal with) may or may not've updated to 1.6.x at that time, this would be completely understandable.

The version of OpenPeripheral I have installed is 0.2.1-preview8 (for MC 1.6.x), which is quite old now. The version of DireWolf I'm running is 1.0.11, which is also old. I have no idea if the latest Direwolf pack has a later version of OpenPeripheral, and in the event that it does, I've no idea if that can read destinations from linking books (the version I've got can't). Nor do I have much interest in installing the however many versions of OpenPeripheral / MineCraft it takes to determine what each can and cannot do.

Anyway.

Assuming whatever versions you're running DO have this table element, this should work:

function checkSlots(id)
   clearInv()
   data = {}
   local slot
   for i=1,p.getInventorySize() do
      slot = p.getStackInSlot(i)
      if slot then data[i] = slot.destination end
   end
   rednet.send(id,"done")
end

If not? You'll likely find that the script runs, but no destinations show up on your monitor. Using "slot.name" instead of "slot.destination" would be a (rather poor, but simple to implement) workaround for this.
KrazyBrute #5
Posted 04 February 2014 - 11:45 AM
Thankyou lua maniac I have just do what you said above:
function checkSlots(id)
clearInv()
data = {}
local slot
for i=1,p.getInventorySize() do
slot = p.getStackInSlot(i)
if slot then data[i] = slot.destination end
end
rednet.send(id,"done")
end

This did work and I can use the books on the monitor however the name of the books does not say what it is supposed e.g. on the book it says Krazy's House but on the monitor it just says "linking book"
Do you know how I could get around that?
Thanks
Krazy
Bomb Bloke #6
Posted 04 February 2014 - 05:55 PM
Odd, I was suspecting "slot.destination" would've showed nothing, and "slot.name" would've given the behaviour you describe.

Anyway, no, sorry. I'm not aware of a method to have the script automatically determine where the books are supposed to go - it has no way of telling them apart, that I can see. You could hard-code the book names into the script, of course, however it's very much geared towards people not doing that, so it'd be a fair bit of a re-write.

If you're lucky, someone may come along and reveal which version(s) of OpenPeripheral will do what you want.
KrazyBrute #7
Posted 06 February 2014 - 02:41 AM
My apologies Lua Maniac I tried again with slot.name and the book showed on the monitor. Just not with it its actual name, it just said "Linking book"
The code that I have changed and how it is now is here: http://pastebin.com/yFqE6aZv
Hopefully you might be able to find a way around this problem :)/>
Bomb Bloke #8
Posted 06 February 2014 - 07:01 PM
It sounds like updating may indeed be your answer.
tecrogue #9
Posted 11 February 2014 - 12:57 PM
Testing things out, it looks like the reason you are getting "Linking book" instead of the name you gave it in a writing desk is the code you put together pulls the item name instead of that field like Dire's original code did.

The work around using your current code would be to just rename the books in an Anvil. (but it's a bit of kludge)