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

Override function?

Started by Cing, 03 February 2016 - 11:59 AM
Cing #1
Posted 03 February 2016 - 12:59 PM
It is just a little question.

Is there i function where you can override a excisting function?

Like you have in ios: obj-c and swift; you have

override func prefareforSegue() {
}
KingofGamesYami #2
Posted 03 February 2016 - 01:45 PM
You just define it again. Generally you'll want to retain a variable which points to the original function though.


local _print = print
print = function( ... )
  _print( ... )
end

This doesn't actually change anything in the print function, but you can see how you might go about doing that.

Edit: Ninja'd
Edited on 03 February 2016 - 12:45 PM
Bomb Bloke #3
Posted 03 February 2016 - 02:29 PM
Edit: Ninja'd

Well, you were, but you actually provided valid code, so…
Cing #4
Posted 03 February 2016 - 07:37 PM
Kan jou also override turtle.moveforward()?
Lupus590 #5
Posted 03 February 2016 - 09:08 PM
if you want to, yes.


local oldF = turtle.forward
local oldB = turtle.back

function turtle.forward()
  oldB()
end
function turtle.back()
  oldF()
end
--#now I need a troll face

edit: fixed
Edited on 04 February 2016 - 10:44 AM
Lyqyd #6
Posted 03 February 2016 - 10:08 PM
The correct function name is turtle.back.
Bomb Bloke #7
Posted 03 February 2016 - 10:45 PM
… and you cannot localise table keys.