Jump to content




Noob needs help with replacing turtle functions..


7 replies to this topic

#1 R4tt3xx

  • New Members
  • 1 posts

Posted 10 June 2013 - 10: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

#2 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 10 June 2013 - 11:35 AM

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


#3 H4X0RZ

  • Members
  • 1,315 posts
  • LocationGermany

Posted 10 June 2013 - 12:07 PM

My idea:
local oldUp = turtle.up
local oldForward = turtle.forward
function turtle.forward()
oldUp()
end
function turtle.up()
oldForward()
end


#4 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 10 June 2013 - 12:32 PM

That does not do what OP asked.

#5 MaHuJa

  • Members
  • 26 posts

Posted 10 June 2013 - 12: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


#6 LBPHacker

  • Members
  • 766 posts
  • LocationBudapest, Hungary

Posted 10 June 2013 - 12:54 PM

^ And it even returns if both moves were succussful! +1

#7 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 10 June 2013 - 01:03 PM

View PostLBPHacker, on 10 June 2013 - 12:54 PM, said:

^ 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.

#8 LBPHacker

  • Members
  • 766 posts
  • LocationBudapest, Hungary

Posted 10 June 2013 - 01:08 PM

View PostLyqyd, on 10 June 2013 - 01:03 PM, said:

View PostLBPHacker, on 10 June 2013 - 12:54 PM, said:

^ 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...





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users