This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
marth010's profile picture

Drop All But One Slot - Loop Not Working

Started by marth010, 08 January 2013 - 05:10 PM
marth010 #1
Posted 08 January 2013 - 06:10 PM
Hello all,

Im trying to code a little something that has a turtle drop everything in all slots except slot 1. To achieve this, I'm trying a for loop as so..

for i=2,9 do
turtle.select(i)
turtle.drop()
end

However, I have noticed that this procedure can be pretty random. Sometimes it will keep slot 1. Other times, it throws it away like everything else. Anyone have any suggestions for fixing this problem? Thank you!
theoriginalbit #2
Posted 08 January 2013 - 06:13 PM
Firstly just an FYI. In this forum there are code tags. [code] and [/code] … use them around code :)/>

Secondly. That looks fine to me. Are you sure it isn't something somewhere else in the code? and is this copied from the code or retyped? if retyped, make sure it looks like this one.
ChunLing #3
Posted 08 January 2013 - 08:44 PM
The code looks fine, but it also looks like a pre-1.4 code. I had some experience with those, but never used drop since it didn't go into an inventory back then.
RunasSudo-AWOLindefinitely #4
Posted 08 January 2013 - 09:24 PM
Where is this code located? It is just that, or is it part of a bigger program? Perhaps you could try changing the variable from i to something else.
ChunLing #5
Posted 08 January 2013 - 11:39 PM
Within the scope of the for loop, that local i should obscure any other variable using the same identifier in a larger scope.

Really, the most likely candidate I can think of for the described behavior is that the drop and select commands are accidentally transposed in the actual code, like:
for i=2,9 do
  turtle.drop()
  turtle.select(i)
end
Okay, I can think of something likelier I bet…how about that the 2 is being passed as a variable, and it isn't really always two?

Now I'm just making while guesses, though.
theoriginalbit #6
Posted 08 January 2013 - 11:43 PM
Okay, I can think of something likelier I bet…how about that the 2 is being passed as a variable, and it isn't really always two?

