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

[Solved] Trying to rewrite term.write

Started by Exerro, 17 February 2013 - 08:18 AM
Exerro #1
Posted 17 February 2013 - 09:18 AM
I have this code

oldTerm.write = _G.term.write
oldTerm.setCursorPos = _G["term"]["setCursorPos"]
oldTerm.getCursorPos = _G["term"]["getCursorPos"]
oldTerm.clear = _G["term"]["clear"]
oldTerm.setBackgroundColour = _G["term"]["setBackgroundColour"]
oldTerm.setTextColour = _G["term"]["setTextColour"]
function term.write( ... )
local arg = { ... }
log.add( "wrote", unpack( arg ) )
oldTerm.write( unpack( arg ) )
end
This should work i think…it doesn't get any errors and if i run it 1 time then it's fine and nothing changes however if i try a second time to run the code this is in either the computer shuts down or term.write doesn't work

The only possible explanation i can think of is that when i first run it, log.add doesn't exist so it doesn't overwrite term.write but the second time it does and term.write is broken

I have no idea how to fix it…please help :P/>

edit: the code works fine every time if i comment out the last 5 lines above

edit2: if i run it uncommented the first time then run it again commented then it does the same error thing

Solution: i changed term.write back to the original at the end of the code and it all seems to work fine now
LBPHacker #2
Posted 17 February 2013 - 10:47 AM
You're writing something similar to TeamViewer, aren't you?
MudkipTheEpic #3
Posted 17 February 2013 - 11:31 AM
It's because if you run it twice, you are making the modified term.write run itself.

Bad example:

oldcake = cake
function cake()
return oldcake()
end
Running it twice would not work, but once would. (If cake was defined)

Good example:

if _G.isLoaded then error("Already loaded")
--code here
_G.isLoaded=true

Will only run once.
LBPHacker #4
Posted 17 February 2013 - 10:33 PM
Bad example:

oldcake = cake
function cake()
return oldcake()
end

Why is this so "bad"?

oldcake points to the original cake function and when you call it inside the new cake, it executes the original cake.
Exerro #5
Posted 18 February 2013 - 12:07 AM
Yeah thanks i did solve it eventually and i think i'm going to publish my program API thingy. This isn't anything like teamviewer however i'm going to start on a screen capture API very soon using stuff like this.
theoriginalbit #6
Posted 18 February 2013 - 12:33 AM
Bad example:
 oldcake = cake function cake() return oldcake() end 
Why is this so "bad"? oldcake points to the original cake function and when you call it inside the new cake, it executes the original cake.

Its a bad example, because if you don't restore the original pointer before you exit your program the next time you run it oldcake will be the current pointer, which is YOUR function, so that means you then have a recursive loop of your function calling itself. it can be made into a good example like this

oldcake = cake
function cake()
  return oldcakse()
end
cake = oldcake
LBPHacker #7
Posted 18 February 2013 - 12:40 AM
-snip-

:D/> Sorry, I'm blind, didn't read that line


Running it twice would

Reminds me of textutils.makePagedScroll
theoriginalbit #8
Posted 18 February 2013 - 12:44 AM
:D/> Sorry, I'm blind, didn't read that line
Hey don't worry! I miss so much stuff too!