Jump to content




Steel tank reading.. cant get it working


  • You cannot reply to this topic
12 replies to this topic

#1 plazter

  • Members
  • 134 posts

Posted 12 April 2016 - 03: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 by plazter, 12 April 2016 - 07:30 PM.


#2 plazter

  • Members
  • 134 posts

Posted 12 April 2016 - 03: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

#3 Sewbacca

  • Members
  • 450 posts
  • LocationStar Wars

Posted 12 April 2016 - 03:39 PM

View Postplazter, on 12 April 2016 - 03:28 PM, said:

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 by Sewbacca, 12 April 2016 - 03:42 PM.


#4 plazter

  • Members
  • 134 posts

Posted 12 April 2016 - 03:45 PM

View PostSewbacca, on 12 April 2016 - 03:39 PM, said:

View Postplazter, on 12 April 2016 - 03:28 PM, said:

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 by plazter, 12 April 2016 - 03:50 PM.


#5 Lupus590

  • Members
  • 2,028 posts
  • LocationUK

Posted 12 April 2016 - 04:08 PM

View PostSewbacca, on 12 April 2016 - 03:39 PM, said:

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


#6 plazter

  • Members
  • 134 posts

Posted 12 April 2016 - 04: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

http://imgur.com/WSTKMBJ - A Picture of curret setup

Edited by plazter, 12 April 2016 - 04:58 PM.


#7 Lupus590

  • Members
  • 2,028 posts
  • LocationUK

Posted 12 April 2016 - 06: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 by Lupus590, 12 April 2016 - 07:25 PM.


#8 plazter

  • Members
  • 134 posts

Posted 12 April 2016 - 06:09 PM

View PostLupus590, on 12 April 2016 - 06:07 PM, said:

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 by plazter, 12 April 2016 - 06:10 PM.


#9 Lupus590

  • Members
  • 2,028 posts
  • LocationUK

Posted 12 April 2016 - 06: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 by Lupus590, 12 April 2016 - 07:23 PM.


#10 plazter

  • Members
  • 134 posts

Posted 12 April 2016 - 06:53 PM

View PostLupus590, on 12 April 2016 - 06:37 PM, said:

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 by plazter, 12 April 2016 - 06:59 PM.


#11 Lupus590

  • Members
  • 2,028 posts
  • LocationUK

Posted 12 April 2016 - 07:24 PM

I've updated my code to get new data.

Is that line 28 error on my code?

edit: should be fixed

Edited by Lupus590, 12 April 2016 - 07:26 PM.


#12 plazter

  • Members
  • 134 posts

Posted 12 April 2016 - 07:29 PM

View PostLupus590, on 12 April 2016 - 07:24 PM, said:

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 :/

#13 Lupus590

  • Members
  • 2,028 posts
  • LocationUK

Posted 12 April 2016 - 07:33 PM

pastebin is sometimes slow to update if you change it alot





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users