Lua Style

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Bindie
Party member
Posts: 151
Joined: Fri Jan 23, 2015 1:29 pm

Lua Style

Post 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.
User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

Re: Lua Style

Post by Roland_Yonaba »

I like to follow those Lua Style guidelines.
- Olivine-labs Lua style guide
- Lua-Users Lua style guide
User avatar
undef
Party member
Posts: 438
Joined: Mon Jun 10, 2013 3:09 pm
Location: Berlin
Contact:

Re: Lua Style

Post 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 )
twitter | steam | indieDB

Check out quadrant on Steam!
User avatar
T-Bone
Inner party member
Posts: 1492
Joined: Thu Jun 09, 2011 9:03 am

Re: Lua Style

Post by T-Bone »

I try to avoid camel case, I find it hard to read. I'd rather add an underscore if necessary.
User avatar
Jasoco
Inner party member
Posts: 3727
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: Lua Style

Post 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.)
User avatar
Azhukar
Party member
Posts: 478
Joined: Fri Oct 26, 2012 11:54 am

Re: Lua Style

Post 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
Bindie
Party member
Posts: 151
Joined: Fri Jan 23, 2015 1:29 pm

Re: Lua Style

Post by Bindie »

Curious, do you write conditions to code easier in C++, to make it familiar?
User avatar
Jasoco
Inner party member
Posts: 3727
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: Lua Style

Post 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.)
User avatar
arampl
Party member
Posts: 248
Joined: Mon Oct 20, 2014 3:26 pm

Re: Lua Style

Post by arampl »

Recently tried to use tab = 3 spaces instead of "classical" tab = 4 spaces and found it more appropriate for me.
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: Lua Style

Post 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.

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest