12.07.2015 Views

Wiley-World.of.Warcraft.Programming.A.Guide.and.Reference.for.Creating.WoW.Addons

Wiley-World.of.Warcraft.Programming.A.Guide.and.Reference.for.Creating.WoW.Addons

Wiley-World.of.Warcraft.Programming.A.Guide.and.Reference.for.Creating.WoW.Addons

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 3 ■ Basic Functions <strong>and</strong> Control Structures 43true> hello2()Hello <strong>World</strong>!> hello2 = function() print(“Hello <strong>World</strong>!“) end> print(hello2 == hello)falseIn the final lines <strong>of</strong> the preceding example, a new function is created <strong>and</strong>bound to hello2. Even though the new function has the exact same definition<strong>and</strong> body as hello, it is actually a distinct function.Making Decisions with the if StatementThe if statement is the basis <strong>for</strong> decision making in Lua, <strong>and</strong> it supportssimple conditionals as well as more complex statements. The syntax <strong>of</strong> themost basic if statement looks like this:if then-- do somethingendSimple ConditionalsAn if statement can be used to execute a block <strong>of</strong> code conditionally whenthe Boolean expression evaluates to true. To better see this, type the followinginto your interpreter:function conditional_test(num)print(“You input: “ .. num)if (num == 7) thenprint(“You found the magic number!“)endendThis example function prints whatever number it gets passed, but if thenumber 7 is passed, it will print an additional special message. Input thisfunction into your interpreter, <strong>and</strong> then test it with the following:> conditional_test(3)You input: 3> conditional_test(7)You input: 7You found the magic number!> conditional_test(13)You input: 13

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!