lualove2dlua-userdata

love2D error: unpack expected table got userdata


When I attempt to unpack this table to call on the images, the program returns the error "bad argument to unpack (table expected got userdata)"

textures = {love.graphics.newImage("image.png"),
      love.graphics.newImage("image.png"),
      love.graphics.newImage("image.png"),
      love.graphics.newImage("image.png"),
      }

This is the table I am using

  drawScreenLineTexture[x] = {unpack(textures[map[mapX][mapY]])}
if side == 1 then
  drawScreenLineTexture[x][1] = drawScreenLineTexture[x][1] / 2
  drawScreenLineTexture[x][2] = drawScreenLineTexture[x][2] / 2
  drawScreenLineTexture[x][3] = drawScreenLineTexture[x][3] / 2
end

This is where I attempt to unpack the table


Solution

  • If you want to unpack the table textures:

    textures = {love.graphics.newImage("image.png"),
          love.graphics.newImage("image.png"),
          love.graphics.newImage("image.png"),
          love.graphics.newImage("image.png"),
          }
    

    you have to write unpack(textures), not unpack(textures[map[mapX][mapY]) as textures[map[mapX][mapY] is obviously not the table textures but one of its sub-elements which happes to be of type userdata.