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

Can't get my head around a simple problem...

Started by ksbd, 28 May 2013 - 08:37 AM
ksbd #1
Posted 28 May 2013 - 10:37 AM
Hey. I have a very limited knowledge of coding, so be easy on me ;)/>

So, I've been trying to resolve an issue for a while now where I call a function to check how many items are in a turtle. Initially, it worked perfectly. Like an idiot, I changed all the code as I went along, not keeping backups and I've hit multiple issues now.

Each time I ran the program to test, it acted as though it was doing x = x + i each time (Which I tried to correct by adding x = 0 before calling x = x + i, though this only ever gave me 0, or whatever number I specified x was initially)

I don't know, my brain is worn out atm, and would like another pair of eyes to take a quick glance, call me an idiot, and tell me where I made the simple mistake…

The pastebin for the full program is: http://pastebin.com/P6PnuB1Z

The code without any attempted fix is:


function checkPathBlocks()
		for i = 1,5 do
				pathBlockCheckA =pathBlockCheckA + turtle.getItemCount(i)
		end
		for i = 6,10 do
				pathBlockCheckB = pathBlockCheckB + turtle.getItemCount(i)
		end
		for i = 11,15 do
				pathBlockCheckC = pathBlockCheckC + turtle.getItemCount(i)
		end
end
theoriginalbit #2
Posted 28 May 2013 - 10:43 AM
Ok so from what I can see your problem is that, its not actually 0 each time, that is just perfect conditions. The variables would actually contain the value of the last item count in the particular set.
To fix this your checkPathBlocks function should look something like this

function checkPathBlocks()
  pathBlockCheckA, pathBlockCheckB, pathBlockCheckC = 0, 0, 0
  for i = 1, 5 do
    pathBlockCheckA = pathBlockCheckA + turtle.getItemCount(i)
  end

  for i = 6, 10 do
    pathBlockCheckB = pathBlockCheckB + turtle.getItemCount(i)
  end

  for i = 11, 15 do
    pathBlockCheckC = pathBlockCheckC + turtle.getItemCount(i)
  end
end
ksbd #3
Posted 28 May 2013 - 10:52 AM
Ok so from what I can see your problem is that, its not actually 0 each time, that is just perfect conditions. The variables would actually contain the value of the last item count in the particular set.
To fix this your checkPathBlocks function should look something like this

function checkPathBlocks()
  pathBlockCheckA, pathBlockCheckB, pathBlockCheckC = 0, 0, 0
  for i = 1, 5 do
	pathBlockCheckA = pathBlockCheckA + turtle.getItemCount(i)
  end

  for i = 6, 10 do
	pathBlockCheckB = pathBlockCheckB + turtle.getItemCount(i)
  end

  for i = 11, 15 do
	pathBlockCheckC = pathBlockCheckC + turtle.getItemCount(i)
  end
end

I was thinking the exact same thing actually. I just came back here to say I fixed it with this same answer.
The thing is, I'd tried this once, but when I added;

pathBlockCheckA, pathBlockCheckB, pathBlockCheckC = 0, 0, 0
the first time, or changed 0 to any number (lets call this x), the turtle would say that the amount of items was x, reguardless of the actual quantity… A restart of minecraft seemed to fix this…
Is this a bug do you know, or maybe I just made an error?
theoriginalbit #4
Posted 28 May 2013 - 10:57 AM
Is this a bug do you know, or maybe I just made an error?
Chances are that it was just a bug in your code that you have fixed at some point, definitely not a bug with ComputerCraft.
ksbd #5
Posted 28 May 2013 - 11:07 AM
Okay, thanks a lot!