Jump to content




[Solved] Trying to rewrite term.write


  • You cannot reply to this topic
7 replies to this topic

#1 Exerro

  • Members
  • 801 posts

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

#2 LBPHacker

  • Members
  • 766 posts
  • LocationBudapest, Hungary

Posted 17 February 2013 - 10:47 AM

You're writing something similar to TeamViewer, aren't you?

#3 MudkipTheEpic

  • Members
  • 639 posts
  • LocationWhere you'd least expect it.

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.

#4 LBPHacker

  • Members
  • 766 posts
  • LocationBudapest, Hungary

Posted 17 February 2013 - 10:33 PM

View PostMudkipTheEpic, on 17 February 2013 - 11:31 AM, said:

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.

#5 Exerro

  • Members
  • 801 posts

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.

#6 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 18 February 2013 - 12:33 AM

View PostLBPHacker, on 17 February 2013 - 10:33 PM, said:

View PostMudkipTheEpic, on 17 February 2013 - 11:31 AM, said:

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


#7 LBPHacker

  • Members
  • 766 posts
  • LocationBudapest, Hungary

Posted 18 February 2013 - 12:40 AM

View PostTheOriginalBIT, on 18 February 2013 - 12:33 AM, said:

-snip-

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


View PostMudkipTheEpic, on 17 February 2013 - 11:31 AM, said:

Running it twice would

Reminds me of textutils.makePagedScroll

#8 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 18 February 2013 - 12:44 AM

View PostLBPHacker, on 18 February 2013 - 12:40 AM, said:

:D Sorry, I'm blind, didn't read that line
Hey don't worry! I miss so much stuff too!





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users