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

Running one program simultainiously on two monitors

Started by em_niss, 14 August 2015 - 11:27 PM
em_niss #1
Posted 15 August 2015 - 01:27 AM
I have this problem where I want to have two monitors showing the same program at the same time (because of symetry :)/> ).
Currently I have the program running on one monitor and I'm currently wraping the monitor like this:
mon = peripheral.wrap("top").
I wonder if there is a way to store two monitors in one variable (in this case mon) so when I call for example mon.write("Hello") both monitors write Hello?
Edited on 14 August 2015 - 11:49 PM
KingofGamesYami #2
Posted 15 August 2015 - 03:12 AM
Well, you can do what you want, but it's rather hackish and usually has some problems.


local mon1, mon2 = peripheral.wrap( "top" ), peripheral.wrap( "left" )

local mon = {} --#make a blank table
for k, v in pairs( mon1 ) do --#iterate through the functions of mon1
  mon[ k ] = function( ... ) --#make a new function that calls both monitors' functions.
    local result = {v( ... )} --#this'll call mon1's function, and store any values returned
    mon2[ k ]( ... ) --#this just calls mon2's function
    return unpack( result ) --#this returns whatever mon1's function would've returned.
  end
end
mon.write( "Hello" )

Note: If you call something like mon.getSize, the result will be equivalent to calling mon1.getSize, because mon2's results are not store and returned. Theoretically, you could do so, effectively doubling the number of results you get.
Exerro #3
Posted 15 August 2015 - 12:46 PM
The 3 lines:

	local result = {v( ... )} --#this'll call mon1's function, and store any values returned
	mon2[ k ]( ... ) --#this just calls mon2's function
	return unpack( result ) --#this returns whatever mon1's function would've returned.

can be simplified to this, assuming the order in which the functions are called doesn't matter:

	mon2[ k ]( ... ) --#this just calls mon2's function
	return v( ... ) --#this returns whatever mon1's function would've returned.

Also, you may want to consider changing 'monitor_touch' events on either monitor to 'mouse_click' events so you can interact with the monitor. Not sure if this is what you want though.
Edited on 15 August 2015 - 10:46 AM
em_niss #4
Posted 18 August 2015 - 07:26 PM
Thanks for the replies! I'll try this as soon as I can!