• Cool vgui techniques
    8 replies, posted
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. [url]http://wiki.garrysmod.com/page/Panel/Dock[/url]
Make helper functions for things you intend to do more than once.
[QUOTE=DanielHershey;50291115]Make helper functions for things you intend to do more than once.[/QUOTE] Define helper functions, I'm relatively new and this is something I have not covered.
[QUOTE=dannyf127;50291135]Define helper functions, I'm relatively new and this is something I have not covered.[/QUOTE] They are just functions you can use to do stuff that is usually very similar, e.g. adding labels: [CODE] 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 ) [/CODE]
Stop using ifs and fors (unless no other option) and start using tables. That'll reduce lines of code by atleast 20%
[QUOTE=Netheous;50291266]Stop using ifs and fors (unless no other option) and start using tables. That'll reduce lines of code by atleast 20%[/QUOTE] Could you show me differenent examples of how this could be done, or anyone else?
[QUOTE=dannyf127;50292581]Could you show me differenent examples of how this could be done, or anyone else?[/QUOTE] Instead of: [code]local a = 3 if a == 1 then print( "First" ) elseif a == 2 then print( "Second" ) else if a == 3 then print( "Third" ) end[/code] You can do: [code]local n = { "First", "Second", "Third" } local a = 3 print( n[ a ] )[/code]
[QUOTE=Netheous;50292593]Instead of: [code]local a = 3 if a == 1 then print( "First" ) elseif a == 2 then print( "Second" ) else if a == 3 then print( "Third" ) end[/code] You can do: [code]local n = { "First", "Second", "Third" } local a = 3 print( n[ a ] )[/code][/QUOTE] Thank you.
Sorry, you need to Log In to post a reply to this thread.