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

Steel tank reading.. cant get it working

Started by plazter, 12 April 2016 - 02:28 PM
plazter #1
Posted 12 April 2016 - 04:28 PM
Hello pros!
so.. i've been looking around the net, and really cant find something that does as i wish, i've been trying and testing stuff, but cant get it to work
all i need is to read the amount and name from the tank, what i wrote is:


tank = peripheral.wrap("back")
tankInfo = tank.getTankInfo()

for k,v in pairs(tankInfo) do
  for o,c in pairs(v) do
	for a,b in pairs(c) do
	  print(a)
	end
  end
end

when i run it it returns with: testTwo:6: table expected, got number
and when i try to print Y, it returns with a string obv :b

when i do tank.getTankInfo() in the LUA it returns with:

{
  {
	  capacity = 56000000
	  contents = {
						 rawName = "Lava",
						 amount = 256300
						 name = "lava",
						 id = 2,
					  },
			   },
}


what am i missing here? :s

all i really want it to do is:
Name:
Amount: x/capacity

EDIT: got it to print to only 1 monitor as i'd liked :D/>
But, it still wont update: pastebin is: http://pastebin.com/bPrRY9nZ
Edited on 12 April 2016 - 06:30 PM
plazter #2
Posted 12 April 2016 - 04:36 PM
Tried with:

tank = peripheral.wrap("back")
tankInfo = tank.getTankInfo()

for k,v in pairs(tankInfo) do
  for o,c in pairs(v) do
		 if c == "contents" then
		   for a,b in pairs(c) do
		    print(a..": ".. B)/>
		   end
    end
  end
end

now it returns no error but just .. ye.. runs the program :P/>
Sewbacca #3
Posted 12 April 2016 - 04:39 PM

tank = peripheral.wrap("back")
tankInfor k,v in pairs(tankInfo) do
  for o,c in pairs(v) do
	for a,b in pairs(c) do
	  print(a)
	end
  end
end

try:


tank = peripheral.wrap("back")
tankInfo = tank.getTankInfo()
for _, t in pairs(tankInfo) do
  for k,v in pairs(t.contents) do
    --Your stuff
  end
end

I am too lazy for explanations.
Edited on 12 April 2016 - 02:42 PM
plazter #4
Posted 12 April 2016 - 04:45 PM

tank = peripheral.wrap("back")
tankInfor k,v in pairs(tankInfo) do
  for o,c in pairs(v) do
	for a,b in pairs(c) do
	  print(a)
	end
  end
end

try:


tank = peripheral.wrap("back")
tankInfo = tank.getTankInfo()
for _, t in pairs(tankInfo) do
  for k,v in pairs(t.contents) do
	--Your stuff
  end
end

I am too lazy for explanations.

Well thanks xD, that worked :P/>
- Tho it prints too much info xD
Edited on 12 April 2016 - 02:50 PM
Lupus590 #5
Posted 12 April 2016 - 05:08 PM
I am too lazy for explanations.

I'll have a go instead then.


--# the original code
tank = peripheral.wrap("back") --# get the tanks table of methods
tankInfor k,v in pairs(tankInfo) do --# something which is not valid lua, attempting to loop through a table which doesn't exist

--# we have already errored so everything below here may as well not exist

  for o,c in pairs(v) do --# looping though a sub-table of the one above which doesn't exist
	for a,b in pairs(c) do --# more nested table looping
	  print(a)
	end
  end
end --# at least you have the correct number of ends, assuming that the line we errored on should have began a loop


--# revised by sawbacca
tank = peripheral.wrap("back") --# get the tanks table of methods
tankInfo = tank.getTankInfo() --#get the table we want to loop through
for _, t in pairs(tankInfo) do
  for k,v in pairs(t.contents) do --# I believe k is the name of the liquid in the tank, and v is the amount. I may be wrong though
    --# I'm just going to add some stuff here - Lupus590

    if k == nil then --# some tanks return nil if there is nothing in the tank
	  print("tank is empty")
    else
	  print("tank contains "..tostring(v).."mb of "..k);

    --# End of my additions - Lupus590
  end
