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

multiple monitors, one computer

Started by thetrin777, 22 December 2014 - 06:13 PM
thetrin777 #1
Posted 22 December 2014 - 07:13 PM
Hello Computercraft forums! This is my first post so I had to say hi. So. … hi!

I have done quite a bit of looking around and haven't found what I need. I'm building a combat arena for some pvp type awesomeness. And I want to time the matches, using a large monitor to display the countdown to the players.

Here's the question.

I've got four big monitors (5 tall by 4 wide) built into the walls. Is there some way to make all four display the same thing coming from one central computer?

Any help would be magnificent. Thanks in advance all you gurus out there!
Cozzimoto #2
Posted 23 December 2014 - 02:21 AM
A quick and dirty way of doing it would be wrapping the monitors in a table, create a function for drawing your screen and then looping through your table of monitors and drawing to all the monitors.

I can give you a simple script later if you need one. Currently out and about browsing on my mobile.
KingofGamesYami #3
Posted 23 December 2014 - 02:37 AM
I was actually trying to do this earlier, my approach is a little intensive on the processing power though.

Here's what I was attempting:

1 - Find all monitors

2 - Tie a new instance of the program to each monitor

3 - Run all of them in a custom coroutine manager

There are two issues with this: 1 - monitor_touch / mouse_click is a little broken, 2 - math.random.
theoriginalbit #4
Posted 23 December 2014 - 10:52 AM
-snip-
this solution is overkill.

The OP stated that they have four monitors all of the same size, the simplest solution would be to define a function which performs the action on all the monitors

--# all the monitors to draw on
local monitors = {
  peripheral.wrap( "monitor_0" ),
  peripheral.wrap( "monitor_1" ),
  peripheral.wrap( "monitor_2" ),
  peripheral.wrap( "monitor_3" ),
}

local function clear()
  for _,mon in pairs( monitors ) do
    mon.clear()
  end
end

local function write( ... )
  for _,mon in pairs( monitors ) do
    mon.write( ... )
  end
end
Bomb Bloke #5
Posted 23 December 2014 - 12:06 PM
Wasn't there another post here before…?

I'd make a redirect object:

local monitors = {peripheral.find("monitor")}  -- "find" needs CC 1.6 or later

local multiTerm = {}
for funcName,_ in pairs(monitors[1]) do
	multiTerm[funcName] = function(...)
		for i=1,#monitors-1 do monitors[i][funcName](unpack(arg)) end
		return monitors[#monitors][funcName](unpack(arg))
	end
end
term.redirect(multiTerm)

term.clear()
term.setCursorPos(1,1)
print("Hello world!")
Lyqyd #6
Posted 23 December 2014 - 04:33 PM
To prevent unhelpful/inaccurate/incorrect information being posted again, the correct way to place multiple monitors of a given type in a table is:


local monitors = {peripheral.find("monitor")}

This method gives you an easily iterable list, and ensures that only monitor-type peripherals are wrapped. A couple of posts by Cozzimoto were removed due to problematic and/or confusing code.
thetrin777 #7
Posted 23 December 2014 - 09:52 PM
Thanks to everyone for getting back to me so fast. :)/>

Something I forgot to mention I needed help with…

The way this arena is built, I need to figure how to physically link these monitors to the computer. Like, would rednet cabling work? I dont think WR-CBE will work. And if it does i cant figure it out lol.

Anyway, sorry if this kinda throws a wrench in this… ill keep working at it but all help is yet again appreciated.

BTW if some screenshots would clarify anything i can get some.
Lyqyd #8
Posted 23 December 2014 - 10:33 PM
The best option is ComputerCraft's networking cables, which much of the code suggested above assumes you are using.
Bomb Bloke #9
Posted 24 December 2014 - 06:02 AM
The idea is that you slap a wired modem on the computer plus each monitor, then use the networking cable to link them all together. Once that's done, use (right-click) each monitor's modem to "connect" them, then try running eg the code snippet I posted above.
thetrin777 #10
Posted 26 December 2014 - 07:47 PM
The idea is that you slap a wired modem on the computer plus each monitor, then use the networking cable to link them all together. Once that's done, use (right-click) each monitor's modem to "connect" them, then try running eg the code snippet I posted above.

I got it all set up and it works once I corrected all my typos. Lol

I'm about to start writing my counter program, but before I do , can you explain this code a little bit,? I'm still really new to lua and though it works I'd like to understand how it works.
Bomb Bloke #11
Posted 26 December 2014 - 11:08 PM
Difficult to write an explanation that's anywhere near as short as the code. I'll try to give a basic overview.

The idea is that term - the table that holds all the functions you use to write to the default display - can be replaced by a table of functions that write to a different display (using term.redirect()).

When you wrap an external monitor, you get a table filled with functions that write to that monitor (peripheral.find() offers a way to wrap lots of peripherals of a similar type quickly). This is an example of the sort of table you might redirect term to.

So, the code works by first filling a table with wrapped monitor tables, then generating a new table containing one function for every function which exists in those monitor tables (using an iterator for loop) - these new functions run the equivalent functions on all wrapped monitors when you call them, using another for loop.

The end result is a table that you can redirect the terminal to that'll result in all subsequent screen writes being performed on all your attached monitors.

You could, if you wanted, use this table without redirecting to it. For example, if you skipped out on the term.redirect(multiTerm) line, you could instead use commands like multiTerm.setCursorPos(1,1) or multiTerm.write("whatever") to deal with the collection of monitors.

The benefit of redirecting is that certain commands - such as print(), which handles display scrolling and word-wrap for you, as well as all the commands in the paintutils API - will only work on the current terminal.
thetrin777 #12
Posted 26 December 2014 - 11:41 PM
OK its a little clearer. Thank you very much! Guess all thats left to do now is incorporate my countdown timer into this.