我可以在 Lua 内部调用 table 吗?

Can I call a table inside itself in Lua?

所以我正在尝试这个:

buttons = {

{imageNothing = love.graphics.newImage("buildingButtonNotSelected.png"), imageHovering = love.graphics.newImage("buildingButtonHovering.png"), imageSelected = love.graphics.newImage("buildingButton.png"),imgW = buttons[1].imageNothing:getWidth(), imgH = buttons[1].imageNothing:getHeight(), imgX = windowWidth - buttons[1].imgW, imgY = windowHeight - buttons[1].imgH, selected = false, hovering = false}

}

我目前收到此错误: 尝试索引全局 'buttons'(零值)

有什么想法吗?

你不能。

table 在 table 构造函数被求值之前不会被创建。所以 buttons 没有在 table 构造函数中定义。

您可以初始化 buttons 而无需在 table 构造函数中使用 `buttons,然后稍后添加这些字段。

buttons = {
  {
    imageNothing = love.graphics.newImage("buildingButtonNotSelected.png"), 
    imageHovering = love.graphics.newImage("buildingButtonHovering.png"), 
    imageSelected = love.graphics.newImage("buildingButton.png"),
    selected = false, 
    hovering = false
  }
}

buttons.imgW = buttons[1].imageNothing:getWidth()
buttons.imgH = buttons[1].imageNothing:getHeight()
buttons.imgX = windowWidth - buttons[1].imgW
buttons.imgY = windowHeight - buttons[1].imgH