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

term.redirect()

Started by bbqroast, 27 October 2012 - 05:30 AM
bbqroast #1
Posted 27 October 2012 - 07:30 AM
From what I understand, in LUA you can reference a function using a variable. For example:

a = function(n)
return n^2
end
print(a(2))
The above code will create a function, which takes a single variable and return its square.
So, in ComputerCraft we have a handy "term.redirect()" function. Perhaps, instead of passing this a monitor object we could pass it a table of functions? Seeing as you can access LUA table entries by non int based keys (for example a["cake"] = true) then why not use the function names (so a valid term.redirect() call would need a table with entries such as var["write"] = function(n), var["setCursor"] = function(n,n) and so on). This way, we could create our own objects, and redirect the computer's output to them.

For example, a remote control program could use term.redirect() to redirect output to its own functions that use rednet to send the output back to the controller.
faubiguy #2
Posted 27 October 2012 - 07:42 AM
Term.redirect already does this. It works with a monitor object because a monitor object is a table of term functions for the monitor.
Orwell #3
Posted 27 October 2012 - 07:01 PM
Have you actually tried what you're suggesting here? I used exactly that numerous times. One of the applications I used it for was a monitor API that displays on the term as well and worked over rednet. So you can just do what you're describing here.
bbqroast #4
Posted 28 October 2012 - 12:02 AM
Have you actually tried what you're suggesting here? I used exactly that numerous times. One of the applications I used it for was a monitor API that displays on the term as well and worked over rednet. So you can just do what you're describing here.
You can? I tried palying around with the various bits of CraftOS and it seemed much like the monitor object was all Java with a few IDs LUA side. I'll do some more wokr and see if I can get this to work :D/>/>.
Orwell #5
Posted 28 October 2012 - 12:25 AM
Every API in CC is a lua table, the values stored under each key can be a reference to a java function or a lua function. What it references too is irrelevant, what is important is that the references are there and are of type function.
bbqroast #6
Posted 28 October 2012 - 12:35 AM
Every API in CC is a lua table, the values stored under each key can be a reference to a java function or a lua function. What it references too is irrelevant, what is important is that the references are there and are of type function.
Ok. Thanks :D/>/>.
This will aid my turtles greatly in over taking the world.