Jump to content




broadcasting data on changed variables.

networking

4 replies to this topic

#1 qwerty

  • Members
  • 72 posts
  • LocationThe Sand Dunes

Posted 20 October 2018 - 09: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 by qwerty, 20 October 2018 - 10:03 AM.


#2 Lupus590

  • Members
  • 2,029 posts
  • LocationUK

Posted 20 October 2018 - 09:48 AM

why are you opening the modem channel yourself when you are using rednet?

#3 qwerty

  • Members
  • 72 posts
  • LocationThe Sand Dunes

Posted 20 October 2018 - 09:52 AM

View PostLupus590, on 20 October 2018 - 09:48 AM, said:

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)

#4 osmarks

  • Members
  • 22 posts

Posted 20 October 2018 - 04:45 PM

Please indent it.

#5 SquidDev

    Frickin' laser beams | Resident Necromancer

  • Members
  • 1,427 posts
  • LocationDoes anyone put something serious here?

Posted 20 October 2018 - 09: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.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users