Warm tip: This article is reproduced from serverfault.com, please click
lua

GLua

发布于 2020-12-03 01:00:22

I have gui wherein I need show information from table When I open gui, I have this error: "attempt to index a string value with bad key ('Index' is not part of the string library)"

SH file. In this file I put table information

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

CL file. This file contains the part of the code where the error occurs. The error occurs on the first line

for k, v in SortedPairsByMemberValue( MyList, "Index" ) do
    if v.Class == "exampleclass" 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
Questioner
VictorNishtyakov
Viewed
0
Spar 2020-12-03 09:16:31

The problem is that you put the whole item in MyList where by logic of SortedPairsByMemberValue you supposed to put new table inside MyList.

This fixes the problem:

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

Notice that now this is a nested table.