Jump to content




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


  • You cannot reply to this topic
4 replies to this topic

#1 CCJJSax

  • Members
  • 262 posts

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 by CCJJSax, 08 November 2013 - 10:18 PM.


#2 CCJJSax

  • Members
  • 262 posts

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 by CCJJSax, 08 November 2013 - 10:34 PM.


#3 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

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.

#4 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

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.

#5 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 09 November 2013 - 06:07 AM

View PostLyqyd, on 09 November 2013 - 04:27 AM, said:

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 ;)





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users