Question about Turtles

  • Please make sure you are posting in the correct place. Server ads go here and modpack bugs go here

tompy97

New Member
Jul 29, 2019
85
0
0
There is no code that exists that will get you turtles to not stop when you log off then back on. This is an absolute rule, no way of getting around it.

What you CAN do however, is store variables in you turtles so they know what position they were in when they shutdown, and then they can pick up where they left off.

This requires a fair bit of clever programming however, and is not something that you can just 'add' in to your programs to make them 'restart proof'. This is especially so if your program is complex to begin with.

Just to give you an idea of what is involved, I've just written a persistent movement API for my turtles which can be found here: http://pastebin.com/RbBiLkh3. When I use the API it saves the turtles locations to a file, which can then be read when the turtle boots up again when the server restarts. This API is the easiest and most basic part of writing a persistent program (at least it is for me anyway). The harder part is making my quarry program smart enough to be able to understand this information...
 

Furious1964

Well-Known Member
Nov 10, 2012
1,436
70
63
There is no code that exists that will get you turtles to not stop when you log off then back on. This is an absolute rule, no way of getting around it.

What you CAN do however, is store variables in you turtles so they know what position they were in when they shutdown, and then they can pick up where they left off.

This requires a fair bit of clever programming however, and is not something that you can just 'add' in to your programs to make them 'restart proof'. This is especially so if your program is complex to begin with.

Just to give you an idea of what is involved, I've just written a persistent movement API for my turtles which can be found here: http://pastebin.com/RbBiLkh3. When I use the API it saves the turtles locations to a file, which can then be read when the turtle boots up again when the server restarts. This API is the easiest and most basic part of writing a persistent program (at least it is for me anyway). The harder part is making my quarry program smart enough to be able to understand this information...

And you would put this in the startup program or append its file to that?
 

Furious1964

Well-Known Member
Nov 10, 2012
1,436
70
63
Have a slight problem, making additions to the code and I want the turtle to move to the right to refuel. But every time I run the program, it gives me either an "=", "Then" or "nil" error.

Code:
print "Checking fuel..."
if turtle.getFuelLevel() < 40 then
  turtle.turnright()
  turtle.forward()
  sleep(delay)
  print "Refueling..."
  turtle.refuel()
    if turtle.getFuelLevel() >= 70 then
      turtle.back()
      turtle.left()
      sleep(delay)
    end
else
  print "No Fuel Necessary"
end
 
local num = 33
local delay = 2
num = num -1
redstone.setOutput("left", true)
for i = 1, num do
  sleep(delay)
  turtle.forward()
end
sleep(delay)
redstone.setOutput("left", false)
for i = 1, num do
  turtle.back()
end

To me, it looks OK, but I don't know much about programming in LUA.

I changed some lines, but now I get an "bios:133: Expected Number" error. What is it looking for?
 

casilleroatr

New Member
Jul 29, 2019
1,360
0
0
The syntax for printing is print(string), so you need to put brackets around your strings when printing. (You also need to do this if you are printing a number of a boolean). On line 3 you need to capitalize Right so it is turtle.turnRight(). similarly on line 10 where you have turtle.left() it should be turtle.turnLeft(). Also, the sleep commands in the first chunk of codes won't work unless delay is defined first. Fix that simply by putting the variables right at the top of the program. It helps to have a different text editor to do this (if you haven't been using one already), note pad will work, notepad++ is better.

How are you charging the turtle by the way?
 

Furious1964

Well-Known Member
Nov 10, 2012
1,436
70
63
The syntax for printing is print(string), so you need to put brackets around your strings when printing. (You also need to do this if you are printing a number of a boolean). On line 3 you need to capitalize Right so it is turtle.turnRight(). similarly on line 10 where you have turtle.left() it should be turtle.turnLeft(). Also, the sleep commands in the first chunk of codes won't work unless delay is defined first. Fix that simply by putting the variables right at the top of the program. It helps to have a different text editor to do this (if you haven't been using one already), note pad will work, notepad++ is better.

How are you charging the turtle by the way?

OK, I fixed that, but how can I list the amount of fuel it currently has? It's charging via a Charge Station. I would like it to give a count indication of the recharge rate until it reaches the desired limit (which I put as 70).

Changed the code a little, but it's not working:
Code:
print "Checking fuel..."
if turtle.getFuelLevel() < 40 then
  turtle.turnRight()
  turtle.forward()
  sleep(1)
  print "Refueling..."
  turtle.refuel()
  FuelCurrent = turtle.getFuelLevel()
    print "Amount of Fuel: " FuelCurrent
    if turtle.getFuelLevel() = 70 then
      turtle.back()
      turtle.turnLeft()
      sleep(1)
    end
else
  print "No Fuel Necessary"
end
 
local num = 33
local delay = 2
num = num -1
redstone.setOutput("left", true)
for i = 1, num do
  sleep(delay)
  turtle.forward()
end
sleep(delay)
redstone.setOutput("left", false)
for i = 1, num do
  turtle.back()
end
 

casilleroatr

New Member
Jul 29, 2019
1,360
0
0
It would require a slight rewrite of your code atarting at the turtle.refuel() up until the else statement using a repeat loop. Also, you don't need turtle.refuel() for the charging station I think.

They are easy, you just type repeat and then some code followed by until and a condition and it will repeat the code until a condition is met. In this case you could do something like this.

It will need some use of the term api otherwise your display will get mucked up

