Jump to content




[question] override old turtle.forward?


  • You cannot reply to this topic
5 replies to this topic

#1 LuaEclipser

  • Banned
  • 220 posts
  • LocationCleveland, Ohio

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

#2 AfterLifeLochie

    Wiki Oracle

  • Moderators
  • 480 posts
  • LocationAfterLifeLochie's "Dungeon", Australia

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.

#3 OmegaVest

  • Members
  • 436 posts

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.

#4 LuaEclipser

  • Banned
  • 220 posts
  • LocationCleveland, Ohio

Posted 09 March 2013 - 12:24 PM

View PostAfterLifeLochie, on 09 March 2013 - 12:23 PM, said:

local turtleForward = turtle.forward

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

I need coffee.

lol

thank you guys

#5 SuicidalSTDz

    Permutator of Strings

  • Members
  • 1,308 posts
  • LocationPennsylvania

Posted 09 March 2013 - 12:57 PM

View PostAfterLifeLochie, on 09 March 2013 - 12:23 PM, said:

I need coffee.
Lol, been working too hard? :P

#6 JokerRH

  • Members
  • 147 posts

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
})






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users