Page 1 of 2

Lua Style

Posted: Sun Apr 05, 2015 6:00 pm
by Bindie
Hey! I guess this is a personal question, lately I have read about lua styles, CamelCase and such.

This is my way:

Code: Select all

-- Some code

local FirstVariable = 5
local SecondVariable = 5

for x == 0, FirstVariable do

     for y == 0, SecondVariable do

          if x == 3 and y == 2 then

               AddTile(x,y)

          end 
    
     end

end
Curious, how do you prefer indents and cases? Tidy is nice.

Re: Lua Style

Posted: Sun Apr 05, 2015 7:05 pm
by Roland_Yonaba
I like to follow those Lua Style guidelines.
- Olivine-labs Lua style guide
- Lua-Users Lua style guide

Re: Lua Style

Posted: Sun Apr 05, 2015 8:16 pm
by undef
I pad braces of functions with spaces, I also pad blocks with spaces when I put them in the same line.
Example:

Code: Select all

function hello( name )  return "hello, " .. name  end
I also like to pad stuff with spaces when it improves readability:

Code: Select all

love.graphics.rectangle( "fill", x,           y, width, height )
love.graphics.rectangle( "fill", x +   width, y, width, height )
love.graphics.rectangle( "fill", x + 2*width, y, width, height )
This way it is easier to see that it's actually three times the same rectangle next to each other than with this code:

Code: Select all

love.graphics.rectangle( "fill", x, y, width, height )
love.graphics.rectangle( "fill", x + width, y, width, height )
love.graphics.rectangle( "fill", x + 2*width, y, width, height )

Re: Lua Style

Posted: Mon Apr 06, 2015 7:57 pm
by T-Bone
I try to avoid camel case, I find it hard to read. I'd rather add an underscore if necessary.

Re: Lua Style

Posted: Tue Apr 07, 2015 8:26 pm
by Jasoco
I have a problem letting go of camelCase and underscores. (And never capitalize the first letter of a variable or function) I keep mixing both of them with lowercase for different variables and if it weren't for Sublime Text telling me what I've already typed I'd probably have forced myself into one or the other by now. I'd want to do all lowercase just because that's how Löve's callbacks are named. (keypressed) But then Löve's functions are camelCase. (setBackgroundColor) So I don't know what to use. I should probably standardize before I get too far.

I also like to use tabs or spaces to make some lines of code match up for easy reading if the subsequent lines are similar. Like this behavior callback I created for my Spark enemy to move around a wall:

Code: Select all

