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

Environments, windows

Started by Sewbacca, 28 September 2016 - 07:47 PM
Sewbacca #1
Posted 28 September 2016 - 09:47 PM
Hey guys,

i'm not sure about this:
If i create a window and start a program with an environment: setmetatable({term = window}, {<index point to _G>}),
would all APIs like textutils use the term of the programs environment or the term of the global table.
I did a test, and textutils respects the window, but in my second test, the program only get term as a window and textutils, without sleep and it also works.
Inherit textutils all fields of the upper program caller or use it the global term or is there a secret mechanism?

Thanks in advance and hopefully it inherits all fields of the upper program
Sewbacca
KingofGamesYami #2
Posted 28 September 2016 - 10:05 PM
textutils respects term.redirects. Don't even bother inserting term into the environment, just use term.redirect. You might replace term.native() though.
Sewbacca #3
Posted 28 September 2016 - 10:18 PM
textutils respects term.redirects. Don't even bother inserting term into the environment, just use term.redirect. You might replace term.native() though.

I used:

loadfile('test', {term = window.create(...), textutils = textutils})
and

term.write(...)
textutils.slowPrint(...)
and it works.
… stands for data, i am to lazy to write it down, not for closures (i'm not sure, if this is the right name)
Edited on 28 September 2016 - 08:32 PM
KingofGamesYami #4
Posted 28 September 2016 - 11:01 PM
Actually (…) is/are vararg(s).
Sewbacca #5
Posted 29 September 2016 - 12:09 PM
varargs, okay.
Why textutils respect the current term in the environment in this case?
Bomb Bloke #6
Posted 29 September 2016 - 12:31 PM
Because textutils doesn't specifically index into _G to get the default term table.

It needs to, though, and a window object won't cut the mustard as a replacement - any script which wants to call term.native(), term.current(), term.redirect(), etc will fail the way you're doing things because windows don't offer those functions.

So: use term.redirect() yourself. If you want to use a specific terminal when calling a function, redirect to that terminal before calling that function. If you want to associate a terminal with a coroutine, redirect before each resume, and pay attention to what it might've switched to when it next yields.
Sewbacca #7
Posted 29 September 2016 - 06:40 PM
Because textutils doesn't specifically index into _G to get the default term table.

It needs to, though, and a window object won't cut the mustard as a replacement - any script which wants to call term.native(), term.current(), term.redirect(), etc will fail the way you're doing things because windows don't offer those functions.

So: use term.redirect() yourself. If you want to use a specific terminal when calling a function, redirect to that terminal before calling that function. If you want to associate a terminal with a coroutine, redirect before each resume, and pay attention to what it might've switched to when it next yields.

I think there are many reason, why window.create don't offers redirect, current and native methods.
Yes, i agree with you using term.redirect. I had many ideas, by setting up an environment with
term = window.create(…), but there are too many ways to hack you out of the given term. The easiest
version i think, is using an API.