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

[Request] I need somebody to make a working monitor program

Started by LDDestroier, 22 January 2015 - 09:35 PM
LDDestroier #1
Posted 22 January 2015 - 10:35 PM
I have found that 'Mirror', a program made by wojbie, does not work in CC 1.64. If there isn't a program that outputs a program to both the screen and a monitor, could someone quickly create one for me? I'm trying to make a tron arena.
cptdeath58 #2
Posted 22 January 2015 - 11:05 PM
If you are not using any peripherals then you can just use "monitor <side> <program> <arguments>"
If you are using a wired one, you can pick up its name when you activate the monitor and activate from there.
If you are using wireless then ,from what I know, you need a computer attached to it.
Now, can you be specific on how you are setting up your monitors such as are you using a central computer attached to a bunch of monitors or do you have computers attached to monitors and how do you want to set it up?
LDDestroier #3
Posted 23 January 2015 - 03:19 AM
I want to connect a monitor to the back of an advanced computer, and I want the program to be outputted not only onto the monitor (as if I used the 'monitor' command), but ALSO on the screen that you see when you right click the computer. I'd like it to have the same syntax as the 'monitor' command. It will not be connected through wires.
cptdeath58 #4
Posted 23 January 2015 - 10:09 PM
Ah ok then.
Well, you can use the Peripheral API and Monitor API to do this and just update both screens.

monitor = peripheral.wrap("Back")
monitor.write("Hello world!")
term.write("Hello world!")
This is just a simple hello program displayed on both the monitor and the computer.
All you got to do is wrap it, then copy the command and display.
So if you wanted it to display this simple character:

monitor = peripheral.wrap("back")
monitor.write("^")
term.write("^")
[Note]
The Monitor API is really just the term API except using monitor.whateverCommand instead of term.whateverCommand
Edited on 23 January 2015 - 09:16 PM
Bomb Bloke #5
Posted 24 January 2015 - 01:19 AM
Come to think about it, I wrote something along those lines not so long ago. If tweaked like so:

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

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

term.clear()
term.setCursorPos(1,1)
print("Hello world!")

… then everything written by the rest of the script should go to the main display in addition to any attached monitors.

Edit: Tweaked in a bugfix, per the below discussion.
Edited on 18 May 2016 - 07:02 AM
LDDestroier #6
Posted 26 January 2015 - 07:52 PM
Nope, it just errors. It waits for a while, then outputs the following error:

nil: vm error:
java.lang.ArrayIndexOutOfBoundsException
Press any key to continue

I do put that at the beginning of a program, right?
Lyqyd #7
Posted 26 January 2015 - 10:09 PM
Come to think about it, I wrote something along those lines not so long ago. If tweaked like so:

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

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

term.clear()
term.setCursorPos(1,1)
print("Hello world!")

… then everything written by the rest of the script should go to the main display in addition to any attached monitors. I think.

You've got a return in there that would prevent it from displaying on more than one device, and using term as the base instead of term.current() or term.native is likely the result of the error displayed above (call term.x, it does your code, then calls term.x, which does your code…).
Bomb Bloke #8
Posted 27 January 2015 - 12:52 AM
That return statement isn't where you think it is, and even if there was any recursion (which I'm doubting), it's performed via a tailcall and so it shouldn't trigger a stack overflow. Hmm. Unless term.redirect() doesn't build a new term table… Come to think of it, that's indeed probably it.

Try:

local multiTerm, oldTerm = {}, term.current()
Edited on 26 January 2015 - 11:56 PM
Lyqyd #9
Posted 27 January 2015 - 12:57 AM
Ah, yeah. That's what I get for reading code on my phone. The term problem is still a real thing, though. Since it's gonna use term.x, which looks up the current redirect target, and that'll be your functions. As in, grabbing term before you declare all the functions won't actually get the current redirect target at the time.
InDieTasten #10
Posted 11 April 2015 - 04:44 PM
Btw, since this thread is getting referenced, the script misses the part, where arg is getting set. Or I'm just overly stupid. someone else should test it
Bomb Bloke #11
Posted 12 April 2015 - 01:34 AM
It gets set automatically. :)/>

local function moo(...)
  print(unpack(arg))
end

moo("Like"," ","this!")