Jump to content




Turtle think function?

turtle lua help

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

#1 MrDeeJayy

  • Members
  • 3 posts

Posted 30 March 2015 - 04:07 PM

Hi all.

Been trying to make this turtle that goes around and around this 3x3x1 spot. You see, in the center, is a "Pure Daisy" from Botania. I want the turtle to identify the "Livingwood" and "Livingstone" generated by the daisy, dig it up, replace it with its minecraft counterpart, and move on. 1 hour of coding later, and all the experience I could call upon from my work as a Garry's Mod developer, I made this piece of crap: http://pastebin.com/7xBHcqKE
Spoiler
The painfully obvious problem: A stack overflow in my "Think" function. My question? How do I make (or hook into) some form of Think function that will run each tick (or at least, loop the function)?
Spoiler

For simplicity's sake, here is a screenshot. The turtle (1 turtle when in operation, the extras are just for effect here) follows this redstone path clockwise, at each step, its supposed to turn right and inspect a block). An eventual desire of mine is to have it dump into that chest every once in a while.

#2 MKlegoman357

  • Members
  • 1,170 posts
  • LocationKaunas, Lithuania

Posted 30 March 2015 - 05:40 PM

Don't call your Think function over and over again. Just use a 'while true do loop' for that:

local function Think ()
  while true do
    --# code here
  end
end


#3 SquidDev

    Frickin' laser beams | Resident Necromancer

  • Members
  • 1,427 posts
  • LocationDoes anyone put something serious here?

Posted 30 March 2015 - 07:34 PM

Just a couple of things of things I noticed: There is a inspectDown function. So instead of going round the outside you can just go over the top. This means you don't need the turnRight to look at the block. You also don't need the turtle.suck on line 42 as dig will automatically pick it up the drops, though you will need to suck and drop the items into the chest. Also the place text is optional (line 48)

#4 MrDeeJayy

  • Members
  • 3 posts

Posted 31 March 2015 - 10:02 PM

Thanks for the advice. I'm too used to avoiding while loops like the plague, they have a garbage collection issue in garry's mod that can make variables magically disappear for a full frame/tick.

I'll begin rewriting right away.

EDIT: Ok new problem. After encountering an issue, which is currently in an unapproved post (moderators you may disregard and prune said post).

Posted Image

The fuel is in the correct slot, but its been sitting here for about 15 minutes now? Almost as if it can't refuel. I even went away and made a phone call, nothing changed.

EDIT #2: After a lot of problems, including a stupid stack overflow and some mysterious "138" error, I got it working.

[url=http://pastebin.com/7xBHcqKE]The working code[/url

(LAST MINUTE EDIT) OK, so it was working until it started raining in the overworld, causing a lone enderman to teleport into my base, and stand in the way of the bloody turtle.

Edited by MrDeeJayy, 01 April 2015 - 12:35 AM.


#5 valithor

  • Members
  • 1,053 posts

Posted 01 April 2015 - 02:31 AM

View PostMrDeeJayy, on 31 March 2015 - 10:02 PM, said:

(LAST MINUTE EDIT) OK, so it was working until it started raining in the overworld, causing a lone enderman to teleport into my base, and stand in the way of the bloody turtle


Something like this might become useful if it is a commonly occuring problem. You would just use this function in the place of turtle.forward.
function forward()
  while not turtle.forward() do
	turtle.attack()
	turtle.dig() -- this is optional
  end
end

Assuming pvp is off your turtle wont accidently kill you :P

Edited by valithor, 01 April 2015 - 02:32 AM.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users