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

So...about that parallel function...

Started by Cranium, 26 February 2013 - 07:52 AM
Cranium #1
Posted 26 February 2013 - 08:52 AM
I ran into another snag while implementing soemthing really fun into my next program. It's supposed to be a nifty little dialogue between two unknown people. The problem is, I want static boxes running at the same time text does. When I try to do that, it will unfortunately not write the text in the proper place. It seems I might have to fanangle my own coroutines, but I have no idea how to use them. Can anyone help me?

Paint image here: http://pastebin.com/pk2Fz2p8

Code here:

local function unlock()
 local codec = fWolf and loadImageFromServer("codec") or paintutils.loadImage("codec")
 paintutils.drawImage(codec, 1, 3)
 local talking = true
 local function static()
  while talking do
   local staticColors = {
	 colors.white,
	 colors.gray,
	 colors.black,
	 colors.lightGray}
   for i = 2, 11 do
	for k = 3, 9 do
	 term.setCursorPos(i, k)
	 term.setBackgroundColor(staticColors[math.random(1,4)])
	 write(" ")
	end
   end
   for i = 41, 50 do
	for k = 3, 9 do
	 term.setCursorPos(i, k)
	 term.setBackgroundColor(staticColors[math.random(1,4)])
	 write(" ")
	end
   end
   sleep(.01)
  end
 end
 local function writeText()
  local chatTab = {
   {"Unknown voice 1: The user has found the",
    "    backdoor. Shall I terminate the connection",
    "    to the server?"},
   {"Unknown voice 2: No. Their dilligence has paid",
    "    off. Give them more tests. Scatter them",
    "    throughout cyberspace. Let's see if they",
    "    canfind all of the clues."},
   {"Unknown voice 1: Understood sir."}
   }
  term.setBackgroundColor(colors.black)
  term.setTextColor(colors.white)
  for i = 1, #chatTab do
   for k = 1, #chatTab[i] do
	term.setCursorPos(4, 12 + k) --It's not writing on the line specified here
	term.setBackgroundColor(colors.black)
	term.setTextColor(colors.white)
	textutils.slowWrite(chatTab[i][k], 10)
   end
   sleep(2)
   for v = 13, 17 do
	paintutils.drawLine(3, v, 49, v, colors.black)
   end
  end
  talking = false
 end
 
 parallel.waitForAll(static, writeText)
 sleep(2)
 background()
end
Bonus points go to those who can recognize what I'm trying to emulate.
remiX #2
Posted 26 February 2013 - 09:15 AM
It looks like it's doing this because of the other function (static)
The static function also sets the cursorpos every 0.1 seconds (20x? times faster than the writeText function). So this is probably causing the text for the other function to write in the wrong position.
Something like this:

.. stuff
writeText function sets cursorpos
writeText function sets background colour to black
and then static function function changes the cursorpos from the loops
then writeText function writes the text to the screen (completely messed up now)

My suggestion would be to add this at every term.setCursorPos

local cx, cy = term.getCursorPos()
-- code here where it sets the cursor pos
-- then do
term.setCursorPos( cx, cy )
-- so it will go back to where it was

So for one of your loops:
        for k = 3, 9 do
         local cx, cy = term.getCursorPos()
         term.setCursorPos(i, k)
         term.setBackgroundColor(staticColors[math.random(1,4)])
         write(" ")
         term.setCursorPos( cx, cy ) -- reset it after it writes
        end

The static function loops quite quickly but I'm sure this will work
Cranium #3
Posted 26 February 2013 - 09:45 AM
It looks like it's doing this because of the other function (static)
The static function also sets the cursorpos every 0.1 seconds (20x? times faster than the writeText function). So this is probably causing the text for the other function to write in the wrong position.
Something like this:

.. stuff
writeText function sets cursorpos
writeText function sets background colour to black
and then static function function changes the cursorpos from the loops
then writeText function writes the text to the screen (completely messed up now)

My suggestion would be to add this at every term.setCursorPos

local cx, cy = term.getCursorPos()
-- code here where it sets the cursor pos
-- then do
term.setCursorPos( cx, cy )
-- so it will go back to where it was

So for one of your loops:
		for k = 3, 9 do
		 local cx, cy = term.getCursorPos()
		 term.setCursorPos(i, k)
		 term.setBackgroundColor(staticColors[math.random(1,4)])
		 write(" ")
		 term.setCursorPos( cx, cy ) -- reset it after it writes
		end

The static function loops quite quickly but I'm sure this will work
That actually worked perfectly. I didn't think it would be so easy. I thought I would have had to learn coroutines for this little thing….