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

Tank levels?

Started by luckie12, 23 May 2015 - 02:50 PM
luckie12 #1
Posted 23 May 2015 - 04:50 PM
Hi, i saw alot of videos about people having a nice monitor with fuel levels of each of their tanks, now the problem i get is i think it does not understand

m.getTanks("unknown")
anymore because everyscript gets an error on that one.
And i cant seem to find the right part like the API for it.
Can somebody explain me this or if this is still working at all.

Thankyou
Lupus590 #2
Posted 23 May 2015 - 05:37 PM
Most of the tank monitoring scripts use open peripherals, do you have that installed?
luckie12 #3
Posted 23 May 2015 - 06:06 PM
Yeah i do, i use the FTB Infinity modpack. I also do have the OpenPeripheral mod installed :)/>
luckie12 #4
Posted 23 May 2015 - 10:24 PM
Is this still possible to do?
Lupus590 #5
Posted 23 May 2015 - 10:59 PM
As I said before, you will need Open Peripherals (I think you will need core and integration for this).
I'm going to assume that you know how to build a railcraft tank. You will need it to have 3 valves, one for each: input, output and computer.
If you don't know how to use peripherals yet then read this.
Place the computer next to a valve.
Run this command into the terminal where side is the side that the tank valve is on the computer (front/back/left/right/up/down)
openp/docs <side>
If the computer says "no such program" then Open Peripherals is installed incorrectly. (You may need to check for case sensitivity, try ls or dir to print all files/folders)
If Open Peripheral is installed correctly, then the documentation of the tank will be printed.
luckie12 #6
Posted 24 May 2015 - 06:54 PM
it has getTankInfo(direction?)

I already tried that, but that doesnt work :/

i get;


table: 1298f94
startup:10: attempt to concatenate table and string
Edited on 24 May 2015 - 05:13 PM
luckie12 #7
Posted 24 May 2015 - 09:12 PM
This is what i have now


net = peripheral.wrap("back")
bridge = peripheral.wrap("left")
while true do
stored = net.callRemote("openblocks_tank_5","getTankInfo")
tank2 = net.callRemote("openblocks_tank_6","getTankInfo")
print(stored)
print(tank2)
bridge.addText(1,1, stored, 0xFF0000)
bridge.addText(1,10, tank2, 0xFFFFFF)
sleep(0.15)
bridge.sync()
bridge.clear()
end

This displays me for STORED:

{1.0={contents={id=1.0,amount=6000.0,rawName=Water,namewater}, capacity=16000.0}}
and for TANK2
{1.0={capacity=16000.0}}
TANK2 is empty

i just want it to show me the Amount, name and Capacity
Edited on 24 May 2015 - 07:12 PM
KingofGamesYami #8
Posted 24 May 2015 - 09:17 PM

local amount = stored[1].contents.amount
local name = stored[1].contents.rawName
local capacity = stored[1].capacity

This should work, although I'm not sure how you are displaying a table without serializing it.
flaghacker #9
Posted 24 May 2015 - 09:21 PM
You're not going to get anywhere without any knowledge of tables, so I suggest you to read that link.

Post again if you've tried something and it still doesn't work.


local amount = stored[1].contents.amount
local name = stored[1].contents.rawName
local capacity = stored[1].capacity

This should work, although I'm not sure how you are displaying a table without serializing it.

Maybe this mysterious "bridge" thing does the serialisation by default.
luckie12 #10
Posted 24 May 2015 - 09:43 PM
This mysterious "bridge" thing is the Terminal Glasses Bridge.

@

local amount = stored[1].contents.amount
i get;
startup:10: attempt to index ? (a nil value)

That happens when i have:


bridge.addText(1,1, amount, 0xFF0000)

EDIT:
Does not work at all :/ Even when i get back on it with

bridge.addText(1,1, stored, 0xFF0000)

it points to the

local amount = stored[1].contents.amount
line

EDIT2:
That error is gone when i fill the tank with water,

then the error moves to the second line;

local amount = stored[1].contents.rawName
Edited on 24 May 2015 - 08:35 PM
Bomb Bloke #11
Posted 25 May 2015 - 01:18 AM
If "stored[1].contents.amount" doesn't error, then "stored[1].contents.rawName" won't error. Methinks you've gotten things mixed up, 'specially since you wouldn't want to assign both values to "amount"…

Keep us up to date on what the whole script looks like.
luckie12 #12
Posted 25 May 2015 - 01:50 AM
This is the whole script at the moment


