Code Help (ComputerCraft)

  • Please make sure you are posting in the correct place. Server ads go here and modpack bugs go here
  • The FTB Forum is now read-only, and is here as an archive. To participate in our community discussions, please join our Discord! https://ftb.team/discord

Zen300

New Member
Jul 29, 2019
94
0
0
Okay I have the following code:
while true do
evt, msg = os.pullEvent()

function energy()
amount = test.callRemote("batbox_0","getStored")​
amount2 = test.callRemote("batbox_1","getStored")​
glass.addBox(5,5,70,0xCCCCCC,0.01)​
total = glass.addText(6,6,"",0xFF0000)​
total.setText(tostring(amount+amount2).. " EU")​
end
exitWhile = false

if evt =="chat_command" and msg == "power" then
glass.addText(60,60,"entering loop",0xFF0000)​
while (not exitWhile) do​
energy()​
if evt == "chat_command" and msg == "clear" then​
glass.addText(60,60,"exiting the loop",0xFF0000)​
exitWhile = true​
glass.clear()​
end​
sleep(0.1)​
glass.clear()​
end
end
end

Basic premise:
I boot up the computer, type in $$power and get a numeric value that increases in realtime. And then I can type in $$clear and clear the EU off the screen. Using a while loop in it to make sure the numeric value is increasing correctly. The text is in there in my failed attempt at debugging... The entering loop flashes on the screen, but the exiting the loop does not.

my hope is that I can have several of these all running within the over-arching while loop. Allowing me to pull up any info I desire (boiler temps, total EU in storage, total MJ in storage, etc) and at the same time clear any of the data I call up off of the screen.


Sooo... Did I miss something in the code? Not take something into account that I should have?
Is there a for sure way to end a while loop? Or is there a different loop that will work that can be ended easily?


Thanks for any help that can be provided...
~Zen
 

Zen300

New Member
Jul 29, 2019
94
0
0
Sorry about that enigmius. Should be fixed now.

If there is no way to terminate an inprogress "while true do" statement then I'll be back to the drawing board.
 

Enigmius1

New Member
Jul 29, 2019
499
0
0
I suspect it has something to do with the fact that you're not managing the events properly. Specifically, you make one call to os.pullEvent at the very beginning of the main loop, assigning a value to each of two variables. You then have your 'energy' function (odd placement, but that shouldn't be hurting anything). Then you have your main code block, which is entered if the values assigned to evt and msg match your initial criteria. Within that code block you check them again, except you haven't done anything between the first check ('power') and the second check ('clear') that would create the possibility that the value(s) have changed since the first check. It looks to me like it would be better organized as:

Code:
function energy ()
    <things>
end
 
Looping = true
 
while Looping do
    evt, msg = os.pullEvent()
    if 'chat_command' and 'power' then
        <stuff>
    elseif 'chat_command' and 'clear' then
        <stuff>
    elseif 'chat_command' and 'stopit' then
        Looping = false
    end
end

Edit to add: the above bit sorts out the logic but doesn't account for a fluid loop updating values between user inputs. For something like that you'd want to take a close look at the parallel API and consider having one function to handle the output and another to handle user input, and then run them in parallel.

eg.

Code:
evn, msg = '', ''
-- note variables are declared globally so that both functions will have access to them
 
outputMgr = function ()
    <stuff similar to your energy function>
end
 
inputMgr = function ()
    evn, msg = os.pullEvent()
end
 
looping = true
 
while looping do
    parallel.waitForAny (outputMgr, inputMgr)
    if (evn == 'chat_command') and (msg = 'end') then
        looping = false
    end
end

Doing it this way would allow the outputMgr function to continue running while the inputMgr function was waiting for an event. I'm just offering a very basic example but there's a lot you can do with it. Definitely give it a good look on the CC wiki.
 

Zen300

New Member
Jul 29, 2019
94
0
0
Okay this is the current itteration of the code:

hud = peripheral.wrap("top")
test = peripheral.wrap("right")

function energy()
amount = test.callRemote("batbox_6","getStored")​
total = hud.addText(6,6,"",0xFF0000)​
total.setText(tostring(amount).. " EU")​
end

while true do
Looping = true

while Looping do
evt, msg = os.pullEvent()​
if evt == "chat_command" and msg == "power" then​
hud.clear()​
energy()​
sleep(0.1)​
elseif evt == "chat_command" and msg = "clear" then​
hud.clear()​
end​
end

end

I tried both of the examples you gave me Enigmius...
The quote above actually is the better outcome of the two so far. The clear worked to remove the text from the screen... Only the text on the screen wasn't looping. When I typed in $$power I'd only get the EU amount in the MFSU at the moment I typed in $$power, it wouldn't be a realtime update of it like the original code had it doing.


The second one would do something odd coloring wise in the code (which then prevented it from working). When I put in the:
evt, msg = "", ""

It wouldn't see the last " mark in there. The rest would all turn to red and the last would remain white... And I couldn't get it to stop that and the code then just seemed to always give me back an error about that line. It was saying a syntax error for that line.
 

Enigmius1

New Member
Jul 29, 2019
499
0
0
The reason the code you used isn't updating the display real-time is because os.pullEvent will sit and wait until there is an event to pull before it moves to the next instruction. That's why I suggested using the parralel API, so you can have os.pullEvent waiting while the other function is updating with the relevant information in a loop.

evt, msg = '', ''

is declaring two string variables with no value. Note that I didn't use quotes, I used apostrophes. I'm not sure that it would matter but it shouldn't be causing an issue, and it shouldn't be something you can't sort out on your own.