1. Post #1
    DarkShadow6's Avatar
    September 2009
    534 Posts
    All of my life, I've seen programmer after programmer; all of them have their own styles and methods of working. Some of these practices are good and beneficial to the program in general, some are purely aesthetic for said programmer and kill off performance. You can tell a lot about a person from the ways they format their code. So tell me, Facepunch, how do you program? What niches do you have?

    Here's some info about my habits:
    When I make big projects that need global access, I use a global table (like the module system, for instance) that contain all of my
    I choose to tabulate every line for organization. I can't read script unless it's tabulated. I mean, seriously, how could you not do it? Python requires you to tabulate your code; it's what it uses for block detection, like Lua's if/then and end, i.e.
    function ThisIsAFunction()
        print("Tabs!")
        local Blah = function(fawnk)
            fawnk()
        end
        Blah(function()
            print("Hey, sup")
        end)
    end
    
    I also do not use the extra additions to the language that Garry has provided for our usage, should we see fit; this is Lua, and as such, I want my code to be Lua. Do not want !=, /**/, etc, kthx.
    When I program in bracket-based languages (like C/++, Java, etc) I always put the brackets on their own lines, i.e.
    Code:
    public class Brackets
    {
        public static int main(String Args[])
        {
            System.out.println("Code!");
            ...
            return 0;
        }
    }
    Reply With Quote Edit / Delete Reply Windows 7 United States Show Events Agree Agree x 6Dumb Dumb x 5 (list)

  2. Post #2
    Pycские Идиот
    LauScript's Avatar
    June 2010
    2,447 Posts
    This is a really cool thread, I think anyway. I don't want to post an example because I feel it won't be exact to my style. Soo I'm just going to post this which I scripted yesterday, I notice that my style is really inconsistent but anyway what do you think? It's not like how you do it, keeping lua all to lua, i make use of pretty much every c++ operator garry has added instead of the lua variants. Just habbit really.

    require("glon");
    
    TreeSauce.characters = {};
    
    if ( SERVER ) then
    
    -- Get a players characters, returns false if they have no characters.
    function TreeSauce.characters.GetPlayerCharacters(player)
    	if ( !player.characters && !player:GetPData("Characters") ) then
    		return false;
    	end;
    	
    	if ( player.characters ) then
    		return player.characters;
    	else
    		return glon.decode( player:GetPData("Characters") );
    	end;
    end;
    
    -- Load the saved version of their characters.
    function TreeSauce.characters.LoadSavedCharacters(player)
    	if ( player:GetPData("Characters") ) then
    		player.characters = glon.decode(player:GetPData("Characters"));
    		
    		for name,data in pairs( player.characters ) do
    			if ( data.inventory ) then
    				
    				local oldInvData = data.inventory;
    				local oldMaxWeight = data.weight;
    	
    				player.characters[name].inventory = TreeSauce.container:Create();
    				player.characters[name].inventory:SetMaxWeight(oldMaxWeight);
    				player.characters[name].inventory:OverrideInventory(oldInvData);
    			end;
    		end;
    	end;
    end;
    
    -- Save the characters table, if an override is given
    function TreeSauce.characters.SavePlayerCharacters( player, override )
    	if ( !player.characters ) then
    		if ( !override ) then return; end;
    	end;
    	
    	local charlist;
    	
    	if ( override ) then
    		charlist = override;
    	else
    		charlist = player.characters;
    	end;
    	
    	for k,v in pairs( charlist ) do
    		if ( v.inventory ) then
    			v.inventory = v.inventory:GetInventoryList();
    		end;
    	end;
    	
    	local charlistenc = glon.encode(charlist);
    	
    	player:SetPData( "Characters", charlistenc );
    end;
    
    -- Create a new character.
    function TreeSauce.characters.PlayerCreateCharacter( player, sGender, sModel, sNameFirst, sNameLast, sDescription )
    	if ( !player ) then return; end;
    	if ( !sGender ) then return; end;
    	if ( !sModel ) then return; end;
    	if ( !sNameFirst ) then return; end;
    	if ( !sNameLast ) then return; end;
    	if ( !sDescription ) then return; end;
    	
    	if ( !player.characters ) then
    		player.characters = {};
    	end;
    	
    	local nameFull = sNameFirst .. " " .. sNameLast;
    	local data = {};
    	
    	data.gender = sGender;
    	data.model = sModel;
    	data.description = sDescription;
    	--data.inventory = {};
    	
    	player.characters[nameFull] = data;
    	
    	TreeSauce.characters.SavePlayerCharacters(player);
    end;
    
    -- Delete a character
    function TreeSauce.characters.Delete( player, character )
    	if ( !player or !character ) then return; end;
    	if ( !TreeSauce.characters.GetPlayerCharacters(player) ) then return; end;
    	
    	if ( !player.characters ) then
    		TreeSauce.characters.LoadSavedCharacters(player);
    	end;
    	
    	if ( !player.characters[character] ) then return; end;
    	
    	player.characters[character] = nil;
    	
    	TreeSauce.characters.SavePlayerCharacters(player);
    	
    	-- This is where we want to check if they were logged onto that character, and if they were display a notification and then the selection menu.
    end;
    
    -- Set a characters model.
    function TreeSauce.characters.SetCharacterModel( player, character, model )
    	if ( !player or !character or !model ) then return; end;
    	if ( !TreeSauce.characters.GetPlayerCharacters(player) ) then return; end;
    	
    	if ( !player.characters ) then
    		TreeSauce.characters.LoadSavedCharacters(player);
    	end;
    
    	if ( !player.characters[character] ) then return; end;
    	
    	player.characters[character].model = model;
    	
    	TreeSauce.characters.SavePlayerCharacters(player);
    	
    	-- this is where we check if they are logged in, and if they are set their player entities model, UNLESS THEY ARE WEARING CLOTHING( update this when we add it ).
    end;
    
    -- Get a characters model.
    function TreeSauce.characters.GetCharacterModel( player, character )
    	if ( !player or !character ) then return; end;
    	if ( !TreeSauce.characters.GetPlayerCharacters(player) ) then return; end;
    	
    	if ( !player.characters ) then
    		TreeSauce.characters.LoadSavedCharacters(player);
    	end;
    
    	if ( !player.characters[character] ) then return; end;
    	
    	return player.characters[character].model;
    end;
    
    -- Set a characters description.
    function TreeSauce.characters.SetCharacterDescription( player, character, description )
    	if ( !player or !character or !description ) then return; end;
    	if ( !TreeSauce.characters.GetPlayerCharacters(player) ) then return; end;
    	
    	if ( !player.characters ) then
    		TreeSauce.characters.LoadSavedCharacters(player);
    	end;
    
    	if ( !player.characters[character] ) then return; end;
    	
    	player.characters[character].description = description;
    	
    	TreeSauce.characters.SavePlayerCharacters(player);
    end;
    
    -- Get a characters description
    function TreeSauce.characters.GetCharacterDescription( player, character )
    	if ( !player or !character ) then return; end;
    	if ( !TreeSauce.characters.GetPlayerCharacters(player) ) then return; end;
    	
    	if ( !player.characters ) then
    		TreeSauce.characters.LoadSavedCharacters(player);
    	end;
    
    	if ( !player.characters[character] ) then return; end;
    	
    	return player.characters[character].description;
    end;
    
    -- Change a characters name, now this should probably be restricted to admin access.
    function TreeSauce.characters.ChangeCharacterName( player, character, newfirst, newlast )
    	if ( !player or !character or !newfirst or !newlast) then return; end;
    	if ( !TreeSauce.characters.GetPlayerCharacters(player) ) then return; end;
    	
    	if ( !player.characters ) then
    		TreeSauce.characters.LoadSavedCharacters(player);
    	end;
    
    	if ( !player.characters[character] ) then return; end;
    	
    	local copy = player.characters[character];
    	local nameFull = newfirst .. " " .. newlast;
    	
    	player.characters[nameFull] = copy;
    	player.characters[character] = nil;
    	
    	TreeSauce.characters.SavePlayerCharacters(player);
    	TreeSauce.characters.ClientRequestLocalCopy( player, nameFull);
    	TreeSauce.characters.DeleteCharacterLocalCopy( player, character );
    	
    	-- If they are logged in you might have to change some shit, best keep this to a hook. when i make the login make sure to call it only if they are on that char, no point otherwise.
    	TreeSauce.characters.OnNameChange(player,character,nameFull);
    end;
    
    -- This is the hook incase we need to update a player after a name change,while they are logged in.
    function TreeSauce.characters.OnNameChange( player, old, new )
    	if ( player.alias == old ) then
    		player.alias = new;
    		player:SetModel( player.characters[new].model );
    	end;
    end;
    
    -- Activate a character for use.
    function TreeSauce.characters.Login( player, character )
    	if ( !player or !character ) then return; end;
    	if ( !TreeSauce.characters.GetPlayerCharacters(player) ) then return; end;
    	
    	if ( !player.characters ) then
    		TreeSauce.characters.LoadSavedCharacters(player);
    	end;
    
    	if ( !player.characters[character] ) then return; end;
    	
    	local data = player.characters[character];
    	
    	player.alias = character;
    	player:SetModel( data.model );
    	-- other data will probably be sent here, just saying for now.
    	
    	TreeSauce.characters.OnCharacterLogin( player, character );
    end;
    
    -- an event for when they login to their character, handle data etc. here
    function TreeSauce.characters.OnCharacterLogin( player, character )
    end;
    
    -- Called when a client wants his local version of his client data updated.
    function TreeSauce.characters.ClientRequestLocalCopy( player, character )
    	if ( !player.characters ) then return; end;
    	if ( !player.characters[character] ) then return; end;
    	
    	local charlist = glon.encode(player.characters[character]);
    	
    	umsg.Start( "ClientRequestCharacterListLocalCopy", player )
    		umsg.String( character );
    		umsg.String( charlist );
    	umsg.End();
    end;
    
    -- delete a local copy of a character table.
    function  TreeSauce.characters.DeleteCharacterLocalCopy( player, character )
    	if ( !player or !character ) then return; end;
    	
    	umsg.Start( "DeleteCharacterLocalCopy", player )
    		umsg.String(character);
    	umsg.End();
    end;
    
    -- so the client can request local copies by themselves
    concommand.Add( "TreeSauce_RequestCharacterListLocalCopy", function( player, command, arguments )
    	if ( !arguments[1] ) then return; end;
    	
    	TreeSauce.characters.ClientRequestLocalCopy( player, arguments[1] );
    end);
    
    end;
    
    if ( CLIENT ) then
    	
    	TreeSauce.characters.LocalData = {};
    	
    	usermessage.Hook( "DeleteCharacterLocalCopy", function( bfr )
    		local character = bfr:ReadString();
    		
    		if ( !TreeSauce.characters.LocalData[character] ) then return; end;
    		
    		TreeSauce.characters.LocalData[character] = nil;
    	end);
    	
    	usermessage.Hook( "ClientRequestCharacterListLocalCopy", function( bfr )
    	
    		local key = bfr:ReadString();
    		local copy = bfr:ReadString();
    		
    		TreeSauce.characters.LocalData[key] = glon.decode(copy);
    	end);
    end;
    
    
    Reply With Quote Edit / Delete Reply Windows 7 United States Show Events Informative Informative x 1 (list)

  3. Post #3
    DarkShadow6's Avatar
    September 2009
    534 Posts
    This is a really cool thread, I think anyway.
    Kthx.
    I don't want to post an example because I feel it won't be exact to my style.
    I feel that way a little, but my style is pretty consistent. I feel confident you could tell it was me that programmed something because I always use proper grammar when I name my variables, proper casing (though I camelCase in Java) and my own methods of tabbing/newlines/etc.
    Soo I'm just going to post this which I scripted yesterday, I notice that my style is really inconsistent but anyway what do you think? It's not like how you do it, keeping lua all to lua, i make use of pretty much every c++ operator garry has added instead of the lua variants. Just habbit really.
    To be honest?
    I really just plain hate your style. No offence or anything, to each his own. But really.
    1. The semicolons aren't required at all. Actually, you can use newlines, spaces, semicolons, commas, etc to separate instructions in Lua. In some cases they aren't even needed, ex.
    print("Hello, world!")print("No spaces or anything needed, since the last character was a parenthesis;\
    no possible instruction could be added to that parenthesis, so it also functions as a delimiter.")
    ...though I think that looks ugly and the only time I use such things are when I'm trying to save space.
    2. You haphazardly add space padding to your parenthetical statements. I understand this makes code easier to read to some, but to me it makes it harder. Your eyes have to move farther to read the same amount of information. I don't like it at all.
    3. So many newlines! I only make newlines to end statements and separate chunks of code, such as functions. Only in the global scope, though.
    4. Comments. I don't use comments. Ever. Unless I'm trying to make a tutorial and add in little notes to assist the person reading it. If I can't remember what I was working on enough to warrant a comment, I probably shouldn't be doing it.
    5. You place parenthesis where they aren't really needed, like in your if statements. I understand how this can make the code easier to read, and it's required by a lot of languages (ex. Java) but I find it to be ugly when programming in Lua.

    However...

    1. You use a global table to store your data. When dealing with a lot of stuff that's a very good practice to have. It can impact speed slightly, having to constantly access a table, but it keeps things organized and you don't clutter the global scope with unnecessary functions that could easily be overwritten by another dumb programmer.
    2. Your code seems generally good besides the fact that you could save space by compacting some of those if statments, ex.
        if ( !player or !character ) then return; end;
        if ( !TreeSauce.characters.GetPlayerCharacters(player) ) then return; end;
    
    ...becomes...
        if ( !player or !character or !TreeSauce.characters.GetPlayerCharacters(player) ) then return; end;
    
    ...proven in my Lua prompt...
    Code:
    Lua 5.1.4  Copyright (C) 1994-2008 Lua.org, PUC-Rio
    > test1 = "test"
    > test2 = {test = "test"}
    > print(test1, test2, test2.test)
    test    table: 00276318 test
    > 
    > test2 = nil
    > 
    > if not test1 or not test2 then print("Not 1!") end
    Not 1!
    > 
    > if test2.test:upper() ~= "TEST" then print("Not 2!") end
    stdin:1: attempt to index global 'test2' (a nil value)
    stack traceback:
            stdin:1: in main chunk
            [C]: ?
    >
    > 
    > if not test1 or not test2 or test2.test:upper() ~= "TEST" then print("Not 3!") end
    Not 3!
    >
    I bet you're thinking that last one should have errored, am I right? The thing about Lua's if statements is that they are linear; if any of the statements in the if statement are false, it will not even compute any of the remaining pieces of code, because even if they were true, one of the required statements was false.
    You could also possibly try using conditional statements, instead of all the return ends (though they can't really be used effectively with functions). Here is an example of them:
    Code:
    Lua 5.1.4  Copyright (C) 1994-2008 Lua.org, PUC-Rio
    > test1 = 10
    > test2 = 30
    > 
    > test3 = test1 == 10 and test2 or test1
    > print(test3)
    30
    > 
    > test1, test2, test3 = nil, 20, nil
    > 
    > test3 = test1 or test2
    > print(test3)
    20
    >
    Edited:

    Whoa I just realized this is really tl;dr.
    Reply With Quote Edit / Delete Reply Windows 7 United States Show Events Dumb Dumb x 7Agree Agree x 1 (list)

  4. Post #4
    Facepunch's #1 Huskybutt
    James xX's Avatar
    July 2011
    2,090 Posts
    Code:
    function ThisIsAFunction()
        print("Tabs!")
        local Blah = function(fawnk)
            fawnk()
        end
        Blah(function()
            print("Hey, sup")
        )
    end
    You missed the end to close the function when you call Blah. Programming with style is cool, but the syntax has to be correct, also.
    Reply With Quote Edit / Delete Reply Windows XP France Show Events Agree Agree x 4 (list)

  5. Post #5
    DarkShadow6's Avatar
    September 2009
    534 Posts
    You missed the end to close the function when you call Blah. Programming with style is cool, but the syntax has to be correct, also.
    Herp derp. I wrote that in the response itself, just as means of example. Thanks for pointing that out, anyway.

    Edited:

    For some reason, though, I felt something was wrong when I typed that. I didn't really care at the time or something, I guess.
    Reply With Quote Edit / Delete Reply Windows 7 United States Show Events Friendly Friendly x 1 (list)

  6. Post #6
    PROUD BRONY 4LYFE
    Drakehawke's Avatar
    February 2009
    3,312 Posts
    I tabulate everything too, it does make everything so much easier to read.

    I also space my code out a lot, blank likes between anything that isn't directly related, and spaces between all brackets (both (), [] and {})

    eg
    foo( 1, "hi", t[ 12 ], { "a", "b", "c" } )
    

    Also, the previous point about combining the two return statements:
    if ( !player or !character or !TreeSauce.characters.GetPlayerCharacters(player) ) then return; end;

    Personally, I would do that, UNLESS it made the line too long to fit in my editor, if it did then I'd split it up.

    I also avoid using garrys C++ additions and I don't use semicolons in Lua.


    I do the same as you in C++ too, with brackets on new lines.
    Reply With Quote Edit / Delete Reply Windows Vista United Kingdom Show Events Agree Agree x 6Disagree Disagree x 1 (list)

  7. Post #7
    DarkShadow6's Avatar
    September 2009
    534 Posts
    I tabulate everything too, it does make everything so much easier to read.
    I don't understand why some people don't. I can't read code worth anything without them.
    I also space my code out a lot, blank likes between anything that isn't directly related, and spaces between all brackets (both (), [] and {})
    I don't use it because it mucks up my ability to read it. Probably because I'm just used to it.
    Also, the previous point about combining the two return statements:
    Personally, I would do that, UNLESS it made the line too long to fit in my editor, if it did then I'd split it up.
    I lol'd at that. Silly reason but valid nonetheless.
    It doesn't make as much of a difference performance-wise, but I try to cut all the corners I can, style be damned.
    I also avoid using garrys C++ additions and I don't use semicolons in Lua.
    I don't even know why he added them, for his own personal use? Why use non-Lua syntax in Lua?
    I do the same as you in C++ too, with brackets on new lines.
    I dunno why I do it, either, it takes up space but looks pretty to me.

    Edited:

    Lua tag <3
    Reply With Quote Edit / Delete Reply Windows 7 United States Show Events Dumb Dumb x 1 (list)

  8. Post #8
    Facepunch's #1 Huskybutt
    James xX's Avatar
    July 2011
    2,090 Posts
    Something I don't get is why people do this :

    if ( x > 1 ) then return x else return DEFAULT_VALUE end
    

    instead of

    
    return ( x > 1 and x or DEFAULT_VALUE )
    
    
    Reply With Quote Edit / Delete Reply Windows XP France Show Events Agree Agree x 3Disagree Disagree x 1 (list)

  9. Post #9
    DarkShadow6's Avatar
    September 2009
    534 Posts
    if ( x > 1 ) the return x else return DEFAULT_VALUE end
    
    if ( x > 1 ) the
    
    Ohohoho.

    But, I agree. It's really quite silly. I know I certainly could use it more... Except many of the times I could use it would end up making things way too overcomplicated and memory-consuming. Sometimes it's just simpler to use ifs.

  10. Post #10
    Facepunch's #1 Huskybutt
    James xX's Avatar
    July 2011
    2,090 Posts
    Ohohoho.

    But, I agree. It's really quite silly. I know I certainly could use it more... Except many of the times I could use it would end up making things way too overcomplicated and memory-consuming. Sometimes it's just simpler to use ifs.
    I'm sure this is going to turn into a "Correct the lua of the person above you" thread ^^

  11. Post #11
    DarkShadow6's Avatar
    September 2009
    534 Posts
    I corrected your Lua.
    Hurrrrrr.

    Edited:

    It's amazing the difference in style you people have, though. I follow the norm with only slight variances that make my code what it is.
    Reply With Quote Edit / Delete Reply Windows 7 United States Show Events Dumb Dumb x 4 (list)

  12. Post #12
    Facepunch's #1 Huskybutt
    James xX's Avatar
    July 2011
    2,090 Posts
    I corrected your Lua.
    Hurrrrrr.
    It's only in German where you need to capitalize the first letter of a noun, even if it's a regular noun.

    It's amazing the difference in style you people have, though. I follow the norm with only slight variances that make my code what it is.
    I agree. I, personally like to put everything in tables.

  13. Post #13
    PROUD BRONY 4LYFE
    Drakehawke's Avatar
    February 2009
    3,312 Posts
    I lol'd at that. Silly reason but valid nonetheless.
    It doesn't make as much of a difference performance-wise, but I try to cut all the corners I can, style be damned.
    It's easy to scroll up-down (mouse wheel) but more tedious to scroll left-right (have to move mouse to bottom of screen, get little draggy thing, move it across etc). When I'm scanning through my code to make sure I haven't done anything stupid or missed a capital letter or something, it's easier, I can read a whole page without having to fiddle around moving it.

    I don't even know why he added them, for his own personal use? Why use non-Lua syntax in Lua?
    I think it's because when he'd been sat there coding C++ for ages, and suddenly needed to do something in Lua, if he accidentally used the C++ syntax he was used to, it wouldn't matter.
    Reply With Quote Edit / Delete Reply Windows Vista United Kingdom Show Events Agree Agree x 2 (list)

  14. Post #14
    im gay
    _Kilburn's Avatar
    July 2007
    3,587 Posts
    function ThisIsAFunction()
        print("Tabs!")
        local Blah = function(fawnk)
            fawnk()
        end
        Blah(function()
            print("Hey, sup")
        end)
    end
    

    Code:
    public class Brackets
    {
        public static int main(String Args[])
        {
            System.out.println("Code!");
            ...
            return 0;
        }
    }
    I like your programming style, it looks exactly like mine. I can't stand people who use C syntax in Lua, like unnecessary parentheses in if/while structures, useless semicolons, and C logical operators and comments. I also hate whitespace abuse, where expressions look like test1 = ( test2 + ( test3 * func ( test4 , test5 ) ) - test6 ). That's bloody unreadable.

    There's just a major flaw in my coding, I always forget to use a global table to store my variables. I know that's what I should do but I just keep forgetting it and just declare ugly global variables everywhere.

    I think it's because when he'd been sat there coding C++ for ages, and suddenly needed to do something in Lua, if he accidentally used the C++ syntax he was used to, it wouldn't matter.
    I don't think that's a valid excuse, I've been coding C++ for about years before learning Lua. A programmer needs to adapt to the language they're using, not the opposite.
    Reply With Quote Edit / Delete Reply Windows 7 France Show Events Agree Agree x 1 (list)

  15. Post #15

    May 2010
    1,092 Posts
    I like your programming style, it looks exactly like mine. I can't stand people who use C syntax in Lua, like unnecessary parentheses in if/while structures, useless semicolons, and C logical operators and comments. I also hate whitespace abuse, where expressions look like test1 = ( test2 + ( test3 * func ( test4 , test5 ) ) - test6 ). That's bloody unreadable.

    There's just a major flaw in my coding, I always forget to use a global table to store my variables. I know that's what I should do but I just keep forgetting it and just declare ugly global variables everywhere.



    I don't think that's a valid excuse, I've been coding C++ for about years before learning Lua. A programmer needs to adapt to the language they're using, not the opposite.
    Can't agree more, "it's a habit" really isn't much of an excuse.
    ...although I admittedly do use the C style comments purely because they're faster to type and mess with.

  16. Post #16
    whitespace's Avatar
    November 2008
    869 Posts
    I have a pretty inconsistent style, but I think it's mostly fine. I like to use the Lua comments and operators as well.
    function GM:LoadMapSetup( )
    	
    	local values = {}
    	local s = file.Read("surf/"..game.GetMap()..".txt")
    	local lines = string.Explode("\n", s)
    	local curkey = ""
    	
    	for linenum, line in pairs( lines ) do
    
    		if( line:sub(-1,-1) == ":" ) then
    			
    			curkey = line:sub(1, -2)
    			values[curkey] = {}
    			
    		else
    		
    			line = string.Explode("=", line)
    			local key, value = line[1]:gsub(" ", ""), line[2]
    			if( value and value:sub(1,1) == " " ) then
    				value = value:sub(2)
    			end
    			
    			if( value and value:find( "," ) ) then
    				-- it's an array of things
    				value = value:gsub("[{}%s]", "")
    				value = string.Explode(",",value)
    				
    			end
    			
    			if( value ) then
    			
    				value = tonumber(value) or value
    				values[curkey][key] = values[curkey][key] or {}
    				local n = #values[curkey][key]
    				values[curkey][key][n+1] = value
    				
    			end
    			
    		end
    		
    	end
    	
    	return values
    	
    end
    Reply With Quote Edit / Delete Reply Windows XP Finland Show Events Winner Winner x 1Disagree Disagree x 1 (list)

  17. Post #17
    Gold Banana
    Banana Lord.'s Avatar
    May 2010
    6,423 Posts
    as I messed with GLua more my style changed a lot, now it's fairly consistent

    local tblSpawns = {
        Vector( 0, 0, 0 ),
        Vector( 30, 100, 0 ),
    }
    hook.Add( "InitPostEntity", "gm_SpawnAmmo", function( )
        if( game.GetMap( ) != "rp_downtown_v2pgs" ) then return; end
        for i=1,#tblSpawns do
            local v = tblSpawns[i]; -- notice how there's no spaces around the i
            local objEnt = ents.Create( "ammo_crate" );
            objEnt:SetPos( v );
            objEnt:SetAngles( Angle( 0, 90, 0 );
            objEnt:Spawn( );
        end
    end );

    Just something I wrote now, but that's pretty much my style. I'll find something better and post it later.

  18. Post #18
    Gold Member
    Splambob's Avatar
    January 2005
    1,216 Posts
    The more I use Lua, the less my code looks like Java which is my other strong language. I think it's pretty cool because I know I'm writing in a completely different language and my coding style is changing to take advantage of what Lua has to offer. It sucks though because when I go back to coding in Java I start getting annoyed because it simply isn't as flexible as Lua (give me back my first class functions >:( ), but it is a lot easier to write because that language is built to hold your hand. With that said, I'm finding it hard to let go of the C-style operators because they feel nicer to use.

    Anyway my latest thing has been to start abusing tables because they're the most useful data structures I've ever come across. Assign anything to ANYTHING? I can pretend that it's an OBJECT?! AND I get to use it like an array too?! I'LL TAKE 20.

    I also like abusing tabs; lining up all of the assignments makes things look neater to me. I can just look down a line of assignment operators to get to the one I want instead of looking back and forth.

    /*
    	This file defines functions for loading from and saving to Holopad project files.
    		
    	Made with Bub!
    */
    
    require "glon"
    include("modelviewer/obj_cl_PRJObject.lua")
    
    HoloProjectFile = {}
    local this 		= HoloProjectFile
    
    function this:Save( rendercoll, filename )
    
    	local holos, clips 	= this:RCollToTables(rendercoll)
    	local savefile 		= file.Open("holopad/"..filename..".txt", "w", "DATA")
    
    	if !savefile then return false end
    	
    	status = pcall( function()
    						savefile:Write( "HOLOPAD PRJ "..PRJObject.version.."\n" )
    						savefile:Write( glon.encode(holos) )
    						savefile:Write( "\nBEGIN CLIPS\n" )
    						savefile:Write( glon.encode(clips) )
    					end )
    					
    	if !status then
    		Error("Unable to save the Holopad project; error writing a holo to file!")
    		return false
    	end
    	
    	savefile:Close()
    
    	return true
    	
    end
    
    
    
    local funcs = {
    	scale	 =	function(self, arg, uids) 	return self:SetScale(arg) end,
    	colour	 =	function(self, arg, uids)	return self:SetColor(arg) end,
    	name	 =	function(self, arg, uids)	return self:SetName(arg) end,
    	model	 =	function(self, arg, uids)	return self:SetModel(arg) end,
    	ang		 =	function(self, arg, uids)	return self:SetAngles(arg) end,
    	material =	function(self, arg, uids)	return self:SetMaterial(arg) end,
    	parent	 =	function(self, arg, uids)	self.parent = uids[arg] return self:SetParent(uids[arg]) end,
    	pos		 =	function(self, arg, uids)	return self:SetPos(arg) end,
    	uid		 =	function(self, arg, uids)	return true end,	// dummy function
    	object	 =	function(self, arg, uids)	return true end,	// dummy function
    	version	 =	function(self, arg, uids)	return true end,	// dummy function
    	type	 =	function(null, arg, uids)	if 		arg == "ClipPlane" 		then return ClipPlane:New(nil)
    											elseif 	arg == "ModelContainer" then return ModelContainer:New(nil) 
    											else 	return nil end end
    }
    
    
    
    local function addToRColl(rcoll, mdls, uids)
    
    	// initialize new objects, populate uid table
    	// this occurs first to allow easier handling of inter-object relationships
    	for k, prj in pairs(mdls) do
    		if prj.uid == nil then ErrorNoHalt("Nil uid for passed PRJObject.  Abandoning load attempt.") return false end
    		
    		prj.object 			= funcs.type(nil, prj.type, uids)
    		prj.object.parent	= rcoll		//TODO: eliminate
    		uids[prj.uid]		= prj.object
    	end
    	
    	
    	local obj, success
    	// for each prjobject, get defined keyvalues and attempt to config the associated attribute in the newly created object
    	for _, prj in pairs(mdls) do
    		obj = prj.object
    
    		for k, v in pairs(prj) do
    			if k != "type" then
    				success = pcall(funcs[k], obj, v, uids)
    				if !success then ErrorNoHalt("Invalid PRJObject keyvalue "..tostring(k).."\t"..tostring(v).." for version "..PRJObject.Version.." loader.  Abandoning load attempt.") return false end
    			end
    		end
    	end
    	
    	for _, prj in pairs(mdls) do
    		rcoll:Add(prj.object)
    		prj.object = nil
    	end
    	
    	return true
    
    end
    
    Reply With Quote Edit / Delete Reply Windows 7 Show Events Agree Agree x 2Artistic Artistic x 2 (list)

  19. Post #19
    Facepunch's #1 Huskybutt
    James xX's Avatar
    July 2011
    2,090 Posts
    Something I've noticed is, it's alot easier to use pseudo-metatables than real metatables. I can post an example of my Hexadecimal library as an example :

    
    
    -- This script is under this license : [url]https://sites.google.com/site/jamesaddons/home/terms-of-Service[/url]
    
    // Stuff
    
    local floor,insert = math.floor, table.insert
    local function basen(n,b)
    
    	n = floor(n)
    	if not b or b == 10 then return tostring(n) end
    	
    	local digits = "0123456789ABCDEF"
    	local t = {}
    	
    	repeat
    		local d = (n % b) + 1
    		n = floor(n / b)
    		insert(t, 1, digits:sub(d,d))
    	until n == 0
    	
    	return table.concat(t,"")
    	
    end
    
    
    // The hex
    
    local HexTable = {
    
    	Data = "",
    	
    	SetData = function( self , NewData ) self.Data = NewData end,
    	Len = function( self ) return #self.Data end,
    	CanGet = function( self, Length ) return ( self:Len( ) > Length ) end,
    	GetData = function( self ) return self.Data end,
    	WriteRaw = function( self , Value ) self.Data = Value .. self.Data end,
    	
    	GetPacket = function( self )
    		local String = "";
    		while( self:CanGet( 2 ) )do
    			String = String .. string.char( self:GetInt( 2 ) )
    		end
    		return String
    	end,
    	
    	GetPacketSize = function( self )
    		return self:Len( ) / 2
    	end,
    	
    	// Integers
    	
    	GetInt = function( self , Length )
    		if ( !self:CanGet( Length ) ) then return false end
    		local String = string.sub( self:GetData( ) , 0 , Length );
    		self.Data = string.gsub( self.Data , String , "" );
    		return tonumber( String, 16 )
    	end,
    	
    	WriteInt = function( self, Int , Length )
    		local i = basen( Int , 16 )
    		local ToAdd = Length - #i;
    		if ( ToAdd > 0 ) then i = string.rep( "0" , ToAdd ) .. i end
    		self:WriteRaw( i );
    	end,
    	
    	// Stream of Hexadecimal chars
    	
    	GetStream = function( self, Length )
    		if ( !self:CanGet( Length ) ) then return false end
    		local String = string.sub( self:GetData( ) , 0 , Length );
    		self.Data = string.gsub( self.Data , String , "" );
    		return String
    	end,
    	
    	WriteStream = function( self , Table )
    		for i=#Table,1,-1 do
    			self:WriteRaw( Table[i] , 2 )
    		end
    		return ( #Table * 2 )
    	end,
    
    	
    	// Stream with no length
    	
    	GetStreamNoLen = function( self, Termination )
    		Termination = Termination or 0x00;
    		local Save = ""
    		while( self:CanGet( 2 ) ) do
    			local MyInt = self:GetInt( 2 );
    			if ( MyInt != Termination ) then
    				break
    			else
    				Save = Save .. MyInt
    			end
    		end
    		return  Save
    	end,
    	
    	WriteStreamNoLen = function( self, Table, Termination )
    		Termination = Termination or 0x00;
    		local Length = self:WriteStream( Table )
    		self:WriteInt( Termination, 2 );
    		return ( Length + 2 )
    	end,
    	
    	// String
    	
    	GetString = function( self, Termination )
    		Termination = Termination or 0x00;
    		local Save = ""
    		while( self:CanGet( 2 ) ) do
    			local Int = self:GetInt( 2 )
    			if ( Int == Termination ) then break end
    			Save = Save .. string.char( Int )
    		end
    		return Save
    	end,
    	
    	WriteString = function( self, String, Termination )
    		Termination = Termination or 0;
    		self:WriteInt( Termination, 0, 2 )
    		for k,v in pairs( string.ToTable( string.reverse( String ) ) ) do
    			self:WriteInt( string.byte( v ) , 2 );
    		end
    		return self
    	end
    }
    
    
    function Hex( Data )
    
    	local t = table.Copy( HexTable );
    	t.Data = Data or ""
    	
    	return t
    	
    end
    

    I put all my functions in a local table, and when I need to use the functions, it returns a copy of that table.

    Just saying.
    Reply With Quote Edit / Delete Reply Windows XP France Show Events Dumb Dumb x 3 (list)

  20. Post #20
    whitespace's Avatar
    November 2008
    869 Posts
    That uses way more resources than real metatables, but sure that works too. I find myself using those too when I want to code something quick, but in the long run real metatables are better.
    Reply With Quote Edit / Delete Reply Windows XP Finland Show Events Agree Agree x 1 (list)

  21. Post #21
    im gay
    _Kilburn's Avatar
    July 2007
    3,587 Posts
    Something I've noticed is, it's alot easier to use pseudo-metatables than real metatables. I can post an example of my Hexadecimal library as an example :
    With "pseudo-metatables":

    function Hex( Data )
     
        local t = table.Copy( HexTable );
        t.Data = Data or ""
         
        return t
         
    end

    With metatables:

    function Hex( Data )
    	return setmetatable({Data=Data}, {__index=HexTable})
    end

    Unless I forgot a detail somewhere, there's no change required other than in the Hex function.

  22. Post #22

    May 2012
    5 Posts
    I hate it when people don't leave comments explaining what a function does. Seriously, its so basic that everyone should know to do it but it seems like some people don't.

  23. Post #23

    April 2012
    64 Posts
    I hate it when people don't leave comments explaining what a function does. Seriously, its so basic that everyone should know to do it but it seems like some people don't.
    You'd want to shoot me right between the eyes then
    Reply With Quote Edit / Delete Reply Windows 7 United States Show Events Agree Agree x 3 (list)

  24. Post #24
    Facepunch's #1 Huskybutt
    James xX's Avatar
    July 2011
    2,090 Posts
    With "pseudo-metatables":

    function Hex( Data )
     
        local t = table.Copy( HexTable );
        t.Data = Data or ""
         
        return t
         
    end

    With metatables:

    function Hex( Data )
    	return setmetatable({Data=Data}, {__index=HexTable})
    end

    Unless I forgot a detail somewhere, there's no change required other than in the Hex function.
    Sweet. I always built them differently, thanks ^^ I learn something new every day.

    And just on a side note for divran, Thank you so much for your box, I was needing one of those for my parallel universe experiment ( I need somewhere to store them all :O ). Explain why you rate someone dumb. Otherwise it's cowardly, by hiding behind the system. If I did something wrong, make me a better coder by telling me what. If it' something I said, make me a better person and tell me what.
    Reply With Quote Edit / Delete Reply Windows XP France Show Events Dumb Dumb x 6Agree Agree x 2Late Late x 1 (list)

  25. Post #25
    whitespace's Avatar
    November 2008
    869 Posts
    Sorry but it's plain dumb to complain about ratings, so have a box!

    Edited:

    I hate it when people don't leave comments explaining what a function does. Seriously, its so basic that everyone should know to do it but it seems like some people don't.
    You can also name your functions so that by looking at the name you get the general idea what it does. Some of the more cluttered stuff inside the function can then be commented to explain what that part does.
    Reply With Quote Edit / Delete Reply Windows XP Finland Show Events Late Late x 1Dumb Dumb x 1 (list)

  26. Post #26
    Gold Member
    Divran's Avatar
    April 2008
    2,622 Posts
    Explain why you rate someone dumb. Otherwise it's cowardly, by hiding behind the system. If I did something wrong, make me a better coder by telling me what. If it' something I said, make me a better person and tell me what.
    whitespace and killburn already did.

    EDIT: But I guess you can't know that was the reason I rated. *shrug*

  27. Post #27
    PROUD BRONY 4LYFE
    Drakehawke's Avatar
    February 2009
    3,312 Posts
    I hate it when people don't leave comments explaining what a function does. Seriously, its so basic that everyone should know to do it but it seems like some people don't.
    I try and use descriptive function names, if I can't name a function in a way that describes what it does and what the arguments are (within a reasonable length of course), then I add a comment.

    Same with most code actually, it would be horrible if every line was commented explaining what it does, I generally comment with an explanation any code that is abnormal, or where it's purpose is unclear without thorough reading (hacky workarounds or stuff like that).

    Edited:

    Speaking of comments, I hate it when people decorate their comments with giant boxes of "#"'s or "-"'s and stuff like that. It's just unnecessary, a simple "--" is enough.
    Reply With Quote Edit / Delete Reply Windows Vista United Kingdom Show Events Agree Agree x 1 (list)

  28. Post #28
    johnny guitar was here
    comet1337's Avatar
    February 2010
    6,075 Posts

    Speaking of comments, I hate it when people decorate their comments with giant boxes of "#"'s or "-"'s and stuff like that. It's just unnecessary, a simple "--" is enough.
    comments? what are those?

    all of my codes are so damn messy that not even the pros can make shit out of them.
    i sometimes have a hard time reading my own script i made an hour ago
    Reply With Quote Edit / Delete Reply Windows XP Finland Show Events Funny Funny x 1 (list)

  29. Post #29

    May 2012
    5 Posts
    comments? what are those?

    all of my codes are so damn messy that not even the pros can make shit out of them.
    i sometimes have a hard time reading my own script i made an hour ago
    Thats why you put comments dude. Even with names for functions you should still describe what it does, which parts make it work and the end result.

    MIT teaches it like this:
    Code:
    ;; Contract: area-of-ring : number number  ->  number
    
    ;; Purpose: to compute the area of a ring whose radius is
    ;; outer and whose hole has a radius of inner
    
    ;; Example: (area-of-ring 5 3) should produce 50.24
    
    ;; Definition: [refines the header]
    (define (area-of-ring outer inner)
      (- (area-of-disk outer)
         (area-of-disk inner)))
      
    ;; Tests:
    (area-of-ring 5 3) 
    ;; expected value
    50.24
    Of course that is written in scheme, not lua, but you get the point.

  30. Post #30
    im gay
    _Kilburn's Avatar
    July 2007
    3,587 Posts
    Speaking of comments, I hate it when people decorate their comments with giant boxes of "#"'s or "-"'s and stuff like that. It's just unnecessary, a simple "--" is enough.
    I use that to distinguish important comments from simple comments. Sometimes when your code is getting really huge, you just need a way to separate it into several parts, and making huge eye catching comments is one of the ways to easily find your way in the code.
    Reply With Quote Edit / Delete Reply Windows 7 France Show Events Agree Agree x 5 (list)

  31. Post #31

    May 2010
    1,092 Posts
    I never use comments for commenting, only for debugging when I need to stop a line of code from executing but don't want to delete it as I don't know if this is the line causing the issue or not.

    i'm a naughty scripter :(

  32. Post #32
    Better place for this thread: http://facepunch.com/forums/240
    Reply With Quote Edit / Delete Reply Windows 8 Russian Federation Show Events Disagree Disagree x 2 (list)

  33. Post #33
    Gold Member
    Splambob's Avatar
    January 2005
    1,216 Posts
    But this thread is about lua, and this is the lua forum?

    In large part anyway. The OP does have multiple languages but most people seem to be hitting up the lua snippets.

  34. Post #34
    PROUD BRONY 4LYFE
    Drakehawke's Avatar
    February 2009
    3,312 Posts
    In large part anyway. The OP does have multiple languages but most people seem to be hitting up the lua snippets.
    Probably because it's in the Lua forum.

    Edited:

    I use that to distinguish important comments from simple comments. Sometimes when your code is getting really huge, you just need a way to separate it into several parts, and making huge eye catching comments is one of the ways to easily find your way in the code.
    I'd just use ctrl + f personally.
    Reply With Quote Edit / Delete Reply Windows Vista United Kingdom Show Events Informative Informative x 1 (list)

  35. Post #35
    Get your own DarkRP Server!
    FPtje's Avatar
    January 2006
    5,539 Posts
    My Lua scripting style has changed over the years.
    Some things I'd like to note is that I've seen the virtues of preventing sidescrolling lines, although I still make some mistakes because I have word wrapping on :).

    Someone once told me that the Lua operators (~=, not, etc.) were ugly and that I should use Garry's C-style syntax implementation instead. The only C-style thing I really use in Lua are the /* */ comments because -[[]] is a bitch to type.

    Oh and I use // (and /**/) to comment code out, but I use -- to provide line to line explanations. For function headers and sections I've got a snippet in sublime: / * <tab> produces this:
    /*---------------------------------------------------------------------------
    Title
    ---------------------------------------------------------------------------*/
    With the "Title" text selected. This is useful because it gives you a nice block comment which is consistent with all the other block comments. (in the amount of dashes )


    Edit:
    I just remembered I maintain the GLua highlighter for sublime. The comment snippet is in it

  36. Post #36
    Map in a box's Avatar
    July 2009
    7,004 Posts
    Java itself has a coding style you should follow
    http://www.oracle.com/technetwork/ja...855.html#11684

  37. Post #37
    DarkShadow6's Avatar
    September 2009
    534 Posts
    I like your programming style, it looks exactly like mine.
    Orly. How odd.

    There's just a major flaw in my coding, I always forget to use a global table to store my variables. I know that's what I should do but I just keep forgetting it and just declare ugly global variables everywhere.
    It happens to the best of us. When I started programming Lua, it was on that now-shitty game (not saying it wasn't shitty before, because it kinda was) ROBLOX. I was so terrible at Lua for the first year or so I used it. Then I became the god of all things ROBLOX.
    ...
    Then the devs went and broke the game even more. Then I ragequit. I'll spare you the tl;dr story behind that.

    I have a pretty inconsistent style, but I think it's mostly fine. I like to use the Lua comments and operators as well.
    as I messed with GLua more my style changed a lot, now it's fairly consistent
    More examples of code I do not like. It may be consistent, but it just looks all floppy and ugly to me because of the whitespace and parenthesis spam.

    The more I use Lua, the less my code looks like Java which is my other strong language.
    Java is my other language too (I mean, I can use a lot of languages if I need to, but I'm best at Lua and Java).
    It sucks though because when I go back to coding in Java I start getting annoyed because it simply isn't as flexible as Lua
    Java was made to be extendable and powerful but still simple to use. Sadly, because of this, and because of the fact that it also has a lot of pre-built security measures to keep your programs safe from attack, it is missing a lot of possibly useful features. Like metatables. If Java ever got anything similar to metatables for classes, __add, __sub, __div, __mul, __index, etc, I would totally throw away Lua.
    With that said, I'm finding it hard to let go of the C-style operators because they feel nicer to use.
    And that's your opinion. If that's what you like to do, go for it. I don't really like them, though. I want my Lua to be Lua :U

    Anyway my latest thing has been to start abusing tables because they're the most useful data structures I've ever come across. Assign anything to ANYTHING? I can pretend that it's an OBJECT?! AND I get to use it like an array too?! I'LL TAKE 20.
    When I found out about tables... Oh, I had the most wonderful time finding out what they did.

    I put all my functions in a local table, and when I need to use the functions, it returns a copy of that table.
    Well, yeah, you can do that in some instances and have it work totally fine. There are places where this is a better practice.
    Then again, you could always just make those functions into a module, or put them into a global table instead of returning a new local instance of the table every time. That's kind of a memory eater there.

    I hate it when people don't leave comments explaining what a function does. Seriously, its so basic that everyone should know to do it but it seems like some people don't.
    Why should we? I'm programming so things will work, not so nobos can come along and try to learn from my code or something.
    And if people want to extend my code, they could always just ask me. :\

    Explain why you rate someone dumb.
    For some reason there's boxes flying around this place like flies. I find it funny how people that rated the OP dumb still posted here.

    You can also name your functions so that by looking at the name you get the general idea what it does. Some of the more cluttered stuff inside the function can then be commented to explain what that part does.
    I always do that.

    I never use comments for commenting, only for debugging when I need to stop a line of code from executing but don't want to delete it as I don't know if this is the line causing the issue or not.
    Agree'd.

    But this thread is about lua, and this is the lua forum?
    I put this here because I wanted to see your people's Lua styles.

    This.

    -snip-
    Interesting. You are very thorough in your ways of the Luas.
    I've seen your code; you are very consistent and your code is relatively clean, though you use some things I don't like, of course :u

    Java itself has a coding style you should follow
    http://www.oracle.com/technetwork/ja...855.html#11684
    Implying conventions are law. I still use my own-ish style.

    Edited:

    tl;dr
    Reply With Quote Edit / Delete Reply Windows 7 United States Show Events Dumb Dumb x 2 (list)

  38. Post #38
    dylanb5123's Avatar
    January 2011
    218 Posts
    
    hook.Add("CalcView", "TopDownView", function(ply, pos, angles, fov)
        local view = {}
        view.origin = pos+Vector(0,0,900)
        view.angles = Vector(90,0,0)
        view.fov = fov
        return view
    end)
    
    hook.Add("ShouldDrawLocalPlayer", "DrawLocalPlayer", function()
        return true
    end)
    
    hook.Add("Think", "ClampViewAngles", function()
        local ang = LocalPlayer():GetAimVector():Angle()
        LocalPlayer():SetEyeAngles(Angle(0, ang.Yaw, ang.Roll))
    end)
    
    hook.Add("HUDPaint", "TPSDisplay", function()
        if !LocalPlayer():Alive() then return end
        local AIM_COLOR = Color(255,255,255,255)
        surface.SetDrawColor(AIM_COLOR)
        local ang = LocalPlayer():GetAimVector()
        local startpos = (LocalPlayer():GetShootPos()+(ang*50)):ToScreen()
        local endpos = (LocalPlayer():GetShootPos()+(ang*250)):ToScreen()
        local circlepos = (LocalPlayer():GetShootPos()+(ang*257)):ToScreen()
        local reticlepos = LocalPlayer():GetEyeTrace().HitPos:ToScreen()
        surface.DrawLine(startpos.x, startpos.y, endpos.x, endpos.y)
        surface.DrawCircle(circlepos.x, circlepos.y, 7, AIM_COLOR)
        surface.DrawCircle(circlepos.x, circlepos.y, 1, AIM_COLOR)
        surface.DrawCircle(reticlepos.x, reticlepos.y, 7, AIM_COLOR) 
    end)
    

    Theres my most recent script... Hope it reflects my style of coding. I'm not a fan of comments usually. Hope this thread is still alive.

  39. Post #39
    Get your own DarkRP Server!
    FPtje's Avatar
    January 2006
    5,539 Posts
    Code
    

    Theres my most recent script... Hope it reflects my style of coding. I'm not a fan of comments usually. Hope this thread is still alive.
    I tend to put enters to separate variable declarations and programming logic, but I'm fine with reading code that doesn't have that :)

  40. Post #40
    dylanb5123's Avatar
    January 2011
    218 Posts
    I tend to put enters to separate variable declarations and programming logic, but I'm fine with reading code that doesn't have that :)
    I'm a big fan of really compact scripts