Lua Scripting Best Practice: Whitespace and Indentation
Hi,
This post is about a best practice for Lua scripting that will help you solve your own problems faster. Yes, I'm talking about the simple idea of whitespace (or indentation).
Indentation is ignored by the Lua script runner and doesn't add anything to make the code faster or better - but it does make coding, reading, and finding bugs easier.
First, let's start with a definition: a 'block'
'Blocks' are lines of code that are grouped together in some meaningful way. In Lua, they start with keywords like 'function', 'for', 'while', 'if'; and they are always ended with the keyword 'end'.
Let's illustrate this point with an example levelgen script:
Can you spot the bug in the above code? If so, did it take long?
Now, let's look at the same code that has proper whitespace:
Did you spot the bug this time? Was it easier?
The bug was that the 'if' block was missing an ending 'end' keyword. Since I took the time to always indent code in a block one indent level further, I could tell at a glance what the problem was.
Now try this one. Can you find the bug?:
Did you find it? Good... There was no bug!
Bad indentation (or whitespace) is just as bad (and probably worse) as no indentation.
The moral
If you have a bug in your script, first make sure your whitespace and indentation is all nice and 'pretty' looking. I can guarantee you that when you ask for help from me or any other experience bitfighter Lua scripter, the first thing we do is indent the code and add whitespace to make it easier to read. And most of the time, the bug is very apparent afterwards.
This post is about a best practice for Lua scripting that will help you solve your own problems faster. Yes, I'm talking about the simple idea of whitespace (or indentation).
Indentation is ignored by the Lua script runner and doesn't add anything to make the code faster or better - but it does make coding, reading, and finding bugs easier.
First, let's start with a definition: a 'block'
'Blocks' are lines of code that are grouped together in some meaningful way. In Lua, they start with keywords like 'function', 'for', 'while', 'if'; and they are always ended with the keyword 'end'.
Let's illustrate this point with an example levelgen script:
Can you spot the bug in the above code? If so, did it take long?
Now, let's look at the same code that has proper whitespace:
Did you spot the bug this time? Was it easier?
The bug was that the 'if' block was missing an ending 'end' keyword. Since I took the time to always indent code in a block one indent level further, I could tell at a glance what the problem was.
Now try this one. Can you find the bug?:
- Code:
- toggle = 1
function onShipEnteredZone(ship) toggle = toggle * -1
if toggle == 1 then turret:setWeapon(Weapon.Seeker)
else turret:setWeapon(Weapon.Triple)
end
local items = {}
bf:findAllObjects(items, ObjType.TestItem, ObjType.ResourceItem)
logprint(items) end
toggle2 = 1
function toggleTeam()
toggle2 = toggle2 * -1
if toggle2 == 1 then
ball:setPos(point.new(800, 0))
else
ball:setPos(point.new(-800, 0))
end
end
function main() subscribe(Event.ShipEnteredZone)
turret = bf:findObjectById(5)
ball = bf:findObjectById(1)
Timer:scheduleRepeating(toggleTeam, 4000)
end
Did you find it? Good... There was no bug!
Bad indentation (or whitespace) is just as bad (and probably worse) as no indentation.
The moral
If you have a bug in your script, first make sure your whitespace and indentation is all nice and 'pretty' looking. I can guarantee you that when you ask for help from me or any other experience bitfighter Lua scripter, the first thing we do is indent the code and add whitespace to make it easier to read. And most of the time, the bug is very apparent afterwards.