Thanks for the reply.
So it's either LuaJ or Lua 5.1, cause my code indeed works with plain Lua 5.2.
Workaround: instead directly editing the table that's being iterated through, either edit the original (global) table or create a temporary new one.
- ComputerCraft | Programmable Computers for Minecraft
- → VADemon's Content
VADemon's Content
There have been 2 items by VADemon (Search limited from 30-March 23)
#196667 [SOLVED] [Lua] nested pairs() error
Posted by VADemon on 21 October 2014 - 07:34 PM in Ask a Pro
Hi,
I need help with a strange error within nested pairs()
Script description: A customizable crafting (stamper) turtle. To define crafting recipes I used the following construction:
This table is then used and modified by the "initCraftingGrid" function which fails on the second for-loop with in pairs(gridValue) do with the following error:
line 30: invalid key to 'next'
Any idea what's causing this? Honestly, looks to me like an internal error
Full code:
Version: ComputerCraft1.64
UPD: This nested pairs() construction doesn't cause any errors on Lua: Demo
I need help with a strange error within nested pairs()
Script description: A customizable crafting (stamper) turtle. To define crafting recipes I used the following construction:
craftingGrid = {-- main table containing all possible recipes compressedSawdust = { -- how to craft this item true, true, true, false, -- for slots 1-16, true when there must be an item in the slot, false when not true, false, true, false, true, true, true, false, false, false, false, false } }
This table is then used and modified by the "initCraftingGrid" function which fails on the second for-loop with in pairs(gridValue) do with the following error:
line 30: invalid key to 'next'
Any idea what's causing this? Honestly, looks to me like an internal error
Full code:
Spoiler
Temporary link for testing: http://pastebin.com/72Hwre1W-- how many items to craft at once? targetItemCount = 16 -- sleep time between inv checks sleepTimeInventory = 2 -- which item from /craftingGrid/ to craft? targetItemType = "compressedSawdust" craftingGrid = { compressedSawdust = { true, true, true, false, true, false, true, false, true, true, true, false, false, false, false, false } } craftingGrid_meta = { -- itemType = { -- resultSlot = number, -- } } function initCraftingGrid() for gridKey, gridValue in pairs(craftingGrid) do print(type(gridValue)) -- returns a table as it should local requiredCraftingSlots = 0 -- that's the first slot where the items would go to local craftResultSlot -- line 30 throwing that error; it should iterate through table acquired by the previous pairs() function, e.g. craftingGrid[compressedSawdust] for slotKey, slotValue in pairs(gridValue) do -- line 30 if slotValue then requiredCraftingSlots = requiredCraftingSlots + 1 else if not craftResultSlot then craftResultSlot = slotKey end gridValue[ slotKey ] = nil -- erase value from table end end if requiredCraftingSlots <= 9 then gridValue.craftingSlots = requiredCraftingSlots else print("Error: craftingGrid for [" .. gridKey .. "] requires more than 9 crafting slots!") print("Double-check this") error() end craftingGrid_meta[ gridKey ].resultSlot = craftResultSlot craftingGrid[ gridKey ] = gridValue -- apply changes to the global table from above end end function craftingGridSort( targetItem ) local readyForCrafting = false for i, v in pairs( craftingGrid[ targetItem ] ) do -- item in slot /i/? if v then local currentSlotItems = turtle.getItemCount(i) -- return next slot or, if at the end, the first slot: local nextSlot = ( next(craftingGrid[ targetItem ], i) or next(craftingGrid[ targetItem ]) ) if currentSlotItems > targetItemCount then turtle.select(i) turtle.transferTo(nextSlot, (currentSlotItems - targetItemCount)) end if nextSlot == 1 then readyForCrafting = true end end end return readyForCrafting end function inputCraftingItems() -- fetch from craftingGrid table return turtle.suckUp() end function outputCraftedItems( targetItemType ) -- same as above turtle.select( craftingGrid_meta[ targetItemType ].resultSlot ) return turtle.dropDown() end -- runtime initCraftingGrid() while true do print("Iteration") turtle.select(1) while inputCraftingItems( targetItemType ) == false do -- no new input items sleep( sleepTimeInventory ) end craftingGridSort( targetItemType ) turtle.craft() while outputCraftedItems( targetItemType ) == false do print("Can't output crafted Items!") sleep(5) end sleep( sleepTimeInventory ) end
Version: ComputerCraft1.64
UPD: This nested pairs() construction doesn't cause any errors on Lua: Demo
Spoiler
craftingGrid = { compressedSawdust = { true, true, true, false, true, false, true, false, true, true, true, false, false, false, false, false } } function initCraftingGrid() for gridKey, gridValue in pairs(craftingGrid) do print(type(gridValue)) local requiredCraftingSlots = 0 -- that's the first slot where the items would go to local craftResultSlot for slotKey, slotValue in pairs(gridValue) do if slotValue then requiredCraftingSlots = requiredCraftingSlots + 1 else if not craftResultSlot then craftResultSlot = slotKey end gridValue[ slotKey ] = nil -- erase value from table end end end end initCraftingGrid()
- ComputerCraft | Programmable Computers for Minecraft
- → VADemon's Content