Given scope its the only issue I could think of, hence the questions. But I don't see many people trying to do this
2 = 16
I think common sense should kick in there with most people…
ChunLing #7
Posted 09 January 2013 - 12:20 AM
No, that wouldn't work anyway, 2 can't be an identifier. The code, as posted, should work. So I'm speculating on how the code that isn't working might differ from what is posted. Which is kinda useless, but my theory was that for i=2,9 do was actually for i=variableThatIsSupposedToBe2ButIsNotAlways2,9 do (okay, I'm not really speculating that people name their variables like that).

If we go on the theory that the code as posted is acting as described because of something somewhere else, then the likely culprit would be a redefinition of the turtle API functions. Not because that's even remotely likely, but because there's nothing else I can see there that could have been redefined elsewhere in a way that would affect the loop as described.
marth010 #8
Posted 09 January 2013 - 04:08 PM
Thank you all for your replies. For some additional information, I am running on 1.33 (tekkit). Also, The code is called in a function, which is called from another function, which is only called from one last function (gets a little tedious). Here are the functions I use (It follows straight down):



function digLine(blocks)
  for i = 1,blocks-1 do
    if turtle.getItemCount(9) ~= 0 then
	  dropoff()
    end
    while forward() == false do
	  turtle.dig()
    end
  end
end

function dropoff()
  recordCoord = {current[1],current[2],current[3]}
  recordCompass = compass
  goToDest(origin)
  dropAndReset()
end

function dropAndReset()
  goToDest(dropZone)
  while compass ~= originalCompass do
    turnLeft()
  end
  for i = 2,9 do
    turtle.select(i)
    turtle.drop()
  end
  turtle.select(1)
  goToDest(origin)
end


It's weird in that sometimes it will drop slot 1 and sometimes not. Even wierder, it will follow the correct sequence (dropping 2 first, then 3, 4..) until the end when it randomly drops slot 1. I am playing on a server, so could it be the lag perhaps? Again, thank you guys very much and I hope I gave enough information
RunasSudo-AWOLindefinitely #9
Posted 09 January 2013 - 04:42 PM
What is in your goToDest function? Is there anything there that could be calling turtle.drop()?
Could you try putting a sleep(3) statement

  for i = 2,9 do
	turtle.select(i)
	turtle.drop()
  end
  sleep(3)				 --Here
  turtle.select(1)
  goToDest(origin)
Other than that, I have no idea…
marth010 #10
Posted 09 January 2013 - 05:27 PM
The goToDest() function I have is a recursive beast that is able to (somewhat) intelligently get a trutle to a certain cooridinate. I put a flag at the beginning of it and.. When it does drop slot 1, it happens in the middle of the goToDest() funciton. I'll try and check it out and keep you guys updated
Heracles421 #11
Posted 10 January 2013 - 03:50 AM
The goToDest() function I have is a recursive beast that is able to (somewhat) intelligently get a trutle to a certain cooridinate. I put a flag at the beginning of it and.. When it does drop slot 1, it happens in the middle of the goToDest() funciton. I'll try and check it out and keep you guys updated

Well, maybe it's your goToDest() that has something weird, because if the turtle drops the slot 1 when traveling, it probably has to do with goToDest().
Just to make sure, try changing the loop a bit like this:

slot = 2
for i = 1,8 do
  turtle.select(slot)
  turtle.drop()
  slot = slot + 1
end

Shouldn't do much difference between using i, but just in case
remiX #12
Posted 10 January 2013 - 04:04 AM
The goToDest() function I have is a recursive beast that is able to (somewhat) intelligently get a trutle to a certain cooridinate. I put a flag at the beginning of it and.. When it does drop slot 1, it happens in the middle of the goToDest() funciton. I'll try and check it out and keep you guys updated

Well, maybe it's your goToDest() that has something weird, because if the turtle drops the slot 1 when traveling, it probably has to do with goToDest().
Just to make sure, try changing the loop a bit like this:

slot = 2
for i = 1,8 do
  turtle.select(slot)
  turtle.drop()
  select = select + 1
end

Shouldn't do much difference between using i, but just in case

Why are you incrementing select? :D/>
Heracles421 #13
Posted 10 January 2013 - 05:08 AM
The goToDest() function I have is a recursive beast that is able to (somewhat) intelligently get a trutle to a certain cooridinate. I put a flag at the beginning of it and.. When it does drop slot 1, it happens in the middle of the goToDest() funciton. I'll try and check it out and keep you guys updated

Well, maybe it's your goToDest() that has something weird, because if the turtle drops the slot 1 when traveling, it probably has to do with goToDest().
Just to make sure, try changing the loop a bit like this:

slot = 2
for i = 1,8 do
  turtle.select(slot)
  turtle.drop()
  select = select + 1
end

Shouldn't do much difference between using i, but just in case

Why are you incrementing select? :D/>/>
Herp derp, I derped so badly there :P/>
Fixed
ChunLing #14
Posted 10 January 2013 - 08:57 AM
No, we've established that the for loop isn't the problem, the extra drop happens during goToDest() after the for loop is done.
marth010 #15
Posted 11 January 2013 - 02:51 PM
Ok so I've checked out my entire program and I can confirm that I do not have a turtle.drop() out of place or anything like that. I scarcely have any drops and they are all where they should be. Thing is, I put a flag at the beginning of the recursive move function that would have the turtle say if he had something in his first slot or not..



if turtle.getItemCount(1) >= 0 then
  print("I have them")
else
  print("I dropped them")
end


However, even when the turtle would randomly drop the first slot, it would claim to still have them.. Idk. This is weird. I have the entire program if anyone would want to see it and possibly help me with what's happening. Thanks again you guys for the responces and feedback
RunasSudo-AWOLindefinitely #16
Posted 11 January 2013 - 02:57 PM
At what point in the program does the turtle drop the item? During the for loop, during a different function, etc?
Also, upload your entire program to pastebin or something so we can take a look at it, run it for ourselves, etc.
marth010 #17
Posted 11 January 2013 - 03:10 PM
The random drop happens in dropAndReset(). I commented out the main program except for that function, and here it is: http://pastebin.com/LJdSMtm9
(btw I've never used pastebin before and I'm not even sure if that's the right website lulz)
Heracles421 #18
Posted 11 January 2013 - 04:08 PM
Lines 95 - 97, why is it placing then dropping something from slot 1?
marth010 #19
Posted 11 January 2013 - 04:32 PM
It places one of the cobblestone from the slot, drops the rest, and then picks it back up. Essentially dropping all unwanted cobblestone by sacrificing a slot. It's where this problem starts actually
theoriginalbit #20
Posted 11 January 2013 - 04:35 PM
It places one of the cobblestone from the slot, drops the rest, and then picks it back up. Essentially dropping all unwanted cobblestone by sacrificing a slot. It's where this problem starts actually

you could always do this

turtle.select( 1 )
turtle.drop( turtle.getItemCount( 1 ) - 1 )
This would drop all except for 1
marth010 #21
Posted 11 January 2013 - 04:42 PM
Oh cool I didn't know that. The one I have was the only thing I could think of at the time
theoriginalbit #22
Posted 11 January 2013 - 04:46 PM
haha thats ok, everyone has to learn the apis at some point :)/>
Heracles421 #23
Posted 11 January 2013 - 04:47 PM
That's why we're here, to help you out. And as you can see some users really help out a lot, and they also know a lot (TheOriginalBIT)
theoriginalbit #24
Posted 11 January 2013 - 04:58 PM
Just a bit of a teaching side note… read if you wish :)/>
SpoilerThe lines all over the place that you have as following:


