1 posts
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
8543 posts
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
1583 posts
Location
Germany
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
8543 posts
Posted 10 June 2013 - 01:32 PM
That does not do what OP asked.
25 posts
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
758 posts
Location
Budapest, Hungary
Posted 10 June 2013 - 01:54 PM
^ And it even returns if both moves were succussful! +1
8543 posts
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.
758 posts
Location
Budapest, Hungary
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…