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

Lua help

Started by colcrunch, 17 September 2014 - 05:15 AM
colcrunch #1
Posted 17 September 2014 - 07:15 AM
First off, sorry the title isn't very descriptive, but I dont know really how to title this.
Ok, so I am currently writing a monitoring program to interface with a big reactor. And I have hit a snag.

The issue that I am having is that Big Reactors wants you to call reactor.getControlRodName() for each rod, same with reactor.getControlRodLevel(). I have 18 control rods, and I cant seem to figure out how to get the information without doing it individually 18 times.

Ideas?
Bomb Bloke #2
Posted 17 September 2014 - 01:41 PM
If there's no command to get them all at once, then that means you have to do them one ata time. Loops can be used so that you don't actually have to type one command for each rod, though.

For example, going by the commands here, we could do something like:

local reactor = peripheral.wrap("where ever")

local levelTotal = 0

for i=1, reactor.getNumberOfControlRods() do
	levelTotal = levelTotal + reactor.getControlRodLevel(i)
end

print("Average control rod insertion level is "..levelTotal/reactor.getNumberOfControlRods()..".")

… and that'd print the average insertion level, regardless as to how many rods were in the reactor.
colcrunch #3
Posted 17 September 2014 - 09:45 PM
Would this work?



i = 1

while true do
  local cr = reactor.getControlRodName(i)
  local cl = reactor.getControlRodLevel(i)
  print(cr,": ",cl)
  i = i + 1
if i > 18 then
break
end
end

Edited on 17 September 2014 - 09:21 PM
blipman17 #4
Posted 17 September 2014 - 10:26 PM
If there's no command to get them all at once, then that means you have to do them one ata time. Loops can be used so that you don't actually have to type one command for each rod, though.

putting that into a function makes it a lot easyer.

something else, you could do is putting it into a table, and then you could read it out
colcrunch #5
Posted 17 September 2014 - 11:59 PM
Would this work?



i = 1

while true do
  local cr = reactor.getControlRodName(i)
  local cl = reactor.getControlRodLevel(i)
  print(cr,": ",cl)
  i = i + 1
if i > 18 then
break
end
end


Ok, so this works, the only problem is that I get an error on line 115

local cr = reactor.getControlRodName(i)

I just get: program: 115: 18

It prints out all 18, I just dont want to see the error

–EDIT: It starts at 0, and cause I wasnt thinking, it should have been i > 17 not 18
Edited on 17 September 2014 - 11:00 PM
Bomb Bloke #6
Posted 18 September 2014 - 01:57 AM
–EDIT: It starts at 0, and cause I wasnt thinking, it should have been i > 17 not 18

Good point, I forgot about that.

While your "while" loop method works, generally if you want to count from one value to another while looping, a "for" loop'll be more efficient. This does the same thing as your loop for eg:

for i=0,17 do
  local cr = reactor.getControlRodName(i)
  local cl = reactor.getControlRodLevel(i)
  print(cr,": ",cl)
end

And you could even ditch the extra variables and just do this:

for i=0,17 do print(reactor.getControlRodName(i),": ",reactor.getControlRodLevel(i)) end