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

How to move the term stack backwards?

Started by Axiom, 10 June 2017 - 08:58 PM
Axiom #1
Posted 10 June 2017 - 10:58 PM
Hello :)/>. I was wondering how to move the term stack backwards. I apologize if this has already been asked, I didn't know how to search for this specific question haha. I know that term.redirect will add another term object to the stack, but then how do you remove it? Also I think that you can do something like:

term = term.native()

But I wasn't sure if this is the intended way of resetting term. I think what I'm looking for is something similar to the old term.restore, from what I can tell on the wiki. If you have any guidance it would be much appreciated, thank you in advance! :)/>.
Bomb Bloke #2
Posted 11 June 2017 - 12:37 AM
There is no stack, not since term.restore() was removed from the mod. term.redirect() returns the previous terminal object, allowing this simple construct:

local oldTerm = term.redirect(newTerm)

.
.
.

term.redirect(oldTerm)

Never assume your script "started off" using term.native().
Edited on 10 June 2017 - 10:38 PM
Axiom #3
Posted 11 June 2017 - 03:11 AM
There is no stack, not since term.restore() was removed from the mod. term.redirect() returns the previous terminal object, allowing this simple construct:
local oldTerm = term.redirect(newTerm)
.
.
.
term.redirect(oldTerm)
Never assume your script "started off" using term.native().
Oh, I see now, thank you! :)/>

If you don't mind, I'm still wondering about something. On the wiki it states that term.native, term.current, and term.redirect are only available on the base term object. I'm unsure what this means as it would only make sense for these functions to be available regardless of what term object is being used. Does this mean that any term object passed to term.redirect need not implement these methods as they will be automatically inherited from the base term object? Sorry for the follow up question, thank you for your help so far! :)/>

Edit: And, if these methods aren't automatically inherited, how would term.redirect be implemented if someone were to make a custom term object?
Edited on 11 June 2017 - 01:45 AM
Bomb Bloke #4
Posted 11 June 2017 - 07:11 AM
Consider a "terminal object" to be a table container function pointers for the purpose of dealing with a given display, and "term" to be a table containing function pointers for the purpose of dealing with a given terminal object. It's a wrapper system.

When a computer boots, the "term" API initially available is filled with pointers for native Lua functions (defined by ComputerCraft's Java-side code - there's no Lua source for them) that act directly upon that computer's display. Then this gets loaded, replacing that original API with itself and making the old copy available via term.native(). The redirect / current / native function pointers don't exist within the native terminal object, but rather are additions to the new "term" API.

When you term.redirect() to a different terminal object, the "term" table doesn't actually change, and nor are the functions within it replaced. Rather, the same functions that the "term" table already pointed to will start calling the new terminal object's functions for you. So your object isn't inheriting anything - rather, it's the other way around: "term" inherits what it needs from your object, and it only needs to inherit the bits that "act on a display".
Axiom #5
Posted 11 June 2017 - 02:32 PM
Ahh, thank you for explaining it very clearly! :)/>