Navigation menu
Personal tools
Not logged in
Talk
Contributions
Create account
Log in
Namespaces
WoW
Talk
English
Views
Read
Edit
History
More
Search
Navigation
Home
Random page
Help using wiki
Editions
for WoW
for WildStar
for Solar2D
Documentation
for WoW
for WildStar
Reference
WoW
⦁ FrameXML
⦁ AddOns
⦁ API
⦁ WoW Lua
WildStar
⦁ AddOns
⦁ API
⦁ WildStar Lua
Engine
Tools
What links here
Related changes
Special pages
Page information
Site
Recent Changes
Editing
WoW:Common Lua shortcuts
Jump to navigation
Jump to search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
{{wow/uihowto}} This article covers several syntactic features of Lua that are commonly used in addon code. == Logical short-cut evaluation == When evaluating the results of boolean operators '''and''' and '''or''', Lua only evaluates the right-hand operand if necessary. This makes it possible to provide default values to nil or false expressions, or implement something resembling a trigraph operator. For instance: MySVTable = MySVTable or {}; print(UnitExists("target") and "You have a target" or "You don't have a target"); -- As a trigraph A notable limitation of using short-cut evaluation as a trigraph operator is that if the ''true'' expression may return a false value, the false expression will be returned: function isFrameShown(frame, checkParentFrames) -- The expression below returns the wrong value if frame's parent is hidden. return checkParentFrames and frame:IsVisible() or frame:IsShown(); end This occurs because nothing prevents the evaluation of the or operator as its left-hand operand is false/nil. == Colon syntax for defining methods == The colon syntax is used for defining methods -- functions that have an implicit first parameter ''self''. The following function definitions are equivalent: t = {}; function t:foo(bar) end function t.foo(self, bar) end When you call a function using the colon syntax, you implicitly supply the table left of the colon as the first argument to the function. The following two function calls are equivalent: t:foo("baz"); t.foo(t, "baz"); This notation is frequently used to emulate something resembling objects. == The global environment table == The global environment table <code>_G</code> is defined in most function environments. You can look up global variables by using their names as a key in this table. For example: -- Suppose MyBar1, MyBar2, MyBar3 are global variables (Frames created in XML, for example) for i=1, 3 do _G["MyBar" .. i]:Show(); end The [[API getglobal|getglobal]]("name") function enables similar behavior, but at a marginally higher cost. [[Category:Interface customization]]
Summary:
Please note that all contributions to AddOn Studio are considered to be released under the Creative Commons Attribution-NonCommercial-ShareAlike (see
AddOn Studio Wiki:Copyrights
for details).
Submissions must be written by you, or copied from a public domain or similar free resource (see
AddOn Studio Wiki:Copyrights
for details).
Cancel
Editing help
(opens in new window)
Templates used on this page:
Template:Apinav
(
edit
)
Template:Editlink
(
edit
)
Template:Wow/uihowto
(
edit
)