if turtle.down() == true then
while forward() == false do

for ease of reading can become this:


if turtle.down() then
while not forward() do

as these functions return a boolean variable, the if statement is able to 'evaluate' its conditional from that returned value… not reverses what the boolean is, so if its true then it becomes false, if its false it becomes true… not can also be used to toggle a boolean variable

var = not var
instead of dooing

if var then
  var = false
else
var = true
end

other notable things is say we want a turtle (non-mining) to move down if there is something in-front of it and nothing below it or else just fail and sit there, we could do this

if not turtle.forward() and not turtle.detectDown() then
  turtle.down()
else
  error()
end
but instead we can do this

if not ( turtle.forward() and turtle.detectDown() ) then
  turtle.down()
else
  error()
end
its not a big change, but its less not's all over the place confusing you when its read… the way is it works is it evaluates the brackets first by using the following binary logic

true and true = true
true and false = false
false and true = false
false and false = false
with one change we can have this move down if there is something in-front of it, or nothing below it… we simply change the and to an or… that uses the following binary logic

true or true = true
true or false = true
false or true = true
false or false = false

Hope this helps you with making better logics in your programs :)/>
Edited on 11 January 2013 - 03:59 PM
theoriginalbit #25
Posted 11 January 2013 - 05:01 PM
That's why we're here, to help you out. And as you can see some users really help out a lot, and they also know a lot (TheOriginalBIT)
Only ~50 posts today :/ average is about 80… my peak is ~180… And thanx for you kind words :)/> I don't know everything though ;)/> thats my genius friend… :P/>
marth010 #26
Posted 11 January 2013 - 05:05 PM
Yeah i can definitely make those changes. I thought i tried that at the beginning and got an error, thought lua was a little weird that way. I guess not! After this problem is fixed I don't think anything else will need to be added
Heracles421 #27
Posted 11 January 2013 - 05:06 PM
That's why we're here, to help you out. And as you can see some users really help out a lot, and they also know a lot (TheOriginalBIT)
Only ~50 posts today :/ average is about 80… my peak is ~180… And thanx for you kind words :)/>/> I don't know everything though ;)/>/> thats my genius friend… :P/>/>

Only 50? ONLY? I usually have like 15 a day, even less :P/>
theoriginalbit #28
Posted 11 January 2013 - 05:08 PM
Yeah i can definitely make those changes. I thought i tried that at the beginning and got an error, thought lua was a little weird that way. I guess not! After this problem is fixed I don't think anything else will need to be added

Sometimes errors can arise when doing it with variables since Lua allows any data type in a variable. however if you always make sure you store a boolean in the variable you won't get any errors… unless your logic is wrong…
marth010 #29
Posted 11 January 2013 - 05:19 PM
It places one of the cobblestone from the slot, drops the rest, and then picks it back up. Essentially dropping all unwanted cobblestone by sacrificing a slot. It's where this problem starts actually

you could always do this

turtle.select( 1 )
turtle.drop( turtle.getItemCount( 1 ) - 1 )
This would drop all except for 1

Oh and I realize I had tried something like this before. For some reason, passing a parameter to turtle.drop() is bugged and ends up not dropping anything at all. At least in 1.33. Is it fixed in the later versions?
Heracles421 #30
Posted 11 January 2013 - 05:22 PM
Spoiler
It places one of the cobblestone from the slot, drops the rest, and then picks it back up. Essentially dropping all unwanted cobblestone by sacrificing a slot. It's where this problem starts actually

you could always do this

turtle.select( 1 )
turtle.drop( turtle.getItemCount( 1 ) - 1 )
This would drop all except for 1

Oh and I realize I had tried something like this before. For some reason, passing a parameter to turtle.drop() is bugged and ends up not dropping anything at all. At least in 1.33. Is it fixed in the later versions?

Yup, I've never had that error on the most rescent versions. You should change your current pack, FTB (Feed The Beast) is way better thy tekkit and is up to date. I assume you're using tekkit
theoriginalbit #31
Posted 11 January 2013 - 05:24 PM
It places one of the cobblestone from the slot, drops the rest, and then picks it back up. Essentially dropping all unwanted cobblestone by sacrificing a slot. It's where this problem starts actually

you could always do this

turtle.select( 1 )
turtle.drop( turtle.getItemCount( 1 ) - 1 )
This would drop all except for 1

Oh and I realize I had tried something like this before. For some reason, passing a parameter to turtle.drop() is bugged and ends up not dropping anything at all. At least in 1.33. Is it fixed in the later versions?

That definitely sounds like a Bukkit issue to me!