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

Counting ores from a quarry.

Started by tysje, 25 October 2012 - 09:12 PM
tysje #1
Posted 25 October 2012 - 11:12 PM
i'm trying to create a code within computercraft

What i'm trying to do is count diamonds and coal, and then display what i get on a monitor.
the thing is that i get an error when i try to add "1" to to "diamonds"

but i can add 1 to "count" and display it. is there anny way for me to get something else that works the same way as "count"

and yes i know i don't know mutch lua and that i need to read moore :D/>/>

here is the code ^_^/>/>


local count = 0
local diamond = 0
local monitor
local pState =
{
  [1] = rs.testBundledInput("back", colors.red),
  [2] = rs.testBundledInput("back", colors.white),
  [3] = rs.testBundledInput("back", colors.yellow),
  [4] = rs.testBundledInput("back", colors.gray)
}

function writeAt(x,y,text)
  monitor.setCursorPos(x,y)
  monitor.write(text)
end

function startMonitor()
  monitor = peripheral.wrap("top")
end

function drawDisplay()
  monitor.clear()
  writeAt(1,1,"***************************")
  writeAt(1,2,"*	from this quarry	 *")
  writeAt(1,3,"***************************")
  writeAt(1,4,"*  coal:	   0		  *")
  writeAt(1,5,"*  Diamonds	0		  *")
end

function writeCount()
  writeAt(16,4,"		   ")
  writeAt(16,4,string.format("%i",count))
end
function diamond()
  writeAt(16,5,"		   ")
  writeAt(16,5,string.format("%i",diamond))
end


function redstoneEvent(p1, p2)
  local cState =
  {
	[1] = rs.testBundledInput("back", colors.red),
	[2] = rs.testBundledInput("back", colors.white),
	[3] = rs.testBundledInput("back", colors.yellow),
	[4] = rs.testBundledInput("back", colors.gray)
  }

   if pState[2] == false and cState[2] == true then -- kull + 1
	  count = count + 1
	  writeCount()
   end
   if pState[1] == false and cState[1] == true then -- i need another count!! cant use this one for both diamonds and coal :/
	  diamond = diamond + 1
	  diamond()
   end

   pState[1] = cState[1]
   pState[2] = cState[2]
   pState[3] = cState[3]
   pState[4] = cState[4]
end

startMonitor()
drawDisplay()
writeCount()

repeat
  local event, p1, p2 = os.pullEvent()
	if event == "redstone" then
	 redstoneEvent(p1, p2)
	end
until event == "char" and p1 == "x"  -- x for og avslutte programmet :P/>/>

os.shutdown()
tysje #2
Posted 25 October 2012 - 11:17 PM
Never mind, i found the problem ^_^/>/>