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

A way to make this code better

Started by ihatetn931, 19 December 2013 - 03:50 PM
ihatetn931 #1
Posted 19 December 2013 - 04:50 PM
I'm use openccsensor wireless sensor turtle to give me info about reactors and mfsus but it seems when i have them both running after a min i can't open the computer termianl or anything just like right click don't work.

This code needs to run all the time to keep the main computer updated with it, i haven't found another way to do it. I have 2 turtles that run basically this code, i can run 1 for awhile but it eventually bugs out as well if i don't restart the turtle everyonce in awhile. I thought about making functions for each thing but wouldn't that be the same as running the code like this

This is the code i have the problem with
Spoiler


os.loadAPI("ocs/apis/sensor")
local debug = 1
rednet.open("right")
while true do
local sR = sensor.wrap("left")
local mFsU = sR.getTargetDetails("0,-1,0")
local msg = textutils.serialize(mFsU)
rednet.broadcast(msg)
if debug == 1 then
print(textutils.unserialize(mFsU.Stored))
print(textutils.unserialize(mFsU.Capacity))
print(textutils.unserialize(mFsU.StoredPercentage))
print(textutils.unserialize(mFsU.Output))
end
sleep(1)
term.clear()
term.setCursorPos(1,1)
end
rednet.close("right")


This code is the reciver computer which recives info from both turtles, i tried to make it so i didn't need to add so many

local id,message = rednet.receive()
local scan = textutils.unserialize(message)

and

local id1,message1 = rednet.receive()
local scan1 = textutils.unserialize(message1)

But when i didn't have them in the loop it wouldn't update and if i just put them in the loop the graph won't draw

I have no problem with this code it works fine
Spoiler


os.loadAPI("ocs/apis/graph")



montop = peripheral.wrap("top")
monleft = peripheral.wrap("left")
monright = peripheral.wrap("right")
rednet.open("back")

gradient1 = {colors.blue, colors.cyan, colors.green, colors.lime}
gradient2 = {colors.purple, colors.magenta, colors.red, colors.orange}

--Menus


local id,message = rednet.receive()
local scan = textutils.unserialize(message)

local id1,message1 = rednet.receive()
local scan1 = textutils.unserialize(message1)

local cableSide = "bottom"

--montop.write(scan1.Heat)

function setBundledColor(side, color, state)
if state then
if not colors.test(rs.getBundledOutput(side), color) then
rs.setBundledOutput(side, colors.combine(rs.getBundledOutput(side), color))
end
else
if colors.test(rs.getBundledOutput(side), color) then
rs.setBundledOutput(side, colors.subtract(rs.getBundledOutput(side), color))
end
end
end

function checkReactorHeat()
if scan.Heat == 0 then
setBundledColor(cableSide, colors.lime, true)
else
setBundledColor(cableSide, colors.lime, false)
end
heatLevel = scan.Heat
return heatLevel
end

function checkBreederHeat()
if scan1.Heat == 0 then
setBundledColor(cableSide, colors.white, false)
else
setBundledColor(cableSide, colors.white, true)
end
heatLevel1 = scan1.Heat
return heatLevel1
end

function checkReactorEng()
if scan.Output > 0 then
eNg = scan.Output
else
eNg = scan.Output +1
end
return eNg
end

function checkBreederEng()
if scan1.EnergyEmitted > 0 then
eNg1 = scan1.EnergyEmitted
else
eNg1 = scan1.EnergyEmitted +1
end
return eNg1
end

--local grtop, err = graph.new(montop, {checkReactorHeat ,checkBreederHeat, checkReactorEng, checkBreederEng}, {"Reactor 1 Heat", "Breeder 1 Heat", "Reactor 1 Energy Output", "Breeder 1 Energy Output"}, {gradient1, gradient2, gradient3, gradient4}, {0, 0, 0, 0}, {scan.MaxHeat, scan1.MaxHeat, scan.EnergyEmitted, scan1.EnergyEmitted}, true)
local grleft, err = graph.new(monleft, {checkReactorHeat ,checkBreederHeat}, {"Reactor 1 Heat", "Breeder 1 Heat"}, {gradient1, gradient2}, {0, 0}, {scan.MaxHeat, scan1.MaxHeat}, true)
local grright, err = graph.new(monright, {checkReactorEng, checkBreederEng}, {"Reactor 1 Energy Output", "Breeder 1 Energy Output"}, {gradient1, gradient2}, {0, 0}, {scan.EnergyEmitted, scan1.EnergyEmitted}, true)
while true do
id,message = rednet.receive()
scan = textutils.unserialize(message)
id1,message1 = rednet.receive()
scan1 = textutils.unserialize(message1)
--grtop:draw()
grleft:draw()
grright:draw()
-- montop.setCursorPos(1,1)
-- montop.write(scan1.Heat)
--montop.clear()
sleep(1)
end
rednet.close("back")

