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

openCCSensors tank sensor getDetails question

Started by meliudaj, 03 April 2013 - 03:30 AM
meliudaj #1
Posted 03 April 2013 - 05:30 AM
What im trying to do seems simple but I believe im getting hung up in the navigation of tables embedded in other tables.

What im trying to do is simply get more details on a tank I already know the location of. Eg. -3, 0, 3.
But not just to for loop through them. I specifically want to pull only tank capacity and tank amount. (To do a percent full tracking kind of thing.

Typing this question from cell phone so please excuse minor syntax errors.

Im trying something like:

tankRelLoc =("-3, 0, 3")
sensor = sensor.wrap ("top")
tankDetails = sensor.getTargetDetails (tankRelLoc)
– this next line is the part that I cant find the simple or functional way to do… but what I want is equivalent to this:

tankCapacity = (tankDetails.tanks.1.Capacity)
tankAmount = (tankDetails.tanks.1.Amount)

In testing tankDetails.tanks gives me the tank key of 1 and a table.. but I cant get past that.
Is there an easy refference to dig deeper into the embedded tables all at once, or do I have to open them up by pairs?
How would you get the capacity/amount of a known tank?
Lyqyd #2
Posted 03 April 2013 - 08:20 AM
You are very close. `tankDetails.Tanks[1].Amount` (and Capacity, of course) should get you the rest of the way there. You can also jump straight to percentage without needing to reassign these values:

tankPercentage = tankDetails.Tanks[1].Amount * 100 / tankDetails.Tanks[1].Capacity

As to why Tanks.1.Amount doesn't work, it is because the dot notation is used for string literals, so it is equivalent to: Tanks["1"].Amount, which is a different index.
meliudaj #3
Posted 04 April 2013 - 07:37 AM
Perfect answer… havent gotten to test it, but the missing [ ] makes sense.

And yes I get what you mean about doing the math for percentage right away.. just felt easier to type on phone the way I did.

Thanks.