8 posts
Posted 13 August 2013 - 11:58 AM
I have this code:
local valve = peripheral.wrap("right")
local tableInfo = valve.getTanks("unknown")
for k, v in pairs(tableInfo) do
for x, y in pairs(v) do
print(x .. ": " .. y)
end
I am new to Tables but not computercraft.
I am using Open Peripherals to interact with Steel Tanks from Railcraft I want to print only the amount and capacity, my current code prints:
rawName: item.railcraft.liquid.steam
pressure: 0
amount: 20736000
capacity: 20736000
name: Steam
id: 7793
All Help Is Much Apprciated
Thanks
Robbo5899
101 posts
Location
Norway
Posted 13 August 2013 - 12:03 PM
shouldn't a if statement fix that?
if x == amount or x == capacity then
print(x..": "..y)
end
8 posts
Posted 13 August 2013 - 12:11 PM
It did Thank you!
I guess i was over thinking it
1 posts
Posted 13 August 2013 - 10:21 PM
I'm fairly new to LUA / turtles, but from reading the tutorials at
http://computercraft.info/wiki/Tables it looks like you can just treat them like structures / objects / classes (whichever term you're familiar with) when the keys are strings, as in the following:
A third method is to simply add the key onto the end of the table name, separated by a ".". Like the previous method, this method requires the table to already exist. It can also be treated as a regular variable, as above. This method is commonly used for when the key is a string.local Table = {}
Table.key = true
if Table.key then print("True!") end
So shouldn't the following directly grab the values you're interested in?
print("Amount: "..tableInfo[0].amount)
print("Capacity: "..tableInfo[0].capacity)
This assumes that the list of tanks returned by getTanks is indexed numerically starting at zero, of course. If that's not the case, replace [0] with [1] or .firstTank'sKey as needed. (As I said, new to LUA.)
I'd be interested in knowing if this does work, as I got here looking for a better way to figure out how full a Railcraft tank is, and I'm considering installing Open Peripherals. My single gold AND gate RS NOR latch apparently won't work because it seems like iron tanks don't properly report when they are full - the "tank full" trigger is never true and the "space for liquid" trigger is always true, even when the tank is not being drained and the GUI shows max capacity. ("Tank empty" and "Liquid in tank" appear to work just fine, but I needed "Liquid in tank" and "Space for liquid" to make the RS latch work properly.)
8 posts
Posted 14 August 2013 - 10:31 AM
Im not sure i understand what you mean, I have a program that writes to a monitor the stats of a tank
EG:
Steam 85%
Amount: 17790851
Capacity: 20736000
More than half full
Thats what it shows right now, I have a computer wrapped to a steel tank valve sending the amount over rednet. I can give you the code if you want, you'd need to adapt it to your computers though
199 posts
Location
Switzerland
Posted 15 August 2013 - 08:48 AM
Hi Talli,
Basicly you are right with that. but the table indexes are not numeric as far as i know they are strings with the id or name of the scanned entity.
For example a script i use.
local function getMaster()
t = s.getTargets()
for name, basicDetails in pairs(t) do
if name == "Sir_Block_Alot" then
print("Found My Master: "..name)
-- get more details about them
local Master = s.getTargetDetails(name)
mHealth = Master.Health
mx = math.floor(Master.Position.X)
mz = math.floor(Master.Position.Z)
my = math.floor(Master.Position.Y)
-- print their health
print("Health: "..mHealth)
print("Position: "..mx.. " / " ..mz.. " / " ..my )
print("------")
return mx, mz, my
end
end
end
could also be shortened like this:
local function getMaster()
local Master = s.getTargetDetails("Sir_Block_Alot")
mHealth = Master.Health
mx = math.floor(Master.Position.X)
mz = math.floor(Master.Position.Z)
my = math.floor(Master.Position.Y)
-- print their health
print("Health: "..mHealth)
print("Position: "..mx.. " / " ..mz.. " / " ..my )
print("------")
return mx, mz, my
end
end
end
in this case its just not useful cause it will error when i am not in range. for a static setup like tanks it would be no problem.
hope that helps.
9 posts
Posted 04 September 2013 - 01:38 PM
shouldn't a if statement fix that?
if x == amount or x == capacity then
print(x..": "..y)
end
local valve = peripheral.wrap("right")
local tableInfo = valve.getTanks("unknown")
for k, v in pairs(tableInfo) do
for x, y in pairs(v) do
if x == amount or x == capacity then
print(x..": "..y)
end
end
end
Like that? I'm new to CC so i dont know that much yet. But it wont work, not even if i remove one of the "end's" that just gives an error so i guess i do need 3 end at the bottom.
whenever i start my program it doesn't show anything on my screen.
3 posts
Posted 04 September 2013 - 02:54 PM
He's missing speech marks:
local tableInfo = valve.getTanks("unknown")
for k, v in pairs(tableInfo) do
for x, y in pairs(v) do
if x == "amount" or x == "capacity" then
print(x..": "..y)
end
end
end
3 Ends, for 2 for's and 1 if. All necessary
9 posts
Posted 04 September 2013 - 04:59 PM
He's missing speech marks:
local tableInfo = valve.getTanks("unknown")
for k, v in pairs(tableInfo) do
for x, y in pairs(v) do
if x == "amount" or x == "capacity" then
print(x..": "..y)
end
end
end
3 Ends, for 2 for's and 1 if. All necessary
Ty :)/>
But do anyone know how to make it always show on screen? I would like to setup a wired modem (or whatever that works), so my computer dont have to be near it and then have the computer somewhere else with monitors to show how much i got in the tank. :)/>
10 posts
Posted 14 November 2013 - 11:24 AM
Attach a modem to the tank valve, click the modem (light up red) and it will tell you the ID.
To wrap it:
local irontank0 = peripheral.wrap("rcirontankvalvetile_0")