Automatic mob farm with Turtles

  • Please make sure you are posting in the correct place. Server ads go here and modpack bugs go here
  • The FTB Forum is now read-only, and is here as an archive. To participate in our community discussions, please join our Discord! https://ftb.team/discord

Servillo

New Member
Jul 29, 2019
35
0
0
I'm going to preface this topic with letting you know that I am a complete novice when it comes to programming. I've taken one class of C++, which thankfully translates fairly well to LUA. However, that means the extend of my knowledge with programming with turtles basically extends to loops, functions to keep things concise, and using the API's to the best of my abilities. I've learned how to use a couple of the slightly more complicated aspects (such as tArgs, if that counts as complex), but since I'm the only turtle programmer on our server, I don't use them in the name of user-friendliness.

That said, I would like to make a program that will let a turtle automatically kill mobs for me with a grinder, and to collect the EXP that drops. I know all the API functions I need to do this with, it's just a matter of getting it to work right. One thing I do know is that I've read that constant attack loops with a turtle, i.e.

while true do
turtle.attack()
end

Can cause lag or framerate issues overtime. In order to avoid that, I tried something like this

while true do
if redstone.getInput("front") then
turtle.attack()
end
end

I put a pressure plate in front of the turtle, and whenever something stands on it, the turtle will attack it. Very brute force, and not very elegant. Of course, I ran into the only natural thing; if the while loop goes too long without doing anything, the program terminates. I didn't expect anything different, this was just the starting step to make sure my thought process on this was working.

So now my only idea is to make the program temporary via a for loop, i.e.

for i=1 to ## do
if redstone.getInput("front") then
turtle.attack()
end
end

However, I'm pretty sure the same thing will happen. The only other thing I can think of is to put a sleep of a couple minutes or so after the if statement, that way mobs can accumulate, the turtle will attack them until they all die, and life continues as normal. I would use a similar program with my EXP turtles, having them collect experience every few minutes or so automatically.

Again, I'm a complete novice at programming, so anything really complex and out there is just going to be beyond me. I program turtles for fun, and I know my programs are incredibly inefficient, clumsy, and any real programmer would probably cry at my attempts. I love my turtles though, and I try to use them often, and this is just the next thing I want to use them for.
 

Dkittrell

New Member
Jul 29, 2019
378
0
0
First I'm not an expert at programming so I won't comment on any of those questions but relating to the lag and attack turtles in my experience I didn't experience any lag with the loop attack code and i had 8 attack turtles plus on dedicated xp turtle but at the same I'm dedicating 4GB of ram to FTB so that would help any frame rate related issues but everyone on my old sever used my xp farm and didn't complain of lag. Just stating my experience take it how you will :)
 

Silent_007

New Member
Jul 29, 2019
302
0
0
The other way to reduce lag using code along the lines of your first example is just to reduce how often the turtle tries to attack. The sleep command is what you're looking for, though I would use it in a simpler way than you were suggesting. For instance:

Code:
while true do
    turtle.attack()
    turtle.sleep(1)
end
Also, there are TONS of topics on these forums about this exact issue. I would suggest checking some of them out. ;)
 

tompy97

New Member
Jul 29, 2019
85
0
0
abdiel has a very good code, use it, it's almost identical to my set up and its the most lag free way to run a turtle farm
 

Servillo

New Member
Jul 29, 2019
35
0
0
Abdiel, thank you very much for that code. Like I said, I know very little about LUA and the various API's (especially the OS API, which I've pretty much never used), so this helps a lot. I think that may give me a good idea on how to use it elsewhere as well.
 

Bomb Bloke

New Member
Jul 29, 2019
612
0
0
The reason a "while" loop might halt is because it goes too long without yielding (or "waiting"). Beats me as to why this matters but the Lua interpreter doesn't like it.

os.pullEvent yields, as does sleep, as do most turtle actions. It doesn't matter how long your program yields for so long as you're doing it at least once per iteration of your loop.