net = peripheral.wrap("back")
bridge = peripheral.wrap("left")
bridge.clear()
-- count = 0
while true do
stored = net.callRemote("openblocks_tank_5","getTankInfo")
tank2 = net.callRemote("openblocks_tank_6","getTankInfo")
stored = net.callRemote("mcp_mobius_betterbarrel_0","getStoredItems")

local amount = stored[1].contents.amount
local name = stored[1].contents.rawName
local capacity = stored[1].capacity

--count = count + 5

print(stored)
print(tank2)
print(barrel)

--bridge.addGradientBox(0,0,900,50,0xFF0000,0.8,0x00FF00,0.8,1)
bridge.addBox(0,0,900,50,0x00CCFF,0.3)
bridge.addBox(0,250,90,40,0x00CCFF,0.2)
bridge.addText(1,255, "By Fatal1tyGC", 0xFFFF00)
bridge.addText(1,265, "This script", 0xFFFF00)
bridge.addText(1,275, "Runs Statview 0.1", 0xFF0000)

bridge.addText(1,1 amount, 0xFF0000)
bridge.addText(1,10, tank2, 0xFFFFFF)
bridge.addText(1,20, barrel, 0xFF0000)
sleep(0.15)
bridge.sync()
bridge.clear()
end
Bomb Bloke #13
Posted 25 May 2015 - 02:41 AM
Ok, so what's the deal here?:

stored = net.callRemote("openblocks_tank_5","getTankInfo")  -- Stick tank info in "stored"
...
stored = net.callRemote("mcp_mobius_betterbarrel_0","getStoredItems")  -- Replace tank info with barrel info

Presumably you were wanting to use different variables…?
luckie12 #14
Posted 25 May 2015 - 03:09 PM
Oh yeah, Stored @ the last line, should be:

barrel = net.callRemote("mcp_mobius_betterbarrel_0","getStoredItems")  -- Replace tank info with barrel info
But the error keeps coming…


This is the right script;


net = peripheral.wrap("back")
bridge = peripheral.wrap("left")
bridge.clear()
-- count = 0
while true do
stored = net.callRemote("openblocks_tank_5","getTankInfo")
tank2 = net.callRemote("openblocks_tank_6","getTankInfo")
barrel = net.callRemote("mcp_mobius_betterbarrel_0","getStoredItems")
local amount = stored[1].contents.amount
local name = stored[1].contents.rawName
local capacity = stored[1].capacity
--count = count + 5
print(stored)
print(tank2)
print(barrel)
--bridge.addGradientBox(0,0,900,50,0xFF0000,0.8,0x00FF00,0.8,1)
bridge.addBox(0,0,900,50,0x00CCFF,0.3)
bridge.addBox(0,250,90,40,0x00CCFF,0.2)
bridge.addText(1,255, "By Fatal1tyGC", 0xFFFF00)
bridge.addText(1,265, "This script", 0xFFFF00)
bridge.addText(1,275, "Runs Statview 0.1", 0xFF0000)
bridge.addText(1,1 amount, 0xFF0000)
bridge.addText(1,10, tank2, 0xFFFFFF)
bridge.addText(1,20, barrel, 0xFF0000)
sleep(0.15)
bridge.sync()
bridge.clear()
end
Edited on 25 May 2015 - 03:10 PM
luckie12 #15
Posted 25 May 2015 - 10:32 PM
So, i made a new script, that works… But only shows me the Capacity: 16000


bridge = peripheral.wrap("left")
tank1 = peripheral.wrap("openblocks_tank_7")
tank2 = peripheral.wrap("openblocks_tank_8")
tank3 = peripheral.wrap("openblocks_tank_9")
tank4 = peripheral.wrap("openblocks_tank_10")

tank1i = tank1.getTankInfo("unknown")
tank2i = tank2.getTankInfo("unknown")
tank3i = tank3.getTankInfo("unknown")
tank4i = tank4.getTankInfo("unknown")

while true do
for k,v in pairs(tank1i[1])
do bridge.addText(1,10,k..": "..v ,0xFFFFFF)
end
sleep(0.75)
bridge.sync()
bridge.clear()
end

i cant seem to access the other contents of it, even when i do this in the terminal:

lua

Lua > p = peripheral.wrap("openblocks_tank_7")
Lua > info = p.getTankInfo("unknown")
Lua > for k,v in pairs(info) do print(k,v) end

it shows me only
capacity16000
contentstable:#######

Also, when there is water in a tank, and the script runs, it stops because i get an error:

startup:14: attempt to concatenate string and table
Bomb Bloke #16
Posted 26 May 2015 - 02:41 AM
Consider; you have this code:

Lua > p = peripheral.wrap("openblocks_tank_7")
Lua > info = p.getTankInfo("unknown")
Lua > for k,v in pairs(info) do print(k,v) end

… which tells you that this:

capacity16000
contentstable:#######

… are the keys and their values, within the table assigned to "info".

If you've been paying attention, you'll know that this means you can refer to those values as "info.capacity" and "info.contents". Given that your above code prints out what's in your "info" table, how do you reckon you'd get it to print out the contents of, say, your "info.contents" table instead?

Also, when there is water in a tank, and the script runs, it stops because i get an error:
startup:14: attempt to concatenate string and table

So don't do that.

If it helps, the concatenation command is "..". It's intended for tacking strings end on end to create a new string. You can't combine a string with a table.

If you really really wanted to do it, you could use k..": "..tostring(v) instead. This won't print the contents of the table "v", of course - it'll instead print a representation of a pointer to the table, "table:#######", that sort of thing - but it won't error out. You might also use k..": "..type(v), which won't tell you the content of v, but rather will state types such as number, string, table, etc…

Another thing - when you get an "attempt to index ? (a nil value)" error, it means you're trying to treat an undefined variable as though it were a table. For eg:

local myTable = {["someKey"] = "someValue"}
print(myTable.someKey)  -- Fine.

myTable = nil
print(myTable.someKey)  -- attempt to index ? (a nil value)

There will be times when you don't know whether a value is nil or not. For example (and this is just an example - you'll want to check how things actually work for yourself), you might find that "info.contents" is a table if your tank has something in it, and is nil when it does not.

In these cases, you can set up conditional statements that treat your variables as booleans - they count as true if defined, or false if they don't. For example:

if info and info.contents then print(info.contents.rawName) end  -- If "info" is non-nil, and "info.contents" is non-nil, then index into info.contents to get rawName and print that.

The check halts as soon as it figures out that the result will be false. For example, if "info" is nil, then referring to "info.contents" would throw an attempt to index nil; but the line stops before getting to that point because it'd already figured out what the final result would be and that it would never need to print anything.
luckie12 #17
Posted 26 May 2015 - 01:55 PM
So i tried;

Lua > p = peripheral.wrap("openblocks_tank_7")
Lua > info = p.getTankInfo("unknown")
Lua > for k,v in pairs(info.contents) do print(k,v) end

Doesn't work , gets error

Then i tried the
if info and info.contents then print(info.contents.rawName) end
That didnt show anything

PS sorry for the stupid questions :$
Bomb Bloke #18
Posted 26 May 2015 - 02:03 PM
Ok, so what that tells you is that info.contents is nil, this time around - possibly because when you wrote your last post you had something in the tank, as opposed to now, when I'd guess that you don't.

Try figuring out under what circumstances you can find a "contents" key in your "info" table.
luckie12 #19
Posted 26 May 2015 - 02:13 PM
If i repeat the same step, it only show me the Capacity table:


Not the contents table anymore(Doesnt matter if it contains water or not)
Bomb Bloke #20
Posted 26 May 2015 - 02:57 PM
Righto, looks like that's the only info OpenPeripheral is going to give you regarding tanks - the amount they're capable of holding. Not very useful information, but there you have it.

(Mind you, are you sure you're putting liquid into the right tank? I notice you've got a few of the things.)
luckie12 #21
Posted 26 May 2015 - 04:34 PM
i've disconnected all the tanks, and then put water in to tank 7 so; openblocks_tank_7, im like 100% sure of it haha :)/>
But, i forgot to tell you this;
I made another TEST script; this is it;

net = peripheral.wrap("back")
bridge = peripheral.wrap("left")
bridge.clear()
--count = 0
while true do
stored = net.callRemote("openblocks_tank_5","getTankInfo")
tank2 = net.callRemote("openblocks_tank_6","getTankInfo")
barrel = net.callRemote("mcp_mobius_betterbarrel_0","getStoredItems")
--local amount = stored[1].contents.amount
--local name = stored[1].content.rawName
--local capacity = stored[1].capacity
-- count = count + 5
print(stored)
print(tank2)
print(barrel)
--bridge.addGradientBox(0,0,900,50,0xFF0000,0.8,0x00FF00,0.8,1)
bridge.addBox(0,0,900,50,0x00CCFF,0.3)
bridge.addBox(0,250,90,40,0x00CCFF,0.2)
bridge.addText(1,255, "By Fatal1tyGC", 0xFF0000)
bridge.addText(1,265, "This script",0xFFFF00)
bridge.addText(1,275, "Runs Statview 0.1",0xFF0000)
bridge.addText(1,1, stored, 0xFF0000)
bridge.addText(1,10, tank2, 0xFFFFFF)
bridge.addText(1,20, barrel, 0xFF0000)
sleep(0.15)
bridge.sync()
bridge.clear()
end

