Cool vgui techniques

Does anyone have any unique way of doing certain things.
Maybe reducing the amount of lines to do x, down from 40 to 12 or something. Just looking for some more effecient ways to code things.

Docking for panel positioning and sizing.

Make helper functions for things you intend to do more than once.

Define helper functions, I’m relatively new and this is something I have not covered.

They are just functions you can use to do stuff that is usually very similar, e.g. adding labels:



local function AddLabel( name, somepanel )
    local l = vgui.Create( 'DLabel', somepanel )
    l:SetText( name )
    l:SizeToContents()
end

AddLabel( 'somename', somepanel )
AddLabel( 'somename2', somepanel )
AddLabel( 'somename3', somepanel )


Stop using ifs and fors (unless no other option) and start using tables.
That’ll reduce lines of code by atleast 20%

Could you show me differenent examples of how this could be done, or anyone else?

Instead of:


local a = 3
if a == 1 then
    print( "First" )
elseif a == 2 then
    print( "Second" ) 
else if a == 3 then
    print( "Third" )
end

You can do:


local n = { "First", "Second", "Third" }

local a = 3
print( n[ a ] )

Thank you.