-- Causes entity to move along the edge of a wall using the "gridTileSolid" table to determine which tiles are solid.
-- "moving" determines direction. 1 = Clockwise, -1 = Counter-clockwise
function behavior.followWall(self, dt, moving)
	moving = moving or 1
	if self.isDead or self.dying then return end
	local dir = self.movingDirection

	if     dir == 0 then	dir, self.movingDirection = 1, 1 end
	if     dir == 1 then	self.vx, self.vy = -self.speed, 0 -- LEFT
	elseif dir == 2 then	self.vx, self.vy = 0, -self.speed -- UP
	elseif dir == 3 then	self.vx, self.vy = self.speed, 0  -- RIGHT
	elseif dir == 4 then	self.vx, self.vy = 0, self.speed  -- DOWN
	end
	self.distanceMoved = self.distanceMoved + self.speed * dt

	local future_x = self.x + self.vx * dt
	local future_y = self.y + self.vy * dt

	if self.distanceMoved > TILESIZE then
		self.distanceMoved = 0
		if dir == 1 then		future_x = math.floor(self.x / TILESIZE) * TILESIZE
		elseif dir == 3 then	future_x =  math.ceil(self.x / TILESIZE) * TILESIZE
		elseif dir == 2 then	future_y = math.floor(self.y / TILESIZE) * TILESIZE
		elseif dir == 4 then	future_y =  math.ceil(self.y / TILESIZE) * TILESIZE
		end

		local b = behavior.returnBlockArea(future_x / TILESIZE, future_y / TILESIZE)
		local tl, tc, tr = b[1][1], b[2][1], b[3][1]
		local cl, cc, cr = b[1][2], b[2][2], b[3][2]
		local bl, bc, br = b[1][3], b[2][3], b[3][3]

		if self.moving == 1 then										-- Clockwise
			if dir == 1 then		if not tc then						self.movingDirection = 2
									elseif tc and cl and not bc then	self.movingDirection = 4
									elseif tc and cl and bc then		self.movingDirection = 3
									end
			elseif dir == 2 then	if not cr then						self.movingDirection = 3
									elseif cr and tc and not cl then	self.movingDirection = 1
									elseif cl and tc and cr then		self.movingDirection = 4
									end
			elseif dir == 3 then	if not bc then						self.movingDirection = 4
									elseif bc and cr and not tc then	self.movingDirection = 2
									elseif tc and cr and bc then		self.movingDirection = 1
									end
			elseif dir == 4 then	if not cl then						self.movingDirection = 1
									elseif cl and bc and not cr then	self.movingDirection = 3
									elseif cl and bc and cr then		self.movingDirection = 2
									end
			end
		else 															-- Counter-Clockwise
			if dir == 1 then		if not bc then						self.movingDirection = 4
									elseif bc and cl and not tc then	self.movingDirection = 2
									elseif bc and cl and tc then		self.movingDirection = 3
									end
			elseif dir == 2 then	if not cl then						self.movingDirection = 1
									elseif cl and tc and not cr then	self.movingDirection = 3
									elseif cl and tc and cr then		self.movingDirection = 4
									end
			elseif dir == 3 then	if not tc then						self.movingDirection = 2
									elseif tc and cr and not bc then	self.movingDirection = 4
									elseif tc and cr and bc then		self.movingDirection = 1
									end
			elseif dir == 4 then	if not cr then						self.movingDirection = 3
									elseif cr and bc and not cl then	self.movingDirection = 1
									elseif cl and bc and cr then		self.movingDirection = 2
									end
			end
		end
	end

	self.x, self.y = future_x, future_y
	gameLevel.collisionMap:remove(self)
	gameLevel.collisionMap:add(self, self.x + 2, self.y + 2, self.w, self.h)
end
(For some reason a single line above does not line up in the forum code tags. But it looks right in Sublime.)

I also place underscores before variables that I will need to name properly but only use as a throwaway. Not sure why. (Nothing to do with using just an underscore for returned variables you're not going to use. I also do that. Very smart.)

Re: Lua Style

Posted: Thu Apr 09, 2015 1:27 pm
by Azhukar
Using spaces for indentation is objectively wrong.

I also use camelCase for clearly superior readability.

And I always write conditions in brackets.

Code: Select all

if (true) then
	setmetatable(_G,{
	__newindex = function(self,k,v)
		print("New global variable '"..tostring(k).."'")
		rawset(self,k,v)
	end
	,})
end

Re: Lua Style

Posted: Thu Apr 09, 2015 2:24 pm
by Bindie
Curious, do you write conditions to code easier in C++, to make it familiar?

Re: Lua Style

Posted: Fri Apr 10, 2015 4:43 am
by Jasoco
Tabs for life. I hate using spaces. I need tabs. And I need 4 spaces per tab. (I used to use 2 spaces per tab but switched back to 4 as it's more standard.)

Re: Lua Style

Posted: Fri Apr 10, 2015 4:55 am
by arampl
Recently tried to use tab = 3 spaces instead of "classical" tab = 4 spaces and found it more appropriate for me.

Re: Lua Style

Posted: Fri Apr 10, 2015 10:56 am
by s-ol
Jasoco wrote:Tabs for life. I hate using spaces. I need tabs. And I need 4 spaces per tab. (I used to use 2 spaces per tab but switched back to 4 as it's more standard.)
As long as your ide handles spaces like soft tabs (hit delete once to unindent twice etc) it does't really matter IMO. I use spaces simply because they work across editors etc, my well formatted code is screwed up when the github editor previews Using 8 spaces (waaaay too much) and people who might contribute probably have a different setting aswell.