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

Overwrite the given apis

Started by Sewbacca, 19 February 2016 - 09:12 PM
Sewbacca #1
Posted 19 February 2016 - 10:12 PM
How can I overwrite a native funcion without a crash?

For example:

local oldTerm = {}
for k, v in pairs(term) do
  oldTerm[k] = v
end

term["write"] = function(...) --Here crashes
  <CODE>
end

Computercraft in minecraft version 1.7.10
Edited on 19 February 2016 - 09:13 PM
HPWebcamAble #2
Posted 19 February 2016 - 10:38 PM
Whats the error?
valithor #3
Posted 19 February 2016 - 10:42 PM
and what specifically are you overwriting the function with?


This for example would work, so it is most likely what you are overwriting it with.


local oldWrite = term.write

term.write = function(...)
  return oldWrite(...)
end
Sewbacca #4
Posted 19 February 2016 - 11:06 PM
Oh sorry, I did this:


term["write"] = function(...)
  write(...)
end

But I forgot, that write use term.write. :D/>
Bomb Bloke #5
Posted 21 February 2016 - 08:54 AM
At least with the term API, it's not intended that you do this. The idea is that you construct a new table filled with your replacement functions for those in term, then pass that as an argument to term.redirect() (which returns the previous such table used - computers start off using term.native(), but multishell typically changes that to a window).

If you attempt to override the whole term API, then you'll likely get undesirable results if a script makes use of multiple display objects.
Edited on 21 February 2016 - 07:55 AM