FAQ  •  Register  •  Login

Lua Scripting Best Practice: Whitespace and Indentation

<<

raptor

Posts: 1046

Joined: Mon Oct 11, 2010 9:03 pm

Post Thu Mar 27, 2014 9:51 am

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:
  Code:
function main()
local words = "hello world!"
local text = TextItem.new()
text:setPos(point.new(0,0))
if(math.random(2) == 1) then
words = "hello jupiter!"
bf:addItem(text)
end

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:
  Code:
function main()
    local words = "hello world!"
   
    local text = TextItem.new()
    text:setPos(point.new(0,0))
   
    if(math.random(2) == 1) then
        words = "hello jupiter!"
   
    bf:addItem(text)
end

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

kaen

User avatar

Posts: 209

Joined: Thu Jun 14, 2012 11:54 am

Post Thu Mar 27, 2014 10:54 am

Re: Lua Scripting Best Practice: Whitespace and Indentation

As a corollary, you should be using a good text editor:
Image

I use Sublime Text 3. It works on Windows/Mac/Linux, and the demo is free and never expires.
http://www.sublimetext.com/3

On top of a TON of other cool features (look up sublime text on youtube), it has syntax coloring and will automatically indent your code as you type, and has a feature to indent the text.
bobdaduck wrote:Next, the moon!

└────────┘
⎈⎈⎈⎈
┌────────┐
<<

watusimoto

Site Admin

Posts: 1558

Joined: Tue Feb 23, 2010 7:07 pm

Location: Quartz's mom's house

Post Fri Mar 28, 2014 2:04 am

Re: Lua Scripting Best Practice: Whitespace and Indentation

I hate to disagree, but Sublime is not a "good" editor. It is an AWESOME editor.
<<

Fordcars

User avatar

Posts: 1016

Joined: Fri Apr 20, 2012 3:51 pm

Location: Some city, somewhere

Post Fri Mar 28, 2014 2:18 pm

Re: Lua Scripting Best Practice: Whitespace and Indentation

Whoa that looks cool!
skybax: Why is this health pack following me?
bobdaduck: uh, it likes you.
<<

amgine

Posts: 1399

Joined: Thu Apr 19, 2012 2:57 pm

Post Sat Sep 06, 2014 2:19 pm

Re: Lua Scripting Best Practice: Whitespace and Indentation

also space each block and each loop out makes it easier.
Bitfighter Forever.

Return to Technical Support

Who is online

Users browsing this forum: No registered users and 8 guests