Thread Tools Display Modes
03/23/14, 09:14 PM   #1
skyraker
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 154
Lua, ESO, or me?

Issue I am trying to wrap my head around:

I have a global variable for a string spacer, spacer = " | ", however, when I view details in Zgoo ESO seems to only be recognizing this as spacer = " ". Is it because it is the pipe character and I need to use something else, or is there something being lost between my file and ESO's interpreter?
  Reply With Quote
03/23/14, 09:47 PM   #2
Yssaril
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 2
Originally Posted by skyraker View Post
Issue I am trying to wrap my head around:

I have a global variable for a string spacer, spacer = " | ", however, when I view details in Zgoo ESO seems to only be recognizing this as spacer = " ". Is it because it is the pipe character and I need to use something else, or is there something being lost between my file and ESO's interpreter?
Make sure your using very unique global variables otherwise other addons or even the default UI may malfunction. Something like "addonname_spacer" usually works

Its usually best to stay away from creating globals. Less risk of other addons messing with yours and faster variable access
  Reply With Quote
03/23/14, 10:07 PM   #3
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
AddOn Author - Click to view addons
Join Date: Feb 2014
Posts: 648
In addition to Yss's advice on creating globals (especially generically names ones), you may need to escape the pipe character. I don't recall correctly off the top of my head though. Try using " || " instead...
  Reply With Quote
03/23/14, 10:39 PM   #4
skyraker
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 154
Yeah, looking like it was my use of the pipe. Using a dash worked.

I completely understand refraining from using globals, however as my addon stands right now it needs to access the same variable from multiple spots. I'm going to look on my next iteration about getting rid of that though.
  Reply With Quote
03/24/14, 10:27 AM   #5
skyraker
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 154
Originally Posted by skyraker View Post
Yeah, looking like it was my use of the pipe. Using a dash worked.

I completely understand refraining from using globals, however as my addon stands right now it needs to access the same variable from multiple spots. I'm going to look on my next iteration about getting rid of that though.
I keep thinking about this and I still have no idea how to get those globals away. I only want the data that makes sense to update every frame, while keeping event items separate. If I remove the globals, I feel that I would need to update everything every frame. While in the grand scheme of the addon it probably isn't taking much processing to do so, trading off one bad practice for another doesn't make any sense.

For example, my toolbar updates every frame by looping through the addons saved variables and only displaying those portions that are currently selected (true). Variables that update quickly (such as position) are called only if that is one of the selected portions and is called from the function that builds the bar. For everything else, the only time the string associated with those portions of the toolbar is changed is when its associated event fires.
  Reply With Quote
03/24/14, 01:37 PM   #6
Vuelhering
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 169
Originally Posted by Yssaril View Post
Its usually best to stay away from creating globals. Less risk of other addons messing with yours and faster variable access
Is there a way to scope a variable to only your addon (across several Lua files) without making it global?

Something like...

file1:
local X=555
setup-file2(&X)
X = 444
print X => 444

file2:
local X
function setup-file2(&Y)
X = &Y
end
print X => 444 --after setting it in file1


I don't remember there being any way to equivalence two differently-scoped variables like that in Lua without doing something like a function. But calling a function would probably be slower than using a global.

Last edited by Vuelhering : 03/24/14 at 01:47 PM.
  Reply With Quote
03/24/14, 02:27 PM   #7
Halja
 
Halja's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 111
Variable scope is always a fun question. I may have this wrong but here I go.
Game Global (G_) is a lua table and global to all
Ad-on's SavedVariable is lua table inside G_ and is global to all your add-on files.
lua file
var1 = "something" --is available to all files in the add-on
local var2 = "something" -- is scoped to the file only
local function
local var1 = "something" -- is scoped to the function call only
lua Class

local var1 = "something" -- is scoped to every thing in the class
local function
local var1 = "something" -- is scoped to the function call only



The 'include' part is the <ad-on>.txt file. I have not played with Bindings type files. So, I can not speak to the scope of those.
--halja
  Reply With Quote
03/24/14, 03:01 PM   #8
Xrystal
caritas omnia vincit
 
Xrystal's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Feb 2014
Posts: 369
I believe the first var1 is classed as global across all addons and is the same as adding it to the _G table.

I am not sure if ESO has the equivalent of the addon wide data table but that would be the only way to be addon wide across all files for just a single addon.



Lua Code:
  1. globalVar = "All Addons can see me"
  2.  
  3. local localVar = "This file can see me"
  4.  
  5. function globalFunc = All addons can use me
  6. global globalFunc = function() ... same as above
  7.  
  8. local function localFunc = Only this file can use me
  9. local localFunc = function() .. same as above

Lua Code:
  1. local function aFunc()
  2.    local localVar = "Only this function has access to me"
  3.    return localVar  ... unless I return it so that they can use it
  4. end

Lua Code:
  1. for I = 1,10 do
  2.    local localVar1  = "available to the whole for loop"
  3.    if I == 4 then
  4.       local localVar2 = "available only to the if block"
  5.    end
  6. end

Hope these and the other comments help you understand how scope works.

Last edited by Xrystal : 03/24/14 at 03:03 PM.
  Reply With Quote
03/24/14, 03:24 PM   #9
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
AddOn Author - Click to view addons
Join Date: Feb 2014
Posts: 648
Originally Posted by Xrystal View Post
I believe the first var1 is classed as global across all addons and is the same as adding it to the _G table.
Correct.
Originally Posted by Xrystal View Post
I am not sure if ESO has the equivalent of the addon wide data table but that would be the only way to be addon wide across all files for just a single addon.
No, it does not.

Local variables are available at the scope they are defined and lower. Global variables are available to all things - hence being called "global". If you use a global variable called spacer, and I accidentally leak a global variable also called spacer, whichever one was defined last will overwrite the other.

If you want variables accessible throughout your entire addon, across multiple files, then use a table to store them in. Here is just one example of how you can do that.

File1.lua
Lua Code:
  1. MyAddonTable = {}   --define your table
  2. local MyAddonTable = MyAddonTable   --go ahead and give it a local reference, too, for this file
  3. MyAddonTable.var = "varForAllFiles"

File2.lua
Lua Code:
  1. --since File1 loaded first, MyAddonTable has already been created
  2. local var = MyAddonTable.var   --we can copy the value of MyAddonTable.var to a local variable
  Reply With Quote

ESOUI » Developer Discussions » Lua/XML Help » Lua, ESO, or me?


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off