'for' limit必须是一个数字 LUA错误

所以,我得到了一个看起来像这样的错误:[ERROR] addons/itemstore/lua/itemstore/lua/itemsure/vgui/container.lua:43 'for' limit必须是一个数字

这是container.lua

local PANEL = {}

AccessorFunc( PANEL, "ContainerID", "ContainerID" )
AccessorFunc( PANEL, "Rows", "Rows" )
AccessorFunc( PANEL, "Columns", "Columns" )

function PANEL:Init()
    self.Items = {}

    table.insert( itemstore.containers.Panels, self )
end

function PANEL:Refresh()
    local container = itemstore.containers.Get( self:GetContainerID() )
    if ( container ) then
        for i = 1, container.Size do
            if ( not self.Items[ i ] ) then
                self.Items[ i ] = self:Add( "ItemStoreSlot" )
            end

            local panel = self.Items[ i ]
            panel:SetItem( container:GetItem( i ) )
            panel:SetContainerID( self:GetContainerID() )
            panel:SetSlot( i )
            panel:InvalidateLayout()
        end

        self:InvalidateLayout()
    end
end

function PANEL:SetContainerID( containerid )
    self.ContainerID = containerid
    self:Refresh()
end

function PANEL:PerformLayout()
    self:SetSpaceX( 1 )
    self:SetSpaceY( 1 )

    local container = itemstore.containers.Get( self:GetContainerID() )
    if ( container ) then
        for i = 1, container.Size do
            local panel = self.Items[ i ]

            if ( panel ) then
                panel:SetSize( unpack( itemstore.config.SlotSize ) )
            end
        end
    end

    self.BaseClass.PerformLayout( self )
end

vgui.Register( "ItemStoreContainer", PANEL, "DIconLayout" )

有什么解决方案吗?我想不出任何办法,因为我认为它应该正常工作?

点赞
用户2858170
用户2858170

错误非常明显。在第43行,您有一个for语句,它使用container.Size作为其限制条件,在您的情况下并不是一个数字。

解决方案:

使用数字作为for循环的限制条件。如果您必须使用来自“外部”的container.Size,那么找出它为什么不是数字以及您可以采取的措施。如果您不能确保它是数字,那么您不能将其用作for循环的限制条件。

因此,将您的for循环放在一个“if type(container.Size)==“number ”then”语句或类似语句中。

2017-08-06 22:28:33