As you can tell it's prolly not the best way to code this but it's all i know at the moment. Is it just me or does the code tag remove me indentation
Edited on 20 December 2013 - 07:28 PM
ihatetn931 #2
Posted 20 December 2013 - 10:29 PM
So i tried to put it all in functions hoping that would change somthing but it had no effect, Must be a way to run the code all the time without having to crash out cc
Bomb Bloke #3
Posted 20 December 2013 - 11:15 PM
Hrm. What do you have to do to get the turtles/computers working again? Just break/replace them, or…? Are only some of your systems affected, or all of them?

What versions of MineCraft and ComputerCraft are you using?
ihatetn931 #4
Posted 20 December 2013 - 11:19 PM
Hrm. What do you have to do to get the turtles/computers working again? Just break/replace them, or…? Are only some of your systems affected, or all of them?

What versions of MineCraft and ComputerCraft are you using?

I close the game with the "X" I'm using minecraft 1.4.7 If i save and quit it just sits at "shut down interneal server" and it never closes. All of the systems are effected when both turtles loops run for like a min or 2 but if i just run one it will run for 30 mins or so then freeze up, It's almost like it disabled all GUIS in game
Bomb Bloke #5
Posted 20 December 2013 - 11:27 PM
Ah. That's a game-killing bug in the version of ComputerCraft you're using, triggered at random when turtles try to use rednet. I'm afraid the only way to install a fixed version of ComputerCraft is to move past MineCraft 1.4.x. Sorry.

In the meantime, don't bother about trying to use rednet with your turtles - lowering the amount of signals they send will make the stalls less frequent, but so long as they're using it you'll keep on getting them at random.
ihatetn931 #6
Posted 20 December 2013 - 11:32 PM
Ah. That's a game-killing bug in the version of ComputerCraft you're using, triggered at random when turtles try to use rednet. I'm afraid the only way to install a fixed version of ComputerCraft is to move past MineCraft 1.4.x. Sorry.

In the meantime, don't bother about trying to use rednet with your turtles - lowering the amount of signals they send will make the stalls less frequent, but so long as they're using it you'll keep on getting them at random.

Thank you for your help. i guess i won't use this modpack but then i guess i loose redpower
minecraftspare #7
Posted 20 December 2013 - 11:34 PM
ok so one problem i can see i dont know it is the cause but does the
local sR = sensor.wrap("left")
need to be inside the while true loop because if it is a ccsensor block the once it is wrapped once it doesnt need re-wrapping until you restart the program
ihatetn931 #8
Posted 21 December 2013 - 12:05 AM
ok so one problem i can see i dont know it is the cause but does the
local sR = sensor.wrap("left")
need to be inside the while true loop because if it is a ccsensor block the once it is wrapped once it doesnt need re-wrapping until you restart the program

Thank you will give that try, i think i tried it outta the loop but it didn't update correctly but i could be wrong
Bomb Bloke #9
Posted 21 December 2013 - 12:49 AM
While it is inefficient to have that inside the loop, it doesn't "need" to be outside it. That certainly doesn't affect the stall. And note that it's "a whole of MineCraft's server thread" stall. You'll find that when the computers become unresponsive it's not even possible to mine blocks.

I should probably also mention that the issue isn't with the rednet API exactly, but rather it's with wireless turtle communications in general. I've searched for workarounds myself with no success.

Most of Redpower's functionality has since been replaced by other mods. In particular, Minefactory Reloaded offers "rednet" cables which are equivalent to Redpower's bundled ones.
ihatetn931 #10
Posted 21 December 2013 - 10:29 AM
Sweet, now i just have to figure out which mods i'm gonna need to get the same fuctionality i got now, should be fun