The funny thing about that, i get all the tables:


However, the first table = a tank filled with water, second is empty, and the third one is a barrel with 13 eggs.
Somebody already told me to use

--local amount = stored[1].contents.amount
--local name = stored[1].content.rawName
--local capacity = stored[1].capacity
But that gives me errors.
Bomb Bloke #22
Posted 27 May 2015 - 01:02 AM
local name = stored[1].content.rawName

That'd error because there's still no "content" table to index into - it's "contents" you're after.
luckie12 #23
Posted 27 May 2015 - 04:23 PM
That was the problem! It shows it correctly now!

Wow! Huge thanks to everybody that helped me!
And a big thanks to you Bomb Bloke! :D/>

It shows now just Water, the amount and the capacity :D/>
Thankyou!!!


EDIT; Script crashes when tank goes empty, while script is running. Any solution for that?
Edited on 27 May 2015 - 02:29 PM
KingofGamesYami #24
Posted 27 May 2015 - 05:14 PM
Before indexing the contents table, make sure it isn't nil. Apparently when the tank is empty, it returns only capacity.

If your script was crashing with 'attempt to index ? (a nil value', this should fix it.


Example:

local capacity, name, amount = stored[1].capacity --#pre declare our variables (so we don't pollute the global table)
if stored[1].contents then --#if there's fluid in the tank
  --#we can use the name and amount of that flue
  name = stored.contents.rawName
  amount = stored.contents.amount
else
  --#we can't
  name = "no fluid"
  amount = 0
end
Edited on 27 May 2015 - 03:14 PM
luckie12 #25
Posted 27 May 2015 - 05:21 PM
I cant use the

local capacity, name, amount = stored[1].capacity

so i just use the old one;
Local amount
local name
local capacity
That still works

The problem with this is,

IF there is NO water in it, the GUI dissapears(My terminal glasses)
script
http://pastebin.com/1JMZjW8w

And if i put the Else above the bridge.addText
i get a nil error on the bridge texts
Edited on 27 May 2015 - 03:26 PM
Bomb Bloke #26
Posted 28 May 2015 - 02:30 AM
It looks like you want to reorganise this:

if stored[1].contents then
local capacity = stored[1].capacity
local amount = stored[1].contents.amount
local name = stored[1].contents.rawName
-- count = count + 5
print(stored)
print(tank2)
print(barrel)
--bridge.addGradientBox(0,0,900,50,0xFF0000,0.8,0x00FF00,0.8,1)
bridge.addBox(0,0,900,50,0x00CCFF,0.3)
bridge.addBox(0,250,90,40,0x00CCFF,0.2)
bridge.addText(1,255, "By Fatal1tyGC", 0xFF0000)
bridge.addText(1,265, "This script",0xFFFF00)
bridge.addText(1,275, "Runs Statview 0.1",0xFF0000)
bridge.addText(1,1, name..": "..amount.."/"..capacity, 0xFF0000)
bridge.addText(1,10,tank2, 0xFFFFFF)
bridge.addText(1,20, barrel, 0xFF0000)
else
name = "No Fluid"
amount = 0
end

… into something like this:

bridge.addBox(0,0,900,50,0x00CCFF,0.3)
bridge.addBox(0,250,90,40,0x00CCFF,0.2)
bridge.addText(1,255, "By Fatal1tyGC", 0xFF0000)
bridge.addText(1,265, "This script",0xFFFF00)
bridge.addText(1,275, "Runs Statview 0.1",0xFF0000)

if stored[1].contents then
	bridge.addText(1,1, stored[1].contents.rawName..": "..stored[1].contents.amount.."/"..stored[1].capacity, 0xFF0000)
else
	bridge.addText(1,1, "No Fluid: 0/"..stored[1].capacity, 0xFF0000)
end

bridge.addText(1,10,tank2, 0xFFFFFF)
bridge.addText(1,20, barrel, 0xFF0000)
luckie12 #27
Posted 28 May 2015 - 08:25 PM
Oooh right!