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? |
Quote:
Its usually best to stay away from creating globals. Less risk of other addons messing with yours and faster variable access :) |
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...
|
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. |
Quote:
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. |
Quote:
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. |
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 allAd-on's SavedVariable is lua table inside G_ and is global to all your add-on files.lua filevar1 = "something" --is available to all files in the add-on 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 |
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:
Lua Code:
Lua Code:
Hope these and the other comments help you understand how scope works. |
Quote:
Quote:
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:
File2.lua Lua Code:
|
Quote:
Although the "local var = MyAddonTable.var" is not two-way. If you change the value of var, you still have to export it back out to MyAddonTable.var. |
Thanks you for all the replies so far. Scope is always a fun topic.
1) Like all the talk about using spacer as a global, but my addon didn't call it simply that. Looking at things, I managed to completely rid it as a global though. 2) If a global in the lua file is available to all addons, and making it a local outside the scope of any function makes it only available to the addon, then I may have my problem solved in a sense without much of a change. When I was thinking of it as a global, I was thinking only that it was global to the addon. If that is more 'acceptable', then maybe it is best to keep things that way (but just in case ensure all my variables aren't easily reproducible by other addons). |
So instead of a Cross-Site scripting attack, there could be a cross add-on scripting attack vector on a lua table...
A silly thing to do but Mojo Jojo would be proud. |
Quote:
Nah, it's still global, you just have a local reference. MyUniqueAddonName_LastQuestID = 1 ... This is a global variable local lastQuestID = MyUniqueAddonName ... this gives you a local version of the variable As far as I know this would either branch, so to speak, the variable so that now there is a global version and a local version. Whether they co-exist or not I don't know. I think I have read both sides of that conversation and still aren't sure of when allocation is just a reference or whether it is another copy of the variable. Possibly functions and tables are referenced and others are just plain values |
But what I am getting at is using the local reference name in both my files rather than the global name.
Code:
MS = {} Code:
thisMS.count = thisMS.count + 1 |
Yes that would work as MS = {} is a global table available to all addons, but each file would only be able to access the MS one, so you would have to add that local line in each file if you want to reference it in the same way.
File 1: -- Create or use the existing table UniqueName_MS = UniqueName_MS or {} -- Create a local reference to the table local thisMS = UniqueName_MS File 2: -- Create or use the existing table UniqueName_MS = UniqueName_MS or {} -- Create a local reference to the table local thisMS = UniqueName_MS Something like this near the top of your files will simulate an addon wide variable but it is in essence a global variable for all addons to access, so bear that in mind. |
Quote:
Quote:
|
All times are GMT -6. The time now is 01:13 AM. |
vBulletin © 2024, Jelsoft Enterprises Ltd
© 2014 - 2022 MMOUI