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

[openccsensors] Problem with chest slot checking

Started by sovietostrich, 21 April 2013 - 09:26 AM
sovietostrich #1
Posted 21 April 2013 - 11:26 AM
This program should be displaying the amount of materials and their corresponding sizes, however it should only display each material once in the list. However the output is just one line repeated throughout the display "Oak Wood Planks: 8" which is just the material size in the first slot, it does not even count any other materials. Can someone hlap me ploise?

os.loadAPI("ocs/apis/sensor")
local sensor = sensor.wrap("left")
local monitor = peripheral.wrap("right")
local targets = sensor.getTargets()

while true do
  local details = sensor.getTargetDetails("-1,0,0")
  loopcounter = 0
  slot = details.Slots
  x=0
  i=0
  currentMaterials = 0

  materials = {27}  --defining arrays with 27 slots
  quantity = {27}
  while x < 27 do
	materials[x] = "nul"   --filling materials and quantity with nul and 0
	quantity[x] = 0
	x=x+1	 --increment x to fill all array slots
  end
  x=0  --reset x
  while x < 27 do
	while i < 27 do		   --Loop to check the current slot with all the other materials
	  if details.Slots[x+1].Name == materials[i] then --If the current slot contains any previous material
		quantity[i] = quantity[i] + slot[x+1].Size	--Add that slot size to the i quantity
	  else  
		materials[currentMaterials] = slot[x+1].Name  --Assign the name of the current slot to the current material name in the array
		quantity[currentMaterials] = slot[x+1].Size   --Assign the quantity --II--
		currentMaterials = currentMaterials + 1		 --Increment the amount of current materials
	  end
	  i = i+1
	end
	x = x + 1
  end
  x=0
  while x<27 do
	monitor.setCursorPos(1,x)
	monitor.clearLine()
	monitor.write(materials[x]..": "..quantity[x])
	x=x+1
  end
end
Spongy141 #2
Posted 21 April 2013 - 03:21 PM
Why would you have 3 different while true loops with the exact same trigger, you have all but one of them set to x < 27…. and you defined x 3 times…. the first 2 seems ok, since you have x adding 1, but then you define x as 0 again… also you need to do, turtle.select() to select an other slot…. PS theirs 16 slots not 27…
Doyle3694 #3
Posted 21 April 2013 - 11:20 PM
I recommend you read through some lua tutorials and guides before applying all c++/java/whatever logic into lua :)/>

Things like your table(what you call an array), you dont define how many indexes it will have using the first number in it. Right now 27 would be an actual number inside materials which would be accessed through materials[1]
sshaggy #4
Posted 14 June 2013 - 05:21 PM
this is a program i wrote to dump from an ME system into a chest to check for 16 items. It kinda works, the problem is that it gets stuck into a loop needing charcoal even when more than 16 is present. Can someone please help? :)/>


os.loadAPI("ocs/apis/sensor")
local w,h = term.getSize()
local wm, hm, monitor
while true do
function startMonitor()
monitor = peripheral.wrap("right")
monitor.setTextScale(1)
monitor.setBackgroundColor(colors.black)
wm, hm = monitor.getSize()
oldx, oldy = monitor.getCursorPos()
–monitor.clear()
end
function check()
if count < 16 then –gets stuck here
monitor.clear()
print("Need Charcoal")
monitor.write("Need Charcoal")
monitor.setCursorPos(1,oldy)
redstone.setOutput("back",true) –turn on lamp
redstone.setOutput("left",true) –turn on machine

else
monitor.clear() – wont get here
monitor.write("Dont Need Charcoal")
monitor.setCursorPos(1,oldy)
print("Dont Need Charcoal")
redstone.setOutput("back",false) –turn off lamp
redstone.setOutput("left",false) –turn off machine



end
startMonitor()
monitor.clear()
s = sensor.wrap("top")
details = s.getTargetDetails("1,-1,-4")
slot = details.Slots
count = details.ItemCount

while count < 16 do

redstone.setOutput("front", true) –import from ME into chest

redstone.setOutput("bottom",false) –turn off export
sleep(5)
check()
else

redstone.setOutput("front", false) –turn off import
redstone.setOutput("bottom",true) – turn on export
end

end
end
Bomb Bloke #5
Posted 14 June 2013 - 06:56 PM
You define the functions "startMonitor()" and "check()" inside your first "while" loop. There are two problems with this; one, you only need to define them once; two, defining them doesn't make them do anything. You need to actually call them.

For eg, this structure won't do anything other then re-define the two functions over and over again:

while true do
  function startMonitor()
    -- Some code
  end

  function check()
    -- Some code
  end
end

This code, on the other hand, defines the two functions once, then runs the "check()" function over and over again (which in turn calls the "startMonitor()" function over and over again, which I'm not certain there's any point to; why not call it once before the "while" loop starts?):

function startMonitor()
  -- Some code
end

function check()
  -- Some code
end

while true do
  check()
end

Don't try to use "else" with "while". Assuming it works (and I'm fairly sure it doesn't), it's redundant. Simply end the "while" block normally then run the code you'd put in the "else" block.

Don't try to have the "check()" function re-call the "check()" function. Lua has to track each call you make, so if the function is calling itself over and over, it'll chomp through the RAM it reserves for such shenanigans and your program will crash with an overflow error.

Make sure the program yields (pauses) with each loop - the Lua interpretor will shut it down if it doesn't. "s.getTargetDetails("1,-1,-4")" may yield, I'm not sure. Add a mandotory sleep line in there somewhere if you get related errors.