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

Why Can't I Do "x = Var.getinvsize()" Openperipheral.

Started by CCJJSax, 08 November 2013 - 09:12 PM
CCJJSax #1
Posted 08 November 2013 - 10:12 PM
This is the code directly from the OpenPeripheral documentation page to get the inventory size. It's worth noting that I don't have a startup program or any program that changes anything before this code is run.


local stringInfo = pIM.getInvName()
print(stringInfo)

here is my code. it "attempts to call nil at line 4"



local chest = peripheral.wrap("front")
local pIM = peripheral.wrap("left")
chest.swapStacks(1,2)
local cSize = chest.getSizeInventory() -- line 4 is here
local pInvSize = pIM.getSizeInventory()


for i = 1, pIM.getSizeInventory() do
  local tableInfo = pIM.getStackInSlot(i)
  if pIM.getStackInSlot(i) then
	for key, value in pairs(tableInfo) do
	  --print(key .. " = " .. tostring(value))
	  if key == "name" then
		print("slot "..i.." contains "..value)
	  end
	end
  end
end

Edited on 08 November 2013 - 09:18 PM
CCJJSax #2
Posted 08 November 2013 - 10:30 PM
After doing a bit of work with google (as in hindsight I admittedly should have done already) I came across this. It was changed from "chest.getSizeInventory()" to "chest.getInventorySize". So if anyone runs across this topic, they'll know that the documentation for it might still be in Openperipheral's 1.5 version.

when all else fails, try


p =  peripheral.wrap(side)
p.listMethods()

http://www.computerc...eralwrap-fails/
Edited on 08 November 2013 - 09:34 PM
theoriginalbit #3
Posted 08 November 2013 - 11:08 PM
Indeed, you should always list methods when you have problems… The method names in the new OpenPeripheral aim at being much more descriptive and making it much easier to understand what the purpose of the method is without needing documentation.
Lyqyd #4
Posted 09 November 2013 - 04:27 AM
No. You should not use listMethods. The correct way to print a list of peripheral methods is:


textutils.tabulate(peripheral.getMethods(side))

The reason to use peripheral.getMethods is because it is built into ComputerCraft, and is not dependent on a mod author implementing a useless alternate method. Do not advise usage of listMethods here, as it is thoroughly incorrect.
theoriginalbit #5
Posted 09 November 2013 - 06:07 AM
Do not advise usage of listMethods here, as it is thoroughly incorrect.
For anything OpenPeripheral it is not incorrect, it still works in listing out the methods and illuminates the problem with names to the programmer. However I too asked Mikee why he bothered implementing it since we have methods of doing it in vanilla CC, can't remember his response, I want to say it was something along the lines of he didn't realise we had it or something. It was also why I was asking on IRC the other week if we could call Lua-side functions from within a Java method, in an attempt to make the listMethods method do exactly the code you posted. I will definitely be striving for that method to be removed in the next major version though ;)/>