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

LUA and Peripherals (Railcraft)

Started by Wiesl, 06 November 2013 - 06:27 AM
Wiesl #1
Posted 06 November 2013 - 07:27 AM
Hello guys:

a short question, i think the answer is pretty easy, but i cannot find my mistake:

this code:


tank = peripheral.wrap("bottom")
modem = peripheral.wrap("back")
modem.open(1)


while true do
tableInfo = tank.getTanks("unknown")
for key,value in pairs(tableInfo) do
  for x,y in pairs(value) do
	  if x == "amount" then
		percent = math.floor(y / 576000 * 100)
		print(percent, "%")
		modem.transmit(1, 1, percent)
	  end
  end
end
  sleep(2)
end

is working as intended (0 errors) if there is any amount > 0 in the tank, so i tried to get rid of the nil problem with:


tank = peripheral.wrap("bottom")
modem = peripheral.wrap("back")
modem.open(1)


while true do
tableInfo = tank.getTanks("unknown")
for key,value in pairs(tableInfo) do
  for x,y in pairs(value) do
	  if x == "amount" then
		percent = math.floor(y / 576000 * 100)
		print(percent, "%")
		modem.transmit(1, 1, percent)
	
	  else
		print("empty")
		sleep(10)	
	 end
end
end
sleep(2)
end

but here i get always "empty" regardless the amount in it

my thoughts:
* why is in the first code the if - function progressing to "then" and in the 2nd to "else" but i changed nothing in the tank
* if the tank is empty there is no x=="amount" in the table, so the if-function should proceed to "else", or?
* i cannot set y==0 or y==nil or y>0 because if there is no amount in the tank the if - function with x=="amount" is not triggering, or?

How can i bypass this problem?

And a short second question:
can i compare the y-value from each loop? – so that a message or what else is only sent, if the value(amount) changed?


Thx in advance
Wiesl
Lyqyd #2
Posted 06 November 2013 - 08:43 PM
Split into new topic.

Are you trying to do something like this?


tank = peripheral.wrap("bottom")
modem = peripheral.wrap("back")
modem.open(1)


while true do
    tableInfo = tank.getTanks("unknown")
    for tNum, tInfo in pairs(tableInfo) do
        if tInfo.amount then
            percent = math.floor(tInfo.amount * 100 / tInfo.capacity)
            print(tostring(percent).."%")
            modem.transmit(1, 1, percent)
        end
    end
    sleep(2)
end

There are other ways to interact with tables than iterating through them. I hope this thing has more than one tank in it, or you don't even need the outside for loop.
Wiesl #3
Posted 07 November 2013 - 02:50 AM
Sry i donnot understand you completly ;)/>
may i ask a little further?

in your explanation, there is again the problem, that if the "amount" == 0 there is no topic amount in the table, only capacity and pressure, so, i cannot get the variable tinfo.amount. Not = nil nor =0, because she simply didn`t exist i think.

i try to get further in it, but any help would be appreciated.


Thx
Wiesl

P.S.: perhaps a 2nd Question, also:

i think the tableinfo = tanks.getTanks("unknown")
is producing a table like this, or?

tableInfo
–> amount –> 1000
–> capacity –> 50000
etc.

i can make this my own with this code, or?(in just this moment)

tableInfo = {}
tableinfo.amount = 2783
tableinfo.capacity = 576000
tableinfo.name = "Steam"

how can i print for example tableInfo –> amount –> 2783
because

tank = peripheral.wrap("bottom")


tableInfo = tank.getTanks("unknown")
print (tableInfo.amount)

is doing nothing beside a empty row.

How can i get the row amount printed, if i donnot know the exact name, because i didn?t made the table myself, but a programm (like .getTanks("side"))

Thx in Advance
Wiesl
Edited on 07 November 2013 - 05:34 AM
Wiesl #4
Posted 07 November 2013 - 07:35 AM
Ok i found something out:
the table is a table in a table with a 1 in front, like
tableInfo–>1–>amount(or capacity)–>value

i donnot know why, but i think it is so configured.

so i can print the amount with:

print(tableInfo[1]["amount"])

ok but my problem is nevertheless:

if the amount is 0, the table doesn´t contain the key "amount", so i cannot make a function with it.

how to say the programm: if there is no key "amount" in table[1] do something??

Wiels
Edited on 07 November 2013 - 06:35 AM
Bomb Bloke #5
Posted 07 November 2013 - 08:21 AM
For the purposes of an if/then/else statement, variables count as "true" if they exist, false if they do not.

Lyqyd's example demonstrates this, but you may find this more clear-cut:

if not tableInfo[1]["amount"] then
  print("Looks like there's no \"amount\" key in the table.")
else
  print("There's an \"amount\" key with an assigned value in the table!")
end
Wiesl #6
Posted 07 November 2013 - 08:57 AM
thx now it is more clear!!

hope i can configure it now!
theoriginalbit #7
Posted 07 November 2013 - 09:02 AM
the reason you must do

tableInfo[1]
is because it is possible to have blocks with multiple tanks within them, for example think Forestry or Thermal Expansion how there is an output as well as a water level. As such each "tank" gets placed into its own index with its appropriate information.
Lyqyd #8
Posted 07 November 2013 - 12:38 PM
When someone gives you code to test, please actually test it before you simply declare that it has the same problem as your original code.