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

Updating Variables in "While" Loops (and cloning monitors)

Started by The Lexiconical, 08 April 2017 - 12:45 PM
The Lexiconical #1
Posted 08 April 2017 - 02:45 PM
I'm by no means an expert, but I've been getting the hang of Computercraft and decided to make my own program to monitor my BigReactor at a glance. It's nothing fancy and I have most of it functional, except I can't get the program to loop and update the variables e.g. reactor.getEnergyStored(). The only thing that will update is reactor.getActive() - which will tell me and update whether the reactor is on or off just fine - so I assume the loop is working.

From what I've seen trying to resolve this issue, it might not be as simple of a fix as I hope it is. I've linked the code below.

https://pastebin.com/uykhhxjs

Another problem I've had is eventually I'd like to be able to display the same information from the reactor to other monitors around my base (cloning the displays, basically), but I can't figure that out either.

I'd appreciate any help.
The Lexiconical #2
Posted 08 April 2017 - 03:25 PM
While fumbling around some more I found a solution to the non-updating variables problem. If anyone's interested I've linked how the code looks now.

https://pastebin.com/wi3jqg0r
Larry84 #3
Posted 08 April 2017 - 03:59 PM
That's because you haven't inserted the functions in the loop.

When you want a variable like that to update every cycle, you have to include the function into the loop, as it will just repeat the processes between "while" and "end", not the entire code! Here you are just rewriting again and again the same nubers onto the screen, which are read when the program starts up.When you assign a function to a variable like you have done, the variable takes the output of the function at that moment and whenever it's recalled it just gives the same number, without recalling the function.

I suggest you to move the three reading functions into the "stats" function to resolve the problem. Also, you don't need to rewrite every cycle the "start" function: let it just run once, before the loop Just noticed that you clear the monitor after every loop. You definitely need that.

Spoiler

--Setup
reactor = peripheral.wrap("BigReactors-Reactor_2")
mon = peripheral.wrap("monitor_22")

maxFuel = reactor.getFuelAmountMax()


--Costmetic Terminal
term.clear()
term.setCursorPos(1, 1)
print("LexiOS: Reactor Edition V7.37")


function start()
--Cosmetic Monitor
mon.setTextScale(0.5)
mon.setTextColor(colors.cyan)
mon.setCursorPos(1, 1)
mon.write("LexiOS: ")
mon.setTextColor(colors.white)
mon.write("Reactor Edition V7.37")
end

function status()
--Reactor Online/Offline
if reactor.getActive() == true then
  mon.setCursorPos(8, 3)
  mon.write("Reactor:")
  mon.setCursorPos(17, 3)
  mon.setTextColor(colors.lime)
  mon.write("Online")
  mon.setTextColor(colors.white)
	else
	mon.setCursorPos(8, 3)
	mon.write("Reactor:")
	mon.setCursorPos(17, 3)
	mon.setTextColor(colors.red)
	mon.write("Offline")
	mon.setTextColor(colors.white)
	end
end
  
function stats()
--Updating Stats
power = reactor.getEnergyStored()
fuel = reactor.getFuelAmount()
re = reactor.getEnergyProducedLastTick()
--Energy Stored
mon.setCursorPos(2, 5)
mon.write("Energy Buffer: "..power.." ")
mon.setTextColor(colors.purple)
mon.setCursorPos(26, 5)
mon.write("RF")
mon.setTextColor(colors.white)

--Fuel
mon.setCursorPos(11, 7)
mon.write("Fuel: "..fuel.." ")
mon.setTextColor(colors.purple)
mon.setCursorPos(26, 7)
mon.write("%")
mon.setTextColor(colors.white)

--RE
mon.setCursorPos(11, 9)
mon.write("RF/t: "..re.." ")
end


while true do

start()
status()
stats()
os.sleep(3)
mon.clear()

end
Edited on 08 April 2017 - 02:11 PM