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

My Code isnt adding up?

Started by Malminas, 14 September 2016 - 07:15 AM
Malminas #1
Posted 14 September 2016 - 09:15 AM
In the code I'm writing I have:

y = turtle.getItemCount(2)

later down the line I use said variable by typing:

if y ~= 3
then
turtle.select(2)
turtle.turnLeft()
turtle.turnLeft()
turtle.suck(3-y)
turtle.turnLeft()
turtle.turnLeft()
end

I then made a backup code to recheck to ensure I had the correct number of items by typing:

if y ~= 3
then
print "storage empty"
return
else
print " "
end

and even though it gets all its correct items it still says "storage empty", that is until I change the second set up to:

if turtle.getItemCount(2) ~= 3
then
print "storage empty"
return
else
print " "
end

then everything works fine, well at least it did until I just tried it right now. what am I doing wrong?
CCJJSax #2
Posted 14 September 2016 - 05:18 PM
first of all code tags are your friend.


[code ]
-- your code here
-- these should have no spaces to get this lovely white box!


you can put the then on the same line as the if.

Lastly somewhere 'y' must have been changed. I'm not seeing anything that would change y here. post your full code either here or on pastebin and give us the link.
Edited on 14 September 2016 - 03:18 PM
wieselkatze #3
Posted 14 September 2016 - 08:34 PM
Lastly somewhere 'y' must have been changed. I'm not seeing anything that would change y here. post your full code either here or on pastebin and give us the link.

Well, as far as I understand his problem this is exactly what needs to happen.
You're checking slot #2 for the amount of items - if it differs from three, it'll pull the needed amount from another inventory (I guess). (By the way, if your amount is above three you will encounter problems as you want to pull a negative amount of items).

So, after you pulled the items, the variable y still contains the value from before checking. So even if you pulled, let's say two items, y will still contain one.


if y ~= 3 then
  turtle.select(2)
  turtle.turnLeft()
  turtle.turnLeft()
  turtle.suck(3-y)
  turtle.turnLeft()
  turtle.turnLeft()
  y = turtle.getItemCount( 2 )
end

This time y would actually get updated after pulling the necessary items and the next if-statement should run smoothly.
CCJJSax #4
Posted 14 September 2016 - 10:58 PM
Oh good call. I was thinking it was getting changed and didn't even think about it needing to just be updated.
Malminas #5
Posted 15 September 2016 - 11:13 PM
Sorry I haven't posted since yesterday, got busy and didnt get a chance to look at my code, I'll give this a try, if it doesn't work I'll post my whole code.
Malminas #6
Posted 15 September 2016 - 11:29 PM
now I'm getting attempt to call nil

here's my code:

http://pastebin.com/5f8znRFg
KingofGamesYami #7
Posted 16 September 2016 - 12:07 AM
You cannot call Main() before it is declared. Move your Main() call to the bottom of your script.
Malminas #8
Posted 16 September 2016 - 12:29 AM
ohhh lmao, I'm stupid

Thanks everyone, all of your tips worked! :D/>
Malminas #9
Posted 16 September 2016 - 12:39 AM
by the way, how do I bypass "By the way, if your amount is above three you will encounter problems as you want to pull a negative amount of items"
KingofGamesYami #10
Posted 16 September 2016 - 12:58 AM
You were requesting 3-(number of items entered). If you enter 1, you request 2. If you request 3, you request 0. What behavior to you want instead?
Malminas #11
Posted 16 September 2016 - 01:21 AM
well he stated above that if it had more than I wanted it would try to pull negative and cause an error, I just made a new if statment saying:

if y > 3
then
turn
turn
drop
turn
turn
y = getitemcount 2
end

that way if it has more than it should, it just puts it back and tries again.

Seems to work as I intended
Edited on 15 September 2016 - 11:21 PM