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

eHelp with Pim program

Started by KaosOveride, 06 October 2016 - 10:10 PM
KaosOveride #1
Posted 07 October 2016 - 12:10 AM
Disclamer:
Im not a coder so hope its not to sloppy and this is first attempt with computercraft


Trying to get a pim to strip all items/armor and place in chest when ever its stepped on


pim = peripheral.wrap("right")
chest = "north"
size = pim.getInventorySize()
while size == 0 do
  sleep(1)
	end


  
for i=size, 0,-1 do
  pim.pushItem(chest, i)

   end


Works after I reboot the computer

then returns error
player:11: -1

any idea on how to make it continue to work with out needing the reboots
Bomb Bloke #2
Posted 07 October 2016 - 01:47 AM
If the first slot in the chest is 0, then highest slot will be size - 1:

for i = 0, size - 1 do

It may also be the case that you need to confirm there's something to push before you actually push it; IIRC you'd call pim.getSlot(i) and check that it's not nil:

if pim.getSlot(i) then pim.pushItem(chest, i) end

… though this tool can help you get specifics on the functions available through OpenPeripheral.
KaosOveride #3
Posted 07 October 2016 - 02:15 AM
If the first slot in the chest is 0, then highest slot will be size - 1:

for i = 0, size - 1 do

What do you mean about the first slot in the chest is 0.

I thought that it is pim.pushItem(target location, player inventory id , amount to pull, location to put id) as the possible arguments and if left blank it would just find next avail open location in target inventory. And take everything in the item slot.
Very possible that I am wrong though. If so how could I adjust the code to fix it.?

The program runs fine and functions correctly until it is completed. Upon completion i get the Error at line 11 (-1) which is I think the for loop (i) variable is being set to -1 and not resetting to 0 at the end of the program.

Any other suggestions for things I could try?
Bomb Bloke #4
Posted 07 October 2016 - 04:36 AM
What do you mean about the first slot in the chest is 0.

Let's say, for the sake of example, that your container had five slots in it. Your loop:

for i=size, 0,-1 do

… will iterate "i" through six values:

5, 4, 3, 2, 1, 0

If the first slot in the container is represented by 0, then the last slot must be represented by size - 1.

If so how could I adjust the code to fix it.?

I dunno, what does the documentation for the PIM's pushItem function tell you?

(No, I don't know, myself. To access it, one needs to actually hook one up and then run a script such as the one I linked you to.)

Upon completion i get the Error at line 11 (-1) which is I think the for loop (i) variable is being set to -1 and not resetting to 0 at the end of the program.

The error is informing you that the script did not run to completion, but rather it crashed on the 11th line.

The 11th line of the code you've displayed here reads:

pim.pushItem(chest, i)

Normally you'd get a more descriptive error than "-1", but whoever coded the PIM peripheral apparently didn't rig things such that one would be provided. Hence my guesses - it's either because your loop checks more slots than your container has, or it's because you can't push from empty slots.
KaosOveride #5
Posted 07 October 2016 - 11:48 PM
That makes alot more sense.. And now that im thinking about it im going to wager that your right.. Looking forward to trying to fix it..
Thank you very much for the help.