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

How to get Item from specified Chest-slot?

Started by meigrafd, 04 July 2015 - 07:35 PM
meigrafd #1
Posted 04 July 2015 - 09:35 PM
Hello everyone.
First: Sorry for my bad english :rolleyes:/>/>


Im currently trying to code an 'Automated Nuclear Reactor Control' Programm with a Computer and a Wireless Turtle beside the Reactor Champ… It works already very good - the Computer has a OpenCCsensor with a 'Inventory Sensor Card', scanning the Items every Second and compares them with a hardcoded Table.. If an Item is missed the Computer tells the Turtle to place a new of which Item into the Rector, and the Turtle have's 2 different Chests with á Uran or Coolant Cells… This part works perfect.

But my Problem is now, that the Turtle also needs to replace "Depleted Uran" Cells - and heres my @topic Question:


Is it possible to tell the Turtle to select a specified Slot of another (not himselfs) Inventory?


I only know 'turtle.select()' but that is only for the Turtle-Inventory, not for an extern-Inventory :unsure:/>/>

The Computer knows the Slot-Number.. I also have a function on the Turtle for 'chestobj.getSizeInventory()' … But all this is currently useless coz i dont know how to tell the turtle "suck slot#21 of Reactor-Inventory" :(/>/>


Is there maybe some trick? Or do i need to take all Item out and apply turtle.getItemDetail() on it? :wacko:/>/>


//EDIT: maybe helpful my Code:
Spoiler

--[[

.. Turtle file for Reactor-Control .. by meigrafd

--]]


---[[ CONFIG - START ]]


-- Modem channel to listen for commands from Computer.
local ModemChannel = 32768 -- Broadcast channel.

-- Maximum loop time (default: 0.5s)
local loopT=0.5

-- Chest Inventory Sides for which Items. Only valid sides: left, right, back, up
-- NOTE: Fastest for Coolant will be "up"
-- (wireless modem is always on the left, but no problem)
-- put a chest below the turtle as trash where it drops all it takes outa reactor
chestCoolant = "up"
chestUran = "back"
chestTrash = "down"
chestReactor = "front"  --turtle must be placed so this fits!


---[[ CONFIG - END ]]


---[[ functions ]]


function getDeviceSide(deviceType)
	-- loop through all the sides
	for i,side in pairs(rs.getSides()) do
		if (peripheral.isPresent(side)) then
			-- Yup, there is something on this side
			if (peripheral.getType(side) == string.lower(deviceType)) then
				-- Yes, this is the device type we need, so return the side
				return side;
			end
		end
	end
	--nothing found, return nil
	return nil;
end

function changeReactorState(state)
	print("Changing Reactor State to: "..state)
	if state == "off" then
		rs.setOutput(chestReactor, false)
	else
		rs.setOutput(chestReactor, true)
	end
end

function turn(side)
	if side == "left" then
		turtle.turnLeft()
	elseif side == "right" then
		turtle.turnRight()
	elseif side == "front" then
		turtle.turnRight()
		turtle.turnRight()
	elseif side == "back" then
		turtle.turnLeft()
		turtle.turnLeft()
	end
end

-- verify reactor in front
function verifyReactor()
	local success, detect = turtle.inspect()
	if string.find(string.lower(detect.name), "reactor") then
		return true
	else
		return false
	end
end

function Coolant()
	if chestCoolant == "up" then
		if not turtle.suckUp(1) then
			print("WARNING: can't pick up the item!")
			changeReactorState('off')
		end
	elseif chestCoolant == "down" then
		if not turtle.suckDown(1) then
			print("WARNING: can't pick up the item!")
			changeReactorState('off')
		end
	else
		turn(chestCoolant)
		if not turtle.suck(1) then
			print("WARNING: can't pick up the item!")
			changeReactorState('off')
		end
		turn(chestReactor)
	end
	-- verify reactor in front
	while verifyReactor() == false do
		turn('left')
	end
	success = turtle.drop()
	commandsexecdCount = commandsexecdCount + 1
end

function Uran()
	if chestUran == "up" then
		if not turtle.suckUp(1) then
			print("WARNING: can't pick up the item!")
			changeReactorState('off')
		end
	elseif chestUran == "down" then
		if not turtle.suckDown(1) then
			print("WARNING: can't pick up the item!")
			changeReactorState('off')
		end
	else
		turn(chestUran)
		if not turtle.suck(1) then
			print("WARNING: can't pick up the item!")
			changeReactorState('off')
		end
		turn(chestReactor)
	end
	-- verify reactor in front
	while verifyReactor() == false do
		turn('left')
	end
	success = turtle.drop()
	commandsexecdCount = commandsexecdCount + 1
end


--[[ Internal values ]]


local version = 0.2
turtle.select(1)
commandsexecdCount=0
w,h = term.getSize()
sleep(2) -- wait for 2s


--[[ Main program ]]


term.clear()
-- detect Modem
print('Connecting to modem...')
sleep(0.5)
side = getDeviceSide("modem")
if side == nil then
	error('Error: Could not connect to Modem!')
else
	myModem = peripheral.wrap(side)
	myModem.open(ModemChannel)
	if myModem.isWireless() then
		print('success  (wireless)')
	else
		print('success')
	end
	sleep(0.5)
end
sleep(3)
term.clear()

--changeReactorState('on')


-- Receive messages Event loop
while true do
	tID = os.startTimer(loopT) -- Wait for loopT secounds.
	repeat
		local event, modemSide, senderChannel, replyChannel, message, senderDistance = os.pullEvent()
		if event == "modem_message" then
			local Slot = message.slot
			local Item = message.item
			print('('..commandsexecdCount..') slot#'..Slot..' -> '..Item)
			if string.find(string.lower(Item), "coolant") then
				Coolant()
			elseif string.find(string.lower(Item), "uran") then
				Uran()
			else
				print('ERROR: received unknown Item "'..Item..'"')
			end
		end
	-- exit repeat loop on timer event
	until (event == "timer" and modemSide == tID)
end
Edited on 05 July 2015 - 12:26 PM
HPWebcamAble #2
Posted 05 July 2015 - 12:48 AM
This topic is similar:
http://www.computercraft.info/forums2/index.php?/topic/23724-quick-turtle-question-ftb-ultimate/


I believe OpenPeripherals adds a method to specify which slot to pull from, but I'm not sure.
meigrafd #3
Posted 05 July 2015 - 07:57 AM
Thanx.

Seems like its possible, thats the Output: http://pastebin.com/Chyexhq8

But im confused how to use pullItem or pullItemIntoSlot or pushItemIntoSlot ? :blink:/>

//EDIT:

I've tryd:
reactor = peripheral.wrap("front")

reactor.pullItemIntoSlot(1,22,NORTH,2)  -- Output: Other inventory not found
reactor.pullItemIntoSlot(NORTH,22,1,2)  -- Output: Argument direction cannot be null
reactor.pullItemIntoSlot("north",22,1,2) -- Output: Other inventory not found

--same with reactor.pullItem()

reactor.listMethods() --[[ Output:
...
pullItem(direction,slot,maxAmount?,intoSlot?)
pullItemIntoSlot(direction,slot,maxAmount?,intoSlot?)
--]]
..i also tryd all other directions..
Edited on 05 July 2015 - 06:57 AM
Bomb Bloke #4
Posted 05 July 2015 - 08:33 AM
If it helps, remember that when you use a peripheral's function, the peripheral is what performs the action.

The description you got for pushItem could be re-written as:

pushItem(string direction, number source slot, number amount to push, number target slot)

Push an item from the current inventory into slot on the other one. Returns the amount of items moved.

So, let's say the inventory had 64 thingies in its third slot, you wanted it to push 32 of those to the turtle's tenth slot, and the turtle was to the south of the inventory. You might do:

local inventory = peripheral.wrap("front")
inventory.pushItem("south", 3, 32, 10)

You should be able to get more readable function descriptions (albeit only for OpenPeripheral-supported blocks) by typing this sort of thing into the command line:

openp/docs <peripheralSide> <functionName>
meigrafd #5
Posted 05 July 2015 - 08:55 AM
Hm seems like im too stupid :(/>
I have setup on each direction a Chest with different Items in Slot1 and also another in Slot1-of-Turtle… Still doesnt work - still says "Other inventory not found" :angry:/>
Bomb Bloke #6
Posted 05 July 2015 - 09:39 AM
The chest is telling you it can't find the turtle in the direction you're telling it to look. For example, if the chest is north of the turtle, then you must specify "south".

If that doesn't make sense, just use the opposite direction to whatever you've been doing.
meigrafd #7
Posted 05 July 2015 - 09:52 AM
Ahh! Now i got it just befor you reply'd ;)/>

I thought the direction means "from the perspective of Turtle". :blink:/> :P/>

My Reactor is 'north' from Turtle, so i have to use inventory.pushItem("south",1,1,2)
'push' to get it from Chest to Turtle.
'pull' to get it from Turtle to Chest.

But what if i have more than 1 Chest? Actually it only works with one chest

Thats the current Test-Setup, but only the red-arrow markt Chest works with "west" -> http://s28.postimg.o...05_09_53_50.png

Or means 'inventory = peripheral.wrap("front")' that i only can use the 'front = west' direction? So i must setup all other Directions also with peripheral.wrap() ?
Edited on 05 July 2015 - 08:12 AM
Bomb Bloke #8
Posted 05 July 2015 - 03:19 PM
When you wrap an inventory as a peripheral, you need to specify which side of the turtle faces the peripheral. This can be front/back/left/right/top/bottom (following the ComputerCraft standard for directions). You can't wrap peripheral blocks to the left/right if the turtle has tools in those positions. If the turtle turns/moves, you'll need to wrap the peripheral again.

When you tell a wrapped inventory peripheral to interact with another inventory, you instead use north/south/east/west/up/down (following the OpenPeripherals standard for directions).