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

Noob needs help with replacing turtle functions..

Started by R4tt3xx, 10 June 2013 - 09:52 AM
R4tt3xx #1
Posted 10 June 2013 - 11:52 AM
Is there a way to create a function that over-writes an existing function while still being able to call the original function ?

Let me give an example. I wish to have the turtle.forward() command move my turtle up ie turtle.up as well as forward by issuing the turtle.forward command

Thanks
Lyqyd #2
Posted 10 June 2013 - 12:35 PM
Split into new topic.

Yes, you can. Why not just make a new function that does both and use it instead, though? If you really need exactly this, which I suspect isn't the best solution, you'd just:


local oldForward = turtle.forward

turtle.forward = function()
  if oldForward() and turtle.up() then
    return true
  else
    return false
  end
end
H4X0RZ #3
Posted 10 June 2013 - 01:07 PM
My idea:

local oldUp = turtle.up
local oldForward = turtle.forward
function turtle.forward()
oldUp()
end
function turtle.up()
oldForward()
end
Lyqyd #4
Posted 10 June 2013 - 01:32 PM
That does not do what OP asked.
MaHuJa #5
Posted 10 June 2013 - 01:50 PM
Here's a oneliner to replace Feack's code.
turtle.up, turtle.forward = turtle.forward, turtle.up

To answer the OP fully,

turtle.rawforward = turtle.forward
turtle.forward = function() 
  return turtle.rawforward() and turtle.up()
end
LBPHacker #6
Posted 10 June 2013 - 01:54 PM
^ And it even returns if both moves were succussful! +1
Lyqyd #7
Posted 10 June 2013 - 02:03 PM
^ And it even returns if both moves were succussful! +1

It's essentially identical to the expanded/simplified code I provided in my original response to OP, so yes, it too provides a success code as a return value.
LBPHacker #8
Posted 10 June 2013 - 02:08 PM
^ And it even returns if both moves were succussful! +1

It's essentially identical to the expanded/simplified code I provided in my original response to OP, so yes, it too provides a success code as a return value.
Okay, didn't see yours. You know, I got used to you often saying only "Split into new topic" and scrolled further down…