end
plazter #6
Posted 12 April 2016 - 05:54 PM
Hey lupus, thanks for answering aswell, the name of the liquid is in the contents part as i showed up in the top,
now ive added all those and made it into a loop ( while true do loop).
got it running, but now the monitor wont update the text :(/>
Spoiler

tank = {peripheral.find("rcsteeltankvalvetile")}
tankInfo = tank[1].getTankInfo()
tankInfoTwo = tank[2].getTankInfo()
tankInfoThree = tank[3].getTankInfo()
tankInfoFour = tank[4].getTankInfo()
tankInfoFive = tank[5].getTankInfo()
tankInfoSix = tank[6].getTankInfo()
monOne = peripheral.wrap("back")
monTwo = peripheral.wrap("monitor_276")
local function nLineOne()
  lineOne = lineOne+1
  monOne.setCursorPos(1,lineOne)
end
local function nLineTwo()
  lineTwo = lineTwo+1
  monTwo.setCursorPos(1,lineTwo)
end
-- tank one
while true do
monOne.clear()
monOne.setCursorPos(1,1)
monTwo.clear()
monTwo.setCursorPos(1,1)
lineOne = 1
lineTwo = 1
for _,a in pairs(tankInfo) do
  for o,c in pairs(a.contents) do
	monOne.write(o..": ".. c)
	nLineOne()
  end
end
nLineOne()
-- tank two
for _,b in pairs(tankInfoTwo) do
  for o,c in pairs(b.contents) do
	monOne.write(o..": ".. c)
	nLineOne()
  end
end
nLineOne()
--tank three
for _,d in pairs(tankInfoThree) do
  for o,c in pairs(d.contents) do
	monOne.write(o..": "..c)
	nLineOne()
  end
end
--tank four

for _,e in pairs(tankInfoFour) do
  for o,c in pairs(e.contents) do
	monTwo.write(o..": "..c)
	nLineTwo()
  end
end
nLineTwo()
for _,f in pairs(tankInfoFive) do
  for o,c in pairs(f.contents) do
	monTwo.write(o..": ".. c)
	nLineTwo()
  end
end
nLineTwo()
--tank Six
for _,g in pairs(tankInfoSix) do
  for o,c in pairs(g.contents) do
  monTwo.write(o..": ".. c)
  nLineTwo()
  end
end
sleep(.5)
end

http://imgur.com/WSTKMBJ - A Picture of curret setup
Edited on 12 April 2016 - 03:58 PM
Lupus590 #7
Posted 12 April 2016 - 07:07 PM
I'm going to go and just rewrite all of that (it's almost painful to look at)

I will edit my post with the code soon


local mon = {peripheral.find("monitor")}
local tank = {peripheral.find("rcsteeltankvalvetile")}
--get tank info for all tanks
local tankInfo = {}

local function getAllTankInfo()
  for i = 1, #tank do
	tankInfo[i] = tank[i].getTankInfo()
  end
end

local function resetMons()
  for i = 1, #mon do
	mon[i].clear()
	mon[i].setCursorPos(1,1)
  end
end
local function printEntry(t, m)--t is tankInfo, m is monitor
  for _,a in pairs(t) do
	for o,c in pairs(a.contents) do
		  m.print(o..": ".. c) --make a new line for you after writing
	end
  end
  m.print()--make a blank line
end


while true do
  resetMons()
  getAllTankInfo()

  for i = 1, #tankInfo do
	if i % 2 == 0 then --split the prints accross both monitors
	  printEntry(tankInfo[i], mon[1])
	else
	  printEntry(tankInfo[i], mon[2])
	end
  end

  sleep(0.5)
end

edit: now gets latest tank data on new loops
edit2: fix error
Edited on 12 April 2016 - 06:25 PM
plazter #8
Posted 12 April 2016 - 07:09 PM
I'm going to go and just rewrite all of that (it's almost painful to look at)

I will edit my post with the code soon

lol xD thanks

made it only write fluid: <name>
and amount: <amount>
Edited on 12 April 2016 - 05:10 PM
Lupus590 #9
Posted 12 April 2016 - 07:37 PM
to do that you will need to replace the inner most loop of printEntry


-- full function
local function printEntry(t, m)--t is tankInfo, m is monitor
  for _,a in pairs(t) do
	m.print("name: "..a.contents.name)
	m.print("amount: "..a.contents.amount)
  end
  m.print() --add a blank line
end


I should say that I'm not familiar with this tank, so it's possible that this won't work at all

One thing I have noticed is that this wont add blank lines to separate things, opps
Edited on 12 April 2016 - 06:23 PM
plazter #10
Posted 12 April 2016 - 07:53 PM
to do that you will need to replace the inner most loop of printEntry


-- full function
local function printEntry(t, m)--t is tankInfo, m is monitor
  for _,a in pairs(t) do
	m.print("name: "..a.contents.name)
	m.print("amount: "..a.contents.amount)
  end
end


I should say that I'm not familiar with this tank, so it's possible that this won't work at all

One thing I have noticed is that this wont add blank lines to separate things, opps

Ill try your version thanks mate

EDIT: Line 28: attempt to index ? (a nil value)
Edited on 12 April 2016 - 05:59 PM
Lupus590 #11
Posted 12 April 2016 - 08:24 PM
I've updated my code to get new data.

Is that line 28 error on my code?

edit: should be fixed
Edited on 12 April 2016 - 06:26 PM
plazter #12
Posted 12 April 2016 - 08:29 PM
I've updated my code to get new data.

Is that line 28 error on my code?

edit: should be fixed
And now pastebin get aint working on zeh server :/
Lupus590 #13
Posted 12 April 2016 - 08:33 PM
pastebin is sometimes slow to update if you change it alot