1. Post #41
    DarkShadow6's Avatar
    September 2009
    537 Posts
    I'm a big fan of really compact scripts
    Yeah obviously.
    I like your code, besides the fact that you don't space your arithmetic and commas I am the master of not reading things.

  2. Post #42
    daranable's Avatar
    November 2011
    10 Posts
    Heh, I am a bit obsessive when it comes to my coding style.

    Here is a good example of how I format code that I intend to release at some point:
    https://github.com/daranable/Data-Si...datasignal.lua

    My biggest difference from most people. Is if I intend to release code, I obsessively use luadoc comments to document my functions. (I realise I missed one on the script I linked to, but meh.)

    And I am in the same boat as op in using only lua syntax for writing lua. I never understood why we got special language syntax for GMLua.

    Other than that, I will generally adjust my coding style to the project I am working on, so that the project stays internally consistent.

  3. Post #43
    Gold Member
    Kogitsune's Avatar
    September 2005
    2,861 Posts
    I don't like most of the c-style features and stick to standard Lua whenever possible, except continue since that's dead useful.

    I also prefer to use plenty of space in my code since it looks cleaner to me:

    local function HUDPaint( )
        surface.SetMaterial( Head )
        
        local index, pl, x, y, w, h, r, n, hp, j, k, j2, k2, wep, str
        
        pl = LocalPlayer( )
        
        if not IsValid( pl ) then
            return
        end
        
        wep = pl:GetActiveWeapon( )
        
        hp = math.Clamp( pl:Health( ), 0, 999 )
        
        index = math.Clamp( math.ceil( ( hp ) / 20 ), 0, 5 )
        
        if pl:Health( ) > 0 and Slot == "Dead" then
            Slot = "Center"
        end
        
        if pl:Health( ) <= 0 and Slot ~= "Dead" then
            Slot = "Dead"
        end
        
        r = ScrW( )
        n = ScrH( )
        
        --Draw face
        x = r / 2 - Coords[ index ].q / 2
        y = n - Coords[ index ][ Slot ].h
        
        
        
        Poly[ 1 ].x = x
        Poly[ 1 ].y = y
        Poly[ 1 ].u = Coords[ index ][ Slot ].x
        Poly[ 1 ].v = Coords[ index ][ Slot ].y
    
        Poly[ 2 ].x = Poly[ 1 ].x + Coords[ index ][ Slot ].w
        Poly[ 2 ].y = Poly[ 1 ].y
        Poly[ 2 ].u = Coords[ index ][ Slot ].x2
        Poly[ 2 ].v = Coords[ index ][ Slot ].y
        
        Poly[ 3 ].x = Poly[ 2 ].x
        Poly[ 3 ].y = y + Coords[ index ][ Slot ].h
        Poly[ 3 ].u = Poly[ 2 ].u
        Poly[ 3 ].v = Coords[ index ][ Slot ].y2
        
        Poly[ 4 ].x = Poly[ 1 ].x
        Poly[ 4 ].y = Poly[ 3 ].y
        Poly[ 4 ].v = Poly[ 3 ].v
        Poly[ 4 ].u = Poly[ 1 ].u

    I prefer to declare all the locals I am going to be using at the top of the current scope - this helps me keep track of which variables I am actually using and helps me keep my train of thought on the rails.

    I dislike creating lambdas and prefer to create a named local function - this is more one of those stupid micro-optimization things since you end up with less generated garbage.

    I extremely dislike creating throw-away tables for single-instance things and will re-use them as much as possible when it would make sense. If I'm going to be doing a lot of traces, I'll only use one trace setup table. If I'm going to be using DrawPoly or CalcView I create a single table for them. Obviously there's cases where I can't really do that ( when they need to be distinct ), but this greatly enhances performance.

    For some reason, I indent code that would be wrapped with a Using block in C# / VB:

    Code:
    eff = EffectData( )
            eff:SetOrigin( tr.HitPos )
            eff:SetStart( tr2.HitPos )
        util.Effect( "lmage_bolt", eff )
    It keeps a flow to my code I guess - I probably only do it out of habit.
    Reply With Quote Edit / Delete Reply Windows 7 United States Show Events Winner Winner x 1Agree Agree x 1 (list)

  4. Post #44
    KatNotDinner's Avatar
    June 2011
    792 Posts
    I don't like most of the c-style features and stick to standard Lua whenever possible, except continue since that's dead useful.

    I also prefer to use plenty of space in my code since it looks cleaner to me:


    code

    I prefer to declare all the locals I am going to be using at the top of the current scope - this helps me keep track of which variables I am actually using and helps me keep my train of thought on the rails.

    I dislike creating lambdas and prefer to create a named local function - this is more one of those stupid micro-optimization things since you end up with less generated garbage.

    I extremely dislike creating throw-away tables for single-instance things and will re-use them as much as possible when it would make sense. If I'm going to be doing a lot of traces, I'll only use one trace setup table. If I'm going to be using DrawPoly or CalcView I create a single table for them. Obviously there's cases where I can't really do that ( when they need to be distinct ), but this greatly enhances performance.

    For some reason, I indent code that would be wrapped with a Using block in C# / VB:

    Code:
    eff = EffectData( )
            eff:SetOrigin( tr.HitPos )
            eff:SetStart( tr2.HitPos )
        util.Effect( "lmage_bolt", eff )
    It keeps a flow to my code I guess - I probably only do it out of habit.
    Kinda off-topic but can you show me how "continue" is used? I've heard it exists in GMod lua bit I've never seen it in use.

  5. Post #45

    May 2010
    1,127 Posts
    Heh, I am a bit obsessive when it comes to my coding style.

    Here is a good example of how I format code that I intend to release at some point:
    https://github.com/daranable/Data-Si...datasignal.lua

    My biggest difference from most people. Is if I intend to release code, I obsessively use luadoc comments to document my functions. (I realise I missed one on the script I linked to, but meh.)

    And I am in the same boat as op in using only lua syntax for writing lua. I never understood why we got special language syntax for GMLua.

    Other than that, I will generally adjust my coding style to the project I am working on, so that the project stays internally consistent.
    your code is beautiful and you should feel beautiful

  6. Post #46
    Get your own DarkRP Server!
    FPtje's Avatar
    January 2006
    5,726 Posts
    Kinda off-topic but can you show me how "continue" is used? I've heard it exists in GMod lua bit I've never seen it in use.
    for k,v in pairs(player.GetAll()) do
        if v == LocalPlayer() then continue end
        -- Do stuff for every player except the LocalPlayer here.
    end
    

    It looks nicer than:
    for k,v in pairs(player.GetAll()) do
        if v ~= LocalPlayer() then
            -- Do stuff for every player except the LocalPlayer here.
        end
    end
    
    Reply With Quote Edit / Delete Reply Windows 7 Netherlands Show Events Informative Informative x 2 (list)

  7. Post #47
    PROUD BRONY 4LYFE
    Drakehawke's Avatar
    February 2009
    3,312 Posts
    Kinda off-topic but can you show me how "continue" is used? I've heard it exists in GMod lua bit I've never seen it in use.
    It's similar to "break", if you have "break" in a loop then it breaks out of the loop where it is and carries on with the rest of the code, "continue" will tell Lua to skip the current iteration, but NOT cancel the entire loop, so it just continues to the next iteration.


    For example:

    for i = 1, 10, 1 do
        
        if i ~= 2 then
                
            print( i )
    
        end
    
    end
    

    is the same as

    for i = 1, 10, 1 do
        
        if i == 2 then continue end
        print( i )
    
    end

    Output 1, 3, 4, 5, 6, 7, 8, 9, 10

    You can get around without it, but you can imagine if you had a large number of conditions to check on each iteration of a loop, it could get very complicated as you can't simply "return" or "break" as then all the subsequent iterations wouldn't run (you could also end up with a large amount of tabs before each line and it generally just wouldn't look very clean).

    Edit: ninja'd :c
    Reply With Quote Edit / Delete Reply Windows Vista United Kingdom Show Events Informative Informative x 2 (list)

  8. Post #48
    Gold Member

    July 2006
    1,965 Posts
    Kinda off-topic but can you show me how "continue" is used? I've heard it exists in GMod lua bit I've never seen it in use.
    It jumps to the next item in a loop.

    for _, ply in ipairs(player.GetAll()) do
    	-- Skip this player if his health is less than 50.
    	
    	if ply:Health() < 50 then
    		continue
    	end
    	
    	print(ply:Nick() .. "'s health is over 50.")
    end
    

    Edited:

    Wow, I'm late.
    Reply With Quote Edit / Delete Reply Linux Finland Show Events Funny Funny x 1Informative Informative x 1 (list)

  9. Post #49
    whitespace's Avatar
    November 2008
    869 Posts
    Since when has 'continue' existed in gmod?

  10. Post #50
    MoronYard
    _nonSENSE's Avatar
    May 2010
    1,266 Posts
    Since when has 'continue' existed in gmod?
    A while.
    Reply With Quote Edit / Delete Reply Windows 7 Germany Show Events Funny Funny x 1Agree Agree x 1Informative Informative x 1 (list)

  11. Post #51
    I fucking love eggs!!!
    Averice's Avatar
    July 2007
    1,991 Posts
    I always use parenthesis in my if statements, even when it's only a boolean like:

    if( something ) then
     -- Do somehting.
    end
    

    I don't know when I started doing it but I've gotten used to it and I like it, I know a lot of people don't like that sort of style but I'm not going to change just to please someone else.
    Reply With Quote Edit / Delete Reply Windows 7 Australia Show Events Agree Agree x 3Winner Winner x 1 (list)

  12. Post #52
    JustSoFaded's Avatar
    December 2011
    432 Posts
    That's exactly how I do it as well Averice. I also use the semicolon.

    function test()
         if( bool ) then
              FunctionCall(var1, var2, var3);
         end
    end
    
    Reply With Quote Edit / Delete Reply Windows 7 United States Show Events Agree Agree x 1Disagree Disagree x 1 (list)

  13. Post #53
    Get your own DarkRP Server!
    FPtje's Avatar
    January 2006
    5,726 Posts
    I personally don't use them because I believe they are redundant characters. :)

    Edit:
    Lately I've been noticing that I comment my code a lot. I'm noticing it in my C# exercises in uni, but also in my Lua things.
    Reply With Quote Edit / Delete Reply Windows 7 Netherlands Show Events Agree Agree x 1 (list)

  14. Post #54
    dylanb5123's Avatar
    January 2011
    227 Posts
    I also use the semicolon
    ...Ewwww
    Reply With Quote Edit / Delete Reply Windows 7 United States Show Events Disagree Disagree x 5Agree Agree x 5 (list)

  15. Post #55
    daranable's Avatar
    November 2011
    10 Posts
    Heh, I don't know why people hate semi-colons. They are slightly annoying to get used to. But personally now that I am getting more used to them because of JavaScript and Java I actually am starting to prefer them.

    To me it feels like I have more control over the end of a statement.

    I only use them in Lua projects I am the lead on.
    But if I am in a project that doesn't use them like I said early, I mold to keep consistent with said project.
    Reply With Quote Edit / Delete Reply Windows 7 United States Show Events Friendly Friendly x 2 (list)

  16. Post #56
    dylanb5123's Avatar
    January 2011
    227 Posts
    They are slightly annoying to get used to.
    Exactly lol

  17. Post #57
    Gold Member
    Divran's Avatar
    April 2008
    2,629 Posts
    Made even more annoying by editors that tell you exactly where you're missing semicolons, but can't place them there for you.
    Reply With Quote Edit / Delete Reply Windows 7 Sweden Show Events Agree Agree x 1 (list)

  18. Post #58
    Your local friendly Lua helper!
    bobbleheadbob's Avatar
    July 2011
    485 Posts
    --I've noticed that some people do it like this:
    print( "hello." )
    function( name, ply )
    self:SetVector( ( Vector( 36, 5, 122 ) * Vector( 323, 232, 21 ) ) )
    
    --I used to do this, but then I realized that it was a waste of finger energy. :P
    --Now I do this:
    print("hello")
    function(name, ply)
    self:SetVector((Vector(36, 5, 122) * Vector(323, 232, 21)))
    
    Reply With Quote Edit / Delete Reply Windows XP United States Show Events Agree Agree x 2Disagree Disagree x 1 (list)

  19. Post #59
    Gold Member
    Bynari's Avatar
    August 2005
    521 Posts
    My preferred method is a tad unorthodox, I'll admit, but when I'm coding I like to roll my face across the keyboard. Not half-assed, either; a nice full ear to ear roll.

    AddCSLuaFile( "cl_init.lua" )
    AddCSLuaFile( "shared.lua" )
     
    include('shared.lua')
    
    
    function rollFace()
    	
          azwazwazwazwazwazwazwl.wwwwrcfgvbujhl; mklrtfuyjh
          tttttgyuhujhyieacwgdhrfmbgiyh,upk;leacvsghdnvkhm,jlm
          xdcfgvecbhcrjntvuyn78,l8p2olnl;mnp889mnklnaika;
    
    end
    


    I also sometimes find it refreshing (especially when I've hit a particularly difficult problem) to pick up my keyboard and repeatedly bash my forehead into the keys.

    AddCSLuaFile( "cl_init.lua" )
    AddCSLuaFile( "shared.lua" )
     
    include('shared.lua')
    
    
    function slamFace()
    	
          jijbniitbunkjunfgbnkjbnkutbufntkufnktbunkfjbndklklfnkbjnkftn
          olhmnlfkthmnflhtjnfljkbnkdndhkdjhndkuhndktjnkdfhndfkcjkbn
          ddrtjkndklhjnldfhkjljjndhlndfhjnjni d uidthjidjnidh dghedghjiodfh
    
    end
    

    On an interesting side note- when I run these scripts through gmod, the console errors tend to spam Katy Perry lyrics.
    Reply With Quote Edit / Delete Reply Windows 7 United States Show Events Funny Funny x 17 (list)

  20. Post #60
    Gold Member
    Shifty Pete's Avatar
    May 2011
    428 Posts
    It drives me insane when people use loads of spaces instead of tabs >_>
    Reply With Quote Edit / Delete Reply Windows 7 Australia Show Events Agree Agree x 11 (list)

  21. Post #61
    JustSoFaded's Avatar
    December 2011
    432 Posts


    It drives me insane when people do this stupid shit.
    Reply With Quote Edit / Delete Reply Windows 7 United States Show Events Agree Agree x 3 (list)

  22. Post #62
    Get your own DarkRP Server!
    FPtje's Avatar
    January 2006
    5,726 Posts


    It drives me insane when people do this stupid shit.
    I know of exactly one person who does that. I just assume he's bad at programming since he's done that since the end of 2008
    Reply With Quote Edit / Delete Reply Netherlands Show Events Funny Funny x 1 (list)

  23. Post #63
    JustSoFaded's Avatar
    December 2011
    432 Posts
    It's really annoying when you are trying to modify or fix someone elses script.

  24. Post #64
    Get your own DarkRP Server!
    FPtje's Avatar
    January 2006
    5,726 Posts
    The code is ridiculous as well, rather than looping through the player.GetAll() table, it checks the owner of every entity. Why not just make a for loop that goes from 1 to a million, picks a random player, check the name and kick if it's equal to "ChickenFriendSteak"?

    Seriously though, is this really someone's code?

  25. Post #65
    JustSoFaded's Avatar
    December 2011
    432 Posts
    The code is ridiculous as well, rather than looping through the player.GetAll() table, it checks the owner of every entity. Why not just make a for loop that goes from 1 to a million, picks a random player, check the name and kick if it's equal to "ChickenFriendSteak"?

    Seriously though, is this really someone's code?
    Actually I wrote that code in the browser. I know you can do that, I was just writing bullshit code to show the way retards tab their ends but nothing else. I was trying to have lots of lines so there would be lots of ends, lol.
    Reply With Quote Edit / Delete Reply Windows 7 United States Show Events Agree Agree x 1 (list)

  26. Post #66
    Please waste more of your money changing this title again.
    Gmod4ever's Avatar
    August 2005
    6,840 Posts


    It drives me insane when people do this stupid shit.
    Jesus Christ how horrifying

    That tabbing is an absolute abomination. Who the hell would ever tab like that?
    Reply With Quote Edit / Delete Reply Windows 7 United States Show Events Agree Agree x 2 (list)

  27. Post #67
    JustSoFaded's Avatar
    December 2011
    432 Posts
    Lately I've been seeing more and more beginners tabbing like that.
    Reply With Quote Edit / Delete Reply Windows 7 United States Show Events Friendly Friendly x 1 (list)

  28. Post #68
    Gold Member
    Divran's Avatar
    April 2008
    2,629 Posts
    Expression 2 has auto tabbing. I've seen people post E2 code where they either hit backspace to delete the tabs, or go to the settings menu to uncheck the auto tabbing just so their code can look like shit.
    Reply With Quote Edit / Delete Reply Windows 7 Sweden Show Events Funny Funny x 4 (list)

  29. Post #69
    DarkShadow6's Avatar
    September 2009
    537 Posts
    Jesus Christ how horrifying

    That tabbing is an absolute abomination. Who the hell would ever tab like that?
    A friend of mine, mcfirno10, tabs like that.
    He is a scriptkiddie to the MAX, bro.

    Expression 2 has auto tabbing. I've seen people post E2 code where they either hit backspace to delete the tabs, or go to the settings menu to uncheck the auto tabbing just so their code can look like shit.
    How is it even possible to fail at autotabbing.

  30. Post #70
    Gold Member
    Kogitsune's Avatar
    September 2005
    2,861 Posts
    Jesus Christ how horrifying

    That tabbing is an absolute abomination. Who the hell would ever tab like that?
    They copy and paste code from various sources. Code ends up looking flat or otherwise malformed when you take tabs or pseudo tabs from code tags / web pages and put them in your script.