GLua——字符串库的一部分

我有一个 GUI,需要显示表格中的信息。当我打开 GUI 时,出现以下错误: "attempt to index a string value with bad key ('Index' is not part of the string library)"

SH 文件。在这个文件中,我放置了表格信息:

MyList = {}
MyList = {
    Index = 1,
    Name = "名称 1",
    Class = "示例类",
    Description = "说明 1",
    Model = "模型名称",
    Color = Color(255, 255, 255, 255)
}

CL 文件。这个文件包含了代码的一部分,这个错误发生在第一行:

for k, v in SortedPairsByMemberValue( MyList, "Index" ) do
    if v.Class == "示例类" then
        local mainbuttons = vgui.Create( "DCollapsibleCategory", category )
        mainbuttons:Dock(TOP)
        mainbuttons:DockMargin(0, 6, 0, 2)
        mainbuttons.Header:SetTall(24)
        mainbuttons:SetExpanded(0)
        mainbuttons:SetLabel("")
        mainbuttons.Text = v.Name
        function mainbuttons:Paint(w, h)
            local h = 24
            surface.SetDrawColor(v.Color)
            surface.DrawRect(0, 0, w, h)

            surface.SetFont("NovuxFont.ArialLight.16")
            local textw, texth = surface.GetTextSize(self.Text)
            surface.SetTextColor(Color(255, 255, 255))
            surface.SetTextPos(16, h / 2  - texth / 2)
            surface.DrawText(self.Text)
        end

        local craftpanel = vgui.Create( "DPanel", mainbuttons )
        craftpanel:SetPos( 0, 25 )
        craftpanel:SetSize( mainframescroll:GetWide(), 250 )
        craftpanel.Paint = function() -- Paint function
            surface.SetDrawColor( 65, 65, 65, 0 )
            surface.DrawRect( 0, 0, craftpanel:GetWide(), craftpanel:GetTall() )
        end

        local spoilertext = vgui.Create( "DLabel", mainbuttons )
        spoilertext:SetText( v.Description )
        spoilertext:SetTextColor( Color( 255, 255, 255 ) )
        spoilertext:SetFont( "NovuxFont.ArialLight.16" )
        spoilertext:SetPos( 108, 32 )
        spoilertext:SetSize( mainframescroll:GetWide(), 25 )
        spoilertext.Paint = function( self, w, h )
            draw.RoundedBox( 0, 0, 0, w, h, Color( 102, 102, 102, 0 ))
        end

        local modelframe = vgui.Create( "DModelPanel", mainbuttons )
        modelframe:SetPos( 0, 65 )
        modelframe:SetSize( 300, 200 )
        modelframe:SetModel( v.Model )
        modelframe:GetEntity():SetAngles( Angle( -10, 0, 15 ) )
        local mn, mx = modelframe.Entity:GetRenderBounds()
        local size = 0
        size = math.max( size, math.abs( mn.x ) + math.abs( mx.x ) )
        size = math.max( size, math.abs( mn.y ) + math.abs( mx.y ) )
        size = math.max( size, math.abs( mn.z ) + math.abs( mx.z ) )
        modelframe:SetFOV( 45 )
        modelframe:SetCamPos( Vector( size, size, size ) )
        modelframe:SetLookAt( (mn + mx) * 0.5 )
        function modelframe:LayoutEntity( Entity )
            return
        end
    end
end
点赞
用户8621712
用户8621712

问题在于你把整个项目放进了 MyList,根据 SortedPairsByMemberValue 的逻辑,你应该把新表放在 MyList 里面。

修复方法如下:

MyList = {
    {
        Index = 1,
        Name = "Name 1",
        Class = "exampleclass",
        Description = "Desc 1",
        Model = "modelname",
        Color = Color(255, 255, 255, 255)
    }
}

注意现在这是一个嵌套表。

2020-12-03 01:16:31