10 posts
Posted 21 December 2012 - 12:52 PM
I wrote this function, and
only this function to test out ways I can record the number of items a Turtle has, but I keep getting "attempt to perform arithmatic __add number and nil" on line
14. What you see here is the entire program and the function is called a total of one time. It's my first time working with lua tables, so I may have a syntax error somewhere.
function resetBeforeTable()
iniT = {}
for row = 0, 4, 12 do
for x = 1, 4, 1 do
table.insert(iniT, turtle.getItemCount(x + row))
if x ==4 then
table.insert(iniT, (x + row + row / 4), (turtle.getItemCount(x + row) + turtle.getItemCount(x + row - 1) + turtle.getItemCount(x + row - 2) + turtle.getItemCount(x + row - 3)))
end
end
for col = 21, 1, 24 do
table.insert(intT, col, (iniT[col - 5] + iniT[col - 10] + iniT[col - 15]))
end
end
table.insert(iniT, 25, iniT[5] + iniT[10] + iniT[15] + iniT[20] + iniT[21] + iniT[22] + iniT[23]) -- calculate the total of the table
end
resetBeforeTable()
EDIT: Corrected typo.
122 posts
Location
New Zealand
Posted 21 December 2012 - 01:04 PM
Can't be sure on this, just skimming over the function itself, but in your table.insert before the final end of the function you might need to add tonumber on each iniT[x] Not sure. And there must be more lines above this function as line 16 is the empty line between end and resetBeforeTable()
818 posts
Posted 21 December 2012 - 01:15 PM
give us the whole program.
10 posts
Posted 21 December 2012 - 02:11 PM
So, SO sorry guys, I had to unexpectantly go afk longer than I wanted to. But I'm sorry to say that what you're looking at IS the whole program; simply the creation and calling of a function. This one still has me scratching my head.
The only thing I can say is that I had the line wrong the first time; it's line 14 but it still doesn't make sense to me.
2005 posts
Posted 21 December 2012 - 02:45 PM
What are you trying to do here? "for row = 0, 4, 12 do"
That looks like you're setting a for loop from 0-4 with an increment of 12. That doesn't make a lot of sense to me, the loop only runs once, while row == 0, after that row == 12, which is way more than 4.
Looks like you do something similar with "for col = 21, 1, 24 do", only that loop never runs at all.
So you've got a bunch of nils that you're trying to add together. Obviously Lua doesn't like that.
10 posts
Posted 21 December 2012 - 03:01 PM
Fixed the error you were talking about, ChunLing, but now the exact same error has moved onto line 11. Now what?
7 posts
Posted 21 December 2012 - 03:07 PM
Now you post the new code.
10 posts
Posted 21 December 2012 - 03:12 PM
function resetBeforeTable()
iniT = {}
for row = 0, 12, 4 do
for x = 1, 4, 1 do
table.insert(iniT, turtle.getItemCount(x + row))
if x ==4 then
table.insert(iniT, (x + row + row / 4), (turtle.getItemCount(x + row) + turtle.getItemCount(x + row - 1) + turtle.getItemCount(x + row - 2) + turtle.getItemCount(x + row - 3)))
end
end
for col = 21, 24, 1 do
table.insert(intT, col, iniT[col - 5] + iniT[col - 10] + iniT[col - 15])
end
end
table.insert(iniT, 25, iniT[5] + iniT[10] + iniT[15] + iniT[20] + iniT[21] + iniT[22] + iniT[23]) -- calculate the total of the table
end
resetBeforeTable()
2005 posts
Posted 21 December 2012 - 03:31 PM
The screwy for loops were a problem, but you shouldn't be trying to do maths on table entries in such a failure prone way.
What I don't get is why you don't just use a single loop to get all the item counts and sum them, like:
local iniT= {[25] = 0}
for i=1,16 do
iniT[i] = turtle.getItemCount(i)
iniT[25] = iniT[25]+iniT[i]
end
What the heck is the point of all this multi-dimensional stuff?
I guess, just in case it matters, that you should be aware that your previous code only assigns values in the table up to iniT[15] (and rewrites some values instead of filling things in completely), so when you start at col = 21 and start doing maths on iniT[col-5], which is iniT[16] (and successive indexes in each loop), it is still nil.
Edited on 21 December 2012 - 02:38 PM
10 posts
Posted 21 December 2012 - 03:38 PM
I'm using it to get the X and Y of slots that have gained an item since the last dig. Doing so will allow me to implicitly idenify junk blocks like redstone and gravel without needing a sample (redstone drops 4-5 items, gravel drops flint in a different slot, etc.). I could also pull and point to specific slots and give them roles, such as holding the most numerous or least numerous blocks. This will also let me know when I detected an abandoned mineshaft since almost EVERY item dug from there are unique to them; if I know which blocks I have and where, this will let me deal with blocks I do not forsee.
10 posts
Posted 21 December 2012 - 03:51 PM
Huh. Just noticed you edited right as I posted. May I ask for more detail about how my code sabotaged itself?
2005 posts
Posted 21 December 2012 - 04:26 PM
(x + row + row / 4)
This equals 4, then 9, then 14, then 19. Looking closer at your code I see that you intend to populate the table with values up to iniT[20], but I highly doubt that they are in the order you want.
And evidently you don't have all those values, because otherwise none of them would be nil when you try to access them.
Try putting print(textutils.serialize(iniT)) just before the error line. That table is a mess.
And I can't understand why. None of the inventory functionality you mention cares about the x, y of the inventory, only the slot order (1-16) matters.
10 posts
Posted 21 December 2012 - 04:34 PM
Thanks for the tip on the debug code, but it looks like this table is too much of a mess to salvage as is. I'll rewrite it and use fewer loops to make it much more clean and managable.
Thanks again! Much appreciated.