Thread: Addon Etiquette
View Single Post
04/21/14, 11:28 AM   #20
Vicster0
 
Vicster0's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 82
Originally Posted by NonameFTW View Post
Give me some examples and I'll add in the first post. I'm currently making a Summary of all topics and their current Status.
Lua Code:
  1. --[[
  2.     Example of and addon creating a table and storing all gobals in the object.
  3.     In this case, only the global AddonObject is exposed and all variables inside
  4.     are accessible via this global variable.
  5.  
  6.     It is also good to point out here that the values in this table can be as generically .
  7.     named as the developer wants because they themselves are not global but rather
  8.     exposed via a global object.
  9. --]]
  10.  
  11. uniqueAddonObject = {}
  12. uniqueAddonObject.Settings = {}
  13. uniqueAddonObject.Data = {}
  14. uniqueAddonObject.someNilValue = nil
  15. uniqueAddonObject.someSetValue = 9999
  16. uniqueAddonObject.someSetString = "I'm a string."
  17. uniqueAddonObject.someReference = ZO_PlayerInventory
  18.  
  19. --[[
  20.     Another way to do this is to globally expose all your globals...
  21.     Each variable itself is globally exposed individually.
  22.     In this case, you will want to use unique names and avoid using generically named variables.
  23.     You can accomplish this by prefixing your 'AddonName_' to the variable for instance. There are many
  24.     ways to do this though.
  25.  
  26.     Note: Developers tend to declare global constants in full caps, for instance:
  27.     UNQIUE_ADDON_NAME_VALUE = 10
  28.     This is obviously not necessary but can help you immediately determine if it is a global, global constant,
  29.     or local.
  30. --]]
  31.  
  32. uniqueAddonName_Settings = {}
  33. uniqueAddonName_Data = {}
  34. uniqueAddonName_someNilValue = nil
  35. uniqueAddonName_someSetValue = 9999
  36. uniqueAddonName_someSetString = "I'm a string."
  37. uniqueAddonName_someReference = ZO_PlayerInventory
  38.  
  39.  
  40. --[[
  41.     What you want to avoid doing is making globals that are poorly named or in a scope they do
  42.     not necessarily need to be a part of.
  43.    
  44.     In this case, mySettings is ENTIRELY too broad and can easily conflict with another addon or
  45.     the default UI. Of course, it does usually take two knuckleheads for this to be a problem. But it
  46.     can quickly become a big problem!
  47. --]]
  48.  
  49. mySettings = {}
  50. myData = {}
  51. mysomeNilValue = nil
  52. mysomeSetValue = 9999
  53. mysomeSetString = "I'm a string."
  54. mysomeReference = ZO_PlayerInventory
  55.  
  56. --[[
  57.     As I said before, it is good to keep in mind the scope of your variables/functions. There is a difference
  58.     between globals and locals.
  59. --]]
  60.  
  61. uniqueAddonGlobalObject = {}  --Global to entire API; other addons can reference this variable
  62.  
  63. local myAddonLocalGlobalObject = {} --Local to the scope of this .lua file's functions (at least from my experience).
  64.  
  65. function myAddonGlobalFunction( ... ) --global function to entire API; other addons can call this function
  66.     local value = "My scope is only within this function. So I'm unique and mama loves me!"
  67.     --Since the scope of the above variable is local, it doesn't matter that it's declared as 'value'.
  68.     uniqueAddonName_someSetString = "I'm global, so I'm bigtime! My scope is EVERYTHING!"
  69. end
  70.  
  71. local function myAddonLocalFunction( ... ) --local function; only functions in this .lua file can call this function
  72.     local returnVal = myAddonGlobalFunction( ... )
  73.     --Calling a global function in a local one can be done, but not ALWAYS the other way around!
  74. end

I just sort of threw this together so it may have typos. Also, please correct me if I am mistaken with any of this and let me know if you want more detail.


Last edited by Vicster0 : 04/21/14 at 12:13 PM.
  Reply With Quote