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

Improvements on reactor script

Started by GreaseMonk, 16 August 2015 - 09:16 AM
GreaseMonk #1
Posted 16 August 2015 - 11:16 AM
Hello everybody,

I'm new to the lua language, could anyone comment how am I doing ? Where can I improve this script ?
i've been cracking over the lua language all night today, I like it so far.

The purpose is just to monitor a reactor so it can activate the reactor for a bit once the internal battery is lower than 50%.
Also, i have some other computers hanging around with a simple 'progress bar' on the energy hogging process, they receive the percentage remaining through channel 1200.

The scripts are a work-in-progress, I plan on adding support for communicating with AE2 to get the yellorium ingots delivered.

Reactor computer: http://pastebin.com/RL5J3VY2
(and no, i did not cheat on this reactor, i actually placed ~1200 buckets of source buckets of gelid cryotheum in dw20 :D/>)

Spoiler

Monitoring computer(s): http://pastebin.com/zexcBn6q

Spoiler
HPWebcamAble #2
Posted 17 August 2015 - 12:52 AM
Not bad for your first Lua program, it defiantly does what its supposed to.

There are a few things you can do to improve it though:

- If you are using CC 1.6 or higher, you can use 'peripheral.find()' http://www.computerc...Peripheral.find

- Instead of using 'term.redirect( monitor )', you can just do 'monitor.<function>', monitors have the same functions as the term (plus .setTextScale)
This way, you can use the computer terminal and the monitor to display info

- Instead of having the program freeze for 30 seconds, you can use os.startTimer( seconds ), and os.pullEvent() to wait for the result (You'll want to read up on these)
The advantange to this is you can accept input from the user, while waiting for the timer to end.
Although this program doesn't need user input, you may want to add it in the future.
Edited on 16 August 2015 - 10:54 PM
GreaseMonk #3
Posted 18 August 2015 - 01:33 AM
That timer function would definiately be handy for when the buttons are being implemented. Cheers!
Lyqyd #4
Posted 18 August 2015 - 02:00 AM
- Instead of using 'term.redirect( monitor )', you can just do 'monitor.<function>', monitors have the same functions as the term (plus .setTextScale)
This way, you can use the computer terminal and the monitor to display info

He has a print call in there that will require the monitor to be the current term target if it's going to print on the monitor. Also, the paintutils calls only use the current term object, so they also require the monitor to be redirected to.
HPWebcamAble #5
Posted 18 August 2015 - 04:03 AM
He has a print call that will require the monitor to be the current term target if it's going to print on the monitor…paintutils calls only use the current term object

The script would take some modification, no doubt about that!
But you gain more flexibility from directly calling monitor functions, rather than redirecting to the monitor.
Bomb Bloke #6
Posted 18 August 2015 - 04:10 AM
Er, how so? You can always redirect back, and in the meantime you're not having to make your own print/paintutils/etc functions (which you can do anyway, if you for some reason want to).
Lyqyd #7
Posted 18 August 2015 - 06:13 AM
Yeah, there's not really anything to be gained by not redirecting to the monitor for those calls. It's a handy tool, might as well use it.
HPWebcamAble #8
Posted 18 August 2015 - 07:14 AM
Good points.

In this program, I guess none of this is very relevant, as it doesn't even use both.

But in a program that DOES use a monitor and the computer terminal, creating a few functions like this could be helpful:

local function mPrint(...)
  local cur = term.current()  
  term.redirect(monitor)
  print(...)
  term.redirect(cur)
end

or even better:


local monitor = peripheral.wrap("right")

monitor.print = function(...)
  local cur = term.current()  
  term.redirect(monitor)
  print(...)
  term.redirect(cur)
end