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

Multi monitors.

Started by Kristopher_Jones, 31 October 2015 - 06:38 AM
Kristopher_Jones #1
Posted 31 October 2015 - 07:38 AM
Good morning programmers!

I want to make next scoreboard for multy sport arena and there i need at least 4 monitors(scorboard). All monitorsare connected but i want that all 4 monitors shows same.
If i try "find" then worksonly last monitor.
Thereare my code:

Spoilerlocal mon = peripheral.wrap('monitor_2')
mon.clear()
mon.setCursorPos(1,1)
mon.write("CA 70")
term.clear()
print("CA700 scoreboard system.")
sleep(2)
print("Loading data.")
sleep(2)
print("Reseting time lamp.")
rs.setOutput("right",true)
sleep(2)
print("Launching scoreboard.")
mon.clear()
mon.setTextColor(colors.black)
mon.setCursorPos(1,2)
mon.setTextScale(5)

rs.setOutput("right",false)
sleep(3)
rs.setOutput("right",false)
rs.setOutput("right",true)
term.clear()
term.setCursorPos(1,1)
print("CA700") –only need minutes
print("1-nos 2- t 3-p 4- arez 5- brez")
local mon = peripheral.wrap('monitor_2')
mon.clear()
mon.setCursorPos(1,1)
mon.setTextScale(3)
mon.write("VEF INZENIERU CENTRS")
mon.setBackgroundColor(colors.black)
mon.setTextColor(colors.white)
mon.setTextScale(5)

mon.setCursorPos(1,1)
mon.write("CA70")
mon.setCursorPos(8,3)

mon.write("1")

mon.setCursorPos(1,1)
mon.setTextScale(5)
mon.setCursorPos(7,5)
mon.write("–")
mon.setCursorPos(8,5)
mon.write(":")
mon.setCursorPos(9,5)
mon.write("–")

mon.setCursorPos(4,3)
mon.write("0")

mon.setCursorPos(13,3)
mon.write("0")
term.clear()

sleep(2)
shell.run("m")
valithor #2
Posted 31 October 2015 - 07:53 AM
The easiest way to do what you are wanting is through the use of tables. Tables are a incredibly powerful part of Lua, and can be thought of as a variable with a lot of other variables inside of it.


local monitors = {peripheral.find("monitor")} --# finding all of the monitors and putting them in a table
local mon = {} --# defining a table to store the functions we are about to create

for k,v in pairs(monitors[1]) do --# looping through the functions of the first monitor
  mon[k] = function(...) --# creates a new function in the mon table with the name k.  k is the name of one of the functions from the monitor we are looping through
	for i = 1, #monitors do --# looping through all of the monitors in the monitors table
	  monitors[i][k](...) --# executing the original function for each monitor with the original arguments
	end
  end
end

This is a semi complicated method of doing it. The other way to do it is just to call the function for each monitor individually, but that would require much more typing. If you want to use this code just put it at the top of your code as you have it now, and delete your first line.
Edited on 31 October 2015 - 06:54 AM
Kristopher_Jones #3
Posted 31 October 2015 - 02:12 PM
Worked, thank you !