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

[question] override old turtle.forward?

Started by LuaEclipser, 09 March 2013 - 11:19 AM
LuaEclipser #1
Posted 09 March 2013 - 12:19 PM
this is a small question, but i want to know how to override the original turtle.forward() with a turtle.forward that will act like this


turtle.forward(2)

will go forward 2
AfterLifeLochie #2
Posted 09 March 2013 - 12:23 PM

local turtleForward = turtle.forward

turtle.forward = function(times)
  for i=1, times do
   turtleForward()
  end
end

I need coffee.
OmegaVest #3
Posted 09 March 2013 - 12:24 PM
Easy. Use this:


oldFor = turtle.forward

turtle.forward = function(iSteps)
  local i = 0
  for i = 1, iSteps do
	oldFor()
  end
end




--Your code here


turtle.forward = oldFor


Should work. I think.


However, this only works on a per-program basis. If you want it to work on a computer, leave off the las line, and put it in startup.
LuaEclipser #4
Posted 09 March 2013 - 12:24 PM

local turtleForward = turtle.forward

turtle.forward = function(times)
  for i=1, times do
   turtleForward()
  end
end

I need coffee.

lol

thank you guys
SuicidalSTDz #5
Posted 09 March 2013 - 12:57 PM
I need coffee.
Lol, been working too hard? :P/>
JokerRH #6
Posted 09 March 2013 - 01:19 PM
Or the metatable way (cause they're awesome!)

turtle.forward = setmetatable({forward = turtle.forward}, {__call = function(self, n)
    for i = 1, n do
      self.forward()
    end
  end
})