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

broadcasting data on changed variables.

Started by qwerty, 20 October 2018 - 08:30 AM
qwerty #1
Posted 20 October 2018 - 10:30 AM
Hi everyone!
I've been working on an Extreme Reactors reactor controller and now I've decided to add networking capabilities such as data readouts but I'm having trouble sending only when the stats update.
(here's my code)

local function clear()
term.clear()
term.setCursorPos(1,1)
end
--defining variables and setting things
local reactor = peripheral.find("BigReactors-Reactor")
local x, y = term.getSize()
os.loadAPI("SliderAPI")
SliderAPI.createSlider("e", 2, 2, x-2, 3, colors.red, colors.gray)
local modem = peripheral.find("modem")
rednet.open("right") --assuming the modem is on top of the reactor controller
modem.open(65535)
while true do --main loop
clear()
--math stuff
local cap = reactor.getEnergyCapacity()
local strd = reactor.getEnergyStored()
local tlen = tostring(stdr,cap) --there are better ways of doing this but idk how
local len = string.len(tlen .."	") --without the added string the text wouldn't center properly, i don't know why
local percent = math.floor((strd/cap) * 100) -- floor it? yes, no, NO DONT FLOOR IT! FLOOR IT!!!
local tPercent = percent
--slide stuff
SliderAPI.updateSlider("e", percent)
SliderAPI.draw("e")
--rednet stuff
if tPercent ~= percent then
rednet.broadcast(percent,"percent")
rednet.broadcast(tostring(reactor.getActive()),"reacActive")
local cast = tostring(strd..","..cap)
rednet.broadcast(cast,"energy")
end
--write stuff
term.setCursorPos( x/2-len , y)
write(strd.."/"..cap)
term.setCursorPos( x/2-2, 1)
write("Energy")
sleep(0.1)
clear()
--controlling the reactor
reactor.setAllControlRodLevels(percent)
if percent >= 99 then reactor.setActive(false) else
reactor.setActive(true)
end
end
I also took the liberty to include the infamous sliders API i found on the forums, but that's not important.
if anyone has a better idea than declaring a variable and checking if it's still equal, it'd be very apreciated!
Sincereley:
Qwerty.
Edited on 20 October 2018 - 09:03 AM
Lupus590 #2
Posted 20 October 2018 - 10:48 AM
why are you opening the modem channel yourself when you are using rednet?
qwerty #3
Posted 20 October 2018 - 10:52 AM
why are you opening the modem channel yourself when you are using rednet?
don't judge, ok?
it's just an old habit. (old habits die hard)
osmarks #4
Posted 20 October 2018 - 05:45 PM
Please indent it.
SquidDev #5
Posted 20 October 2018 - 10:21 PM
One thing which stands out is this code:

local percent = math.floor((strd/cap) * 100) -- floor it? yes, no, NO DONT FLOOR IT! FLOOR IT!!!
local tPercent = percent
--slide stuff...
--rednet stuff
if tPercent ~= percent then
You never change the value of tPercent or percent between these two calls, and so that if statement will always be false. It's possible you want to compare against the value from the previous iteration instead, but all the variables are declared within the loop.