Code:
...
print("refueling...")
local x,y = term.getCursorPos()
repeat
term.clearLine()
term.setCursorPos(1,y)
write("Fuel Level: "..turtle.getFuelLevel())
sleep(0.05) -- The length of a tick in seconds. The charge station goes up one EU per tick so you should see this go up nicely
until turtle.getFuelLevel() >= 70
turtle.back()
turtle.turnLeft()
sleep(delay)
else
print("No fuel necessary")
end
...
 

Furious1964

Well-Known Member
Nov 10, 2012
1,436
70
63
It would require a slight rewrite of your code atarting at the turtle.refuel() up until the else statement using a repeat loop. Also, you don't need turtle.refuel() for the charging station I think.

They are easy, you just type repeat and then some code followed by until and a condition and it will repeat the code until a condition is met. In this case you could do something like this.

It will need some use of the term api otherwise your display will get mucked up

Code:
...
print("refueling...")
local x,y = term.getCursorPos()
repeat
term.clearLine()
term.setCursorPos(1,y)
write("Fuel Level: "..turtle.getFuelLevel())
sleep(0.05) -- The length of a tick in seconds. The charge station goes up one EU per tick so you should see this go up nicely
until turtle.getFuelLevel() >= 70
turtle.back()
turtle.turnLeft()
sleep(delay)
else
print("No fuel necessary")
end
...

OK, did that and it doesn't seem to be charging.
 

casilleroatr

New Member
Jul 29, 2019
1,360
0
0
Try it with the turtle.refuel().

Also make absolutely sure that EU is going into the charger and that the turtle is adjacent to the dotted side. Also, before testing it might be a good idea to change the sleep time in the repeat loop to something a bit longer, or add an iterator to make sure it doesn't repeat forever. Like this

local it = 1
repeat
same as what i put earlier
i = i+1
until turtle.getFuelLevel() >=70 or i == 70
 

Furious1964

Well-Known Member
Nov 10, 2012
1,436
70
63
I don't know why it's doing what it's doing, but it's still not listing the current amount. Will look into it tomorrow.
 

Henry Link

Popular Member
Dec 23, 2012
2,601
553
128
USA - East Coast
You do not need the turtle.refuel command if you are using the charge station. I would suggest double checking the wiring to the charge station. I know on my Direwolf 20 1.4.7 pack I had to break the charging station and replace it to make it work. Also there is a little round dot on the charging station. Make sure it is facing the turtle.
 

Furious1964

Well-Known Member
Nov 10, 2012
1,436
70
63
You do not need the turtle.refuel command if you are using the charge station. I would suggest double checking the wiring to the charge station. I know on my Direwolf 20 1.4.7 pack I had to break the charging station and replace it to make it work. Also there is a little round dot on the charging station. Make sure it is facing the turtle.

Here's what the code looks like now and I'm getting "Unexpected Symbol" errors for the "until turtle.getFuelLevel() = 70 then" line.

Code:
local num = 30
local delay = 2
num = num -1
redstone.setOutput("left", true)
for i = 1, num do
  sleep(delay)
  turtle.forward()
end
sleep(delay)
redstone.setOutput("left", false)
for i = 1, num do
  turtle.back()
end
 
print "Checking fuel..."
local i = 1
repeat
if turtle.getFuelLevel() < 40 then
  turtle.turnRight()
  turtle.forward()
  turtle.turnLeft()
  print("refueling...")
  local x,y = term.getCursorPos()
  repeat
  term.clearLine()
  term.setCursorPos(1,y)
  write("Fuel Level: "..turtle.getFuelLevel())
  sleep(0.05)
  i = i + 1
  until turtle.getFuelLevel() = 70 then
  turtle.turnRight()
  turtle.back()
  turtle.turnLeft()
  sleep(delay)
else
print("No fuel necessary")
end
 

casilleroatr

New Member
Jul 29, 2019
1,360
0
0
Like Riuga said, take out the first repeat

EDIT: You will need the >= condition or the > condition for the until
 

Furious1964

Well-Known Member
Nov 10, 2012
1,436
70
63
Also, what happens if the fuel level is greater than 70?

Got it to work a little, but fuel goes over 70...way over, charging doesn't stop and it doesn't go back to its starting position. Can't use the "repeat" - "until" statements as they don't work for me, keeps throwing up an "unexpected symbol" error.

Code:
local num = 30
local delay = 2
num = num -1
redstone.setOutput("left", true)
for i = 1, num do
  sleep(delay)
  turtle.forward()
end
sleep(delay)
redstone.setOutput("left", false)
for i = 1, num do
  turtle.back()
end
 
print "Checking fuel..."
local i = 1
if turtle.getFuelLevel() < 40 then
  turtle.turnRight()
  turtle.forward()
  turtle.turnLeft()
  print("refueling...")
  local x,y = term.getCursorPos()
    term.clearLine()
    term.setCursorPos(1,y)
    write("Fuel Level: "..turtle.getFuelLevel())
    sleep(0.05)
    i = i + 1
    if turtle.getFuelLevel() == 70 then
      turtle.turnRight()
      turtle.back()
      turtle.turnLeft()
      sleep(delay)
    end
else
print("No fuel necessary")
end
 

casilleroatr

New Member
Jul 29, 2019
1,360
0
0
You won't be able to get the updating fuel counter without using a repeat loop. Make sure that the condition (currently at the second if) is set to > or >=