Thread: Slash Commands
View Single Post
04-03-16, 09:07 PM   #10
MunkDev
A Scalebane Royal Guard
 
MunkDev's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2015
Posts: 431
The compiler reads your code like a book, from start to finish. If you don't declare variables before you use them, they'll always be nil (unless they exist in a higher scope). As an example:

Lua Code:
  1. -- myVariable is declared, but is nil
  2. local myVariable
  3.  
  4. -- this function can access myVariable, because myVariable was declared beforehand,
  5. -- even though myVariable has no value assigned to it.
  6. local function whatIsMyVariable()
  7.     print("My variable is: ", myVariable)
  8. end
  9.  
  10. whatIsMyVariable()
  11. -- prints: My variable is nil
  12.  
  13. myVariable = "abc"
  14. -- myVariable is given a value
  15.  
  16. whatIsMyVariable()
  17. -- prints: My variable is abc

Lua Code:
  1. -- this function has no idea what myVariable is,
  2. -- because it didn't exist before this function was declared.
  3. local function whatIsMyVariable()
  4.     print("My variable is: ", myVariable)
  5. end
  6.  
  7. local myVariable
  8.  
  9. whatIsMyVariable()
  10. -- prints: My variable is nil
  11.  
  12. myVariable = "abc"
  13. -- myVariable is given a value
  14.  
  15. whatIsMyVariable()
  16. -- prints: My variable is nil
__________________

Last edited by MunkDev : 04-03-16 at 09:11 PM.
  Reply With Quote