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

parallel api

Started by flo00297, 14 January 2017 - 08:55 AM
flo00297 #1
Posted 14 January 2017 - 09:55 AM
I'm a bad english speaker/writer so please don't blame me :)/>
I have a problem with the parallel API. Starting the Programm does nothing and i can't terminate it anymore. I have to disconnect the monitors so that the computer doesn't find the peripherals, then i can use the computer again. Here is the code: http://pastebin.com/pH7K8UxK

Another proble is using the paintutils API. If i run "clear" on a monitor with a graphic the used color become the backgroundcolor and i can't clear it anymore to black. That's why i draw a black Pixel at 0,0 and clear so the color became black again… Is that normal? I'm playing FTB Direwolf20 1.7.10. Pls help.
Bomb Bloke #2
Posted 14 January 2017 - 12:09 PM
You're attempting to start functions through the parallel API before you've defined those functions. Assuming that doesn't just crash, that'd mean you've got old copies of those functions floating around in the global scope from an older version of your script. Rebooting the computer would probably lead the script to error instead.

So yeah, move those function definitions above the point where you try to actually use them.

term.clear() sets the entire terminal to its current background colour. Do term.setBackgroundColour(colours.black) if you want to be sure the background colour is black.
flo00297 #3
Posted 14 January 2017 - 05:11 PM
Thank you for your answer but it still does not work…
I've created a new paste because it was uploaded by a computer. Link here: http://pastebin.com/GDthxATY

If I try to chance the background color nothing happens. and i can see, that the monitors are connected because everything else works. Maybe it's something with FTB because in FTB Ultimate 1.4.7 it's not a problem to change Background colors. For information: Direwolf20 1.10.0, Minecraft 1.7.10, Computercraft 1.75.
Bomb Bloke #4
Posted 15 January 2017 - 02:38 AM
I see you've added a term.setBackgroundColor(colors.red) AFTER a term.clear()… if you wanted to paint the screen red, you might have better luck setting the background colour before the clear.

The idea is that setting the background colour doesn't change the display right away: it affects any writes you make to the screen afterwards.

parallel.waitForAny() converts multiple functions into coroutines, executes them, then stops all of them as soon as any complete. Your draw_reactor_off() function will complete nearly as soon as it's first started (it doesn't loop or anything), so your output() function will never even begin.

You probably don't want to be using the parallel API with this script.
flo00297 #5
Posted 15 January 2017 - 08:05 PM
Thank you very much but how can I run the "output" function near the hole program so that the poweroutput is always displayed and updated?
Bomb Bloke #6
Posted 16 January 2017 - 01:18 AM
Personally, I'd replace your parallel API call with the content of your output function, and call draw_reactor_on/off() from within that loop.
flo00297 #7
Posted 16 January 2017 - 07:59 PM
Could you please give me an example?
Bomb Bloke #8
Posted 17 January 2017 - 09:51 AM
Something along these lines may be better:

Spoiler
--------------------------------------------
--# Declare Variables
--------------------------------------------

local sec = peripheral.wrap("monitor_4")
local mon = peripheral.wrap("monitor_5")

local reactor = peripheral.wrap("BigReactors-Reactor_1")

local myTimer = os.startTimer(0)

local oldTerm = term.current()

--------------------------------------------
--# Init
--------------------------------------------

term.redirect(sec)
term.setBackgroundColor(colors.black)
term.clear()
term.setCursorPos(1,1)
textutils.slowPrint("--------------------------------------------")  --# This is a waste of time.
sleep(1)
term.clear()

--------------------------------------------
--# Main Program
--------------------------------------------

while true do
	local event, par1, par2, par3 = os.pullEvent()
	
	if event == "timer" and par1 == myTimer then
		term.redirect(sec)
		term.clear()
		term.setCursorPos(1,1)
		term.write(reactor.getEnergyProducedLastTick().."RF/Tick")

		term.redirect(mon)
		term.setBackgroundColor(colors.black)
		term.clear()

		if reactor.getActive() then
			--# Reactor is on.
			paintutils.drawBox(31,3,48,17,colors.white)
			paintutils.drawFilledBox(33,5,37,15,colors.blue)
			paintutils.drawFilledBox(42,5,46,15,colors.blue)
			paintutils.drawBox(39,4,40,16,colors.yellow)

			local y2 = reactor.getFuelAmount() / reactor.getFuelAmountMax()

			if y2 <= 0.1 then
				paintutils.drawBox(39,4,40,16,colors.red)
			elseif y2 < 0.95 then
				y2 = y2 * 12 + 4
				paintutils.drawBox(39,4,40,y2,colors.red)
			end
		else
			--# Reactor is off.
			paintutils.drawBox(31,3,48,17,colors.gray)
			paintutils.drawFilledBox(33,5,37,15,colors.gray)
			paintutils.drawFilledBox(42,5,46,15,colors.gray)
			paintutils.drawBox(39,4,40,16,colors.gray)
		end
	
		myTimer = os.startTimer(5)  --# Or whatever duration.
		
	elseif event == "mouse_click" then
		--# par3 / par4 are the x / y co-ords; test them to see where
		--# the user clicked, then maybe do something about it...
		
	elseif event == "key" and par1 == keys.q then
		os.pullEvent("char")
		break  --# User pressed "q"; exit the loop.
	
	end
end

term.redirect(oldTerm)
flo00297 #9
Posted 17 January 2017 - 12:29 PM
Thank you very much for your help :)/>
flo00297 #10
Posted 21 January 2017 - 11:06 PM
I have another question about your Programm. I ´paste it to my Computer but after the slowprint nothing happens. Is that because of the timer or what?
Bomb Bloke #11
Posted 21 January 2017 - 11:30 PM
Sort of - the timer actually needs to be set after your sleep() / slowPrint() calls. Try moving that line down.