Turtle loop ending when null value occurs

  • 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

SirDiggalot

New Member
Jul 29, 2019
5
0
0
I made a turtle xp grinder, auto enchants when level 30 is achieved or more if the current item selected cant be enchanted,
it displays a simple number showing the current level, but if there is no item in the turtles current items slot, the enchant returns a null value.
A while loop ends for some reason when there is a null value in the loop, here is my turtle xp grinder script.
PHP:
local PrevLev = 0
xp=peripheral.wrap("right")

while true do 
  turtle.attackUp()
  if xp.getLevels() => 30 then
    redstone.setOutput("bottom", false)
    xp.setAutoCollect(false)
    xp.enchant(30)
  else
    redstone.setOutput("bottom", true)
    xp.setAutoCollect(true)  
  end
  if xp.getLevels() > PrevLev then
    print(xp.getLevels())
    PrevLev = xp.getLevels()
  end
  if PrevLev > xp.getLevels() then
    PrevLev = xp.getLevels()
  end
  sleep(0.1)
end
I am not sure if I did something wrong, or if there is a way to ignore a null value, I would appreciate any help, because I cannot find anything on this, this was my last resort. :/
 

slay_mithos

New Member
Jul 29, 2019
1,288
0
0
I had the same kind of problem for mine, and sadly I didn't send my program to pastebin before the world got ruined, so I'll do it from memory.

Basically, there are three things that can happen when trying to enchant, and that "null" is one you need to protect yourself against.

I ended up with an 'if' checking if there was a single item in the slot before running the command (no stack, and no empty).

While I am at it, if you reverse those lines:
Code:
print(xp.getLevels())
PrevLev = xp.getLevels()

It can become
Code:
PrevLev = xp.getLevels()
print(PrevLev)
Basically, lowering the number of times that you call the same function is a thing that can get you slightly better speed, even if that won't be noticeable here.

EDIT:
Heck, if I'm going at it, might as well give you the 'if' too:
Code:
if (turtle.getItemCount(1)==1) then
 xp.enchant(30)
end
 
  • Like
Reactions: SirDiggalot

Quesenek

New Member
Jul 29, 2019
396
0
0
I wrote a very simple program lastnight when i get to my pc ill post it.
Basically i have it right now set to watch the exp level and enchant when i get level 30, i have it get a book from the 15-16th slot (too lazy to add a barrel) and stick it in the 1st slot and enchant the book then drop it in a diamond chest.
 

SirDiggalot

New Member
Jul 29, 2019
5
0
0
I had the same kind of problem for mine, and sadly I didn't send my program to pastebin before the world got ruined, so I'll do it from memory.

Basically, there are three things that can happen when trying to enchant, and that "null" is one you need to protect yourself against.

I ended up with an 'if' checking if there was a single item in the slot before running the command (no stack, and no empty).

While I am at it, if you reverse those lines:
Code:
print(xp.getLevels())
PrevLev = xp.getLevels()

It can become
Code:
PrevLev = xp.getLevels()
print(PrevLev)
Basically, lowering the number of times that you call the same function is a thing that can get you slightly better speed, even if that won't be noticeable here.

EDIT:
Heck, if I'm going at it, might as well give you the 'if' too:
Code:
if (turtle.getItemCount(1)==1) then
xp.enchant(30)
end
Thank you for this, I hadn't thought of checking the dang slot for an item :p and I didn't realize that calling a function affected performance, even if its small, so I will keep that in mind.
 

Quesenek

New Member
Jul 29, 2019
396
0
0
Sorry I forgot I was supposed to post something lmao.
Code:
while true do
  m = peripheral.wrap("right")
  m.collect()
  term.clear()
  term.setCursorPos(1,1)
  print(m.getLevels())
  XPcount = m.getLevels()
  if XPcount >= 30 and turtle.getItemCount(15) > 0 or turtle.getItemCount(16) > 0 then
    if turtle.getItemCount(16) < 1 then
      count = turtle.getItemCount(15)
      turtle.select(15)
      turtle.transferTo(16, count)
    end
    turtle.select(16)
    turtle.transferTo(1, 1)
    turtle.select(1)
    m.enchant(30)
    turtle.dropDown()
end
sleep(0)
end

I'm using the 15th and 16th slots as a buffer so that I'm not taking Items out of a barrel(imaginary at this point) as often.
The enchant command will throw a null if everything is not explicitly taken care of and told what to do when.
I would also not use the same turtle for enchanting as you are to kill the mobs with my code it may not kill the mobs fast enough.