Thread Tools Display Modes
01-28-10, 05:36 PM   #21
Xrystal
nUI Maintainer
 
Xrystal's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Feb 2006
Posts: 5,892
Do I take it then that you could go a step further and make something like this ?

PublicData[addonName].SomeTable_or_DataVariable_Here = "Just so someone can access this variable outside of the addon for further customization..."

Is something we could do to make a public variable more unique seeing as only one addon of that name should exist. Hmm but then again, there's no guarantee that someone doesn't use the same name for data storage ... sigh, was a thought.
__________________
  Reply With Quote
01-28-10, 06:09 PM   #22
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,313
If you really need to, I guess, but you have to write code to make that table global. Maybe something like what LibStub does.
__________________
WoWInterface AddOns
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)
  Reply With Quote
01-28-10, 06:15 PM   #23
Xrystal
nUI Maintainer
 
Xrystal's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Feb 2006
Posts: 5,892
As far as I know though any object not declared local is global right ? Like I can access many of nUI's functions and variables from my plugins because they are globally available for that purpose.

local ThisIsALocalVariable
ThisIsAGlobalVariable = 1234
ThisIsAGlobalTable = { SubTable1 = {}, Value1 = 23 }

In all the addons I have seen have done things this way which allows public access to the variables.
__________________
  Reply With Quote
01-28-10, 06:26 PM   #24
Torhal
A Pyroguard Emberseer
 
Torhal's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2008
Posts: 1,196
Variables of any type are global unless specified local.
__________________
Whenever someone says "pls" because it's shorter than "please", I say "no" because it's shorter than "yes".

Author of NPCScan and many other AddOns.
  Reply With Quote
01-28-10, 10:12 PM   #25
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,313
The vararg expression isn't a variable, it's a Lua language construct, used to hold any number of arguments passed into a function. The values contained in are just that, values, not variables. In this case, a pointer to the same empty table is passed as the second return to the root of each Lua file contained in each specific addon. Every addon has its own table reference passed to it.

The vararg expression was implemented in Lua 5.1 and WoW started using that version of Lua in patch 2.0. In the normal Lua environment, the vararg expression at the root of the code would hold the contents of the argc and argv vars in C. Blizzard, of course, blocked this.
__________________
WoWInterface AddOns
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)
  Reply With Quote
01-28-10, 10:17 PM   #26
Xrystal
nUI Maintainer
 
Xrystal's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Feb 2006
Posts: 5,892
Ah, thanks for the info. Was just an idea. If I wanted to make an object public I would just do it the normal way and use the new way for keeping my internal objects internal.
__________________
  Reply With Quote
01-28-10, 11:39 PM   #27
Akryn
A Firelord
AddOn Author - Click to view addons
Join Date: Mar 2008
Posts: 479
I'm not seeing why you can't create a global pointer to the table (although, it does seem like that defeats most of the point of having it).

Code:
_, exposedTable = ...
  Reply With Quote
01-29-10, 12:45 AM   #28
Torhal
A Pyroguard Emberseer
 
Torhal's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2008
Posts: 1,196
Originally Posted by Akryn View Post
I'm not seeing why you can't create a global pointer to the table (although, it does seem like that defeats most of the point of having it).

Code:
_, exposedTable = ...
You can, and it does.
__________________
Whenever someone says "pls" because it's shorter than "please", I say "no" because it's shorter than "yes".

Author of NPCScan and many other AddOns.
  Reply With Quote
01-29-10, 12:49 AM   #29
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,313
Any value could be easily assigned to a global variable, I was just explaining that it isn't one initially.
__________________
WoWInterface AddOns
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)
  Reply With Quote
02-01-10, 06:15 PM   #30
Xrystal
nUI Maintainer
 
Xrystal's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Feb 2006
Posts: 5,892
Well, I found out an interesting thing while utilising this new feature in my latest addon.

Eg.

Code:
local MultipleAnchors = Variables["ScrollFrame"].MultipleAnchors;
print("Current Multiple Anchors in Use ? : ", MultipleAnchors);
Variables["ScrollFrame"].MultipleAnchors = true;
print("Old Multiple Anchors in Use ? : ", MultipleAnchors);
print("New Multiple Anchors in Use ? : ", Variables["ScrollFrame"].MultipleAnchors);
Initially it is defaulted to false and then set to true. I initially expected it to override the value in the stored table being that variables are pointers to another. Erm, I guess not in this instance. The first print shows false, the second shows false and the last print shows true. So to override a value in the table for use by other files, specify the table completely. To simply use a value locally without affecting the table you can use a local. Which I believe you all inferred but I didn't realise you meant it would work that way rofl.

Whats good about this is that I can now keep a set of default variables without fear of overwriting them if I then set them to a new variable table so in theory ( and I will be testing this shortly ) you could do this.

DefaultVars.lua
Code:
addonTable.Variables = {};
addonTable.Variables["Defaults"] = {};
addonTable.Variables["Defaults"].defaultVar1 = "blah"
addonTable.Variables["Defaults"].defaultTab1 = { item1 = "blah2", item2 = "blah3" }
etc
SpecialVars.lua
Code:
local DefaultVars = addonTable.Variables["Default"];
addonTable.Variables["Special"] = DefaultVars;
SavedVars.lua
Code:
local DefaultVars = addonTable.Variables["Default"];
SavedVars = DefaultVars;

function addonTable.Functions["SavedVars"].ResetDefaults()
  local DefaultVars = addonTable.Variables["Default"];
  SavedVars = DefaultVars;
end

etc
Main.lua
Code:
local DefaultVars = addonTable.Variables["Default"];
local SpecialVars = addonTable.Variables["Special"];

if slashCmdParam1 = "reset" then
  addonTable.Functions["SavedVars"].ResetDefaults()
  --UpdateAddonWithNewValues
end
Handy thing to know if you have a file holding generic data you always use such as background values, generic functions etc. Put them all in a file and use it in all your addons.

Edt: And this is probably what was breaking some parts of my addon as I was testing against values that had been overriden via a localised version of the variable expecting it to override the addonTable one.
__________________

Last edited by Xrystal : 02-01-10 at 06:20 PM.
  Reply With Quote
02-01-10, 06:42 PM   #31
Recluse
A Cliff Giant
 
Recluse's Avatar
AddOn Author - Click to view addons
Join Date: Feb 2007
Posts: 70
omg! D=

I had no idea about this local global concept's inner workings. I heard about it, but didn't notice any changes with my addons, and dismissed it all.

This is WONDERFUL though. Really.
__________________
We'd be together, but only diamonds last forever...
  Reply With Quote
02-02-10, 11:09 AM   #32
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,313
Originally Posted by Xrystal View Post
Well, I found out an interesting thing while utilising this new feature in my latest addon.

Eg.

Code:
local MultipleAnchors = Variables["ScrollFrame"].MultipleAnchors;
print("Current Multiple Anchors in Use ? : ", MultipleAnchors);
Variables["ScrollFrame"].MultipleAnchors = true;
print("Old Multiple Anchors in Use ? : ", MultipleAnchors);
print("New Multiple Anchors in Use ? : ", Variables["ScrollFrame"].MultipleAnchors);
Initially it is defaulted to false and then set to true. I initially expected it to override the value in the stored table being that variables are pointers to another. Erm, I guess not in this instance. The first print shows false, the second shows false and the last print shows true. So to override a value in the table for use by other files, specify the table completely. To simply use a value locally without affecting the table you can use a local. Which I believe you all inferred but I didn't realise you meant it would work that way rofl.
One thing to note, there are two kinds of values. Primitive ones such as numbers, strings, boolean, and nil are directly stored in the variable. Changes do not propagate to other variables that might've been assigned a copy to. However, functions, tables, userdata, and coroutines are stored as pointers to the object in memory. The difference here is an object stored by value or by reference.

Here's an example:
Code:
a=false;-- a initialized to true
b=a;-- b set to a
a=true;-- a is set to true, b remains false
Since boolean is one of the primitive variable types, it's stored by value. Any change to variable a won't be reflected in variable b.

Code:
a={v=false};-- a.v initialized to false
b=a;-- b set to a
a.v=true;-- a.v set to true, b.v is also true
Variable a is set to the pointer of a new table, in which variable b is set to the same pointer. The table is stored by reference, so any changes inside the table are reflected in both variables.

Here's where your test code came in...
Code:
a={v=false};-- a.v initialized to false
b=a.v;-- b set to a.v
a.v=true;-- a.v set to true, b remains false
Even though a table is involved, this still follows the same rules as set by value. Variable b is still set with a primitive value and retains it after the change in the table.
__________________
WoWInterface AddOns
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)
  Reply With Quote
02-02-10, 11:22 AM   #33
nightcracker
A Molten Giant
 
nightcracker's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2009
Posts: 716
Anything involved with table defaults is solved the easiest way with metatables.

lua Code:
  1. local addon = select(2,...)
  2. local defaults = {
  3.     __index = {
  4.         somefunc = function() end,
  5.         iwantthisvardefault = "321g90",
  6.     }
  7. }
  8. function addon.DefaultTable(tab)
  9.     setmetatable(tab or {}, defaults)
  10. end

Now if you put this in the top of every lua file in your addon you will be able to call it to create a table with default functions:
lua Code:
  1. local DefaultTable = select(2, ...).DefaultTable

Which creates the following API:

local deftab = DefaultTable([tab])
If tab is given, then this will apply the defaults to the table given, else it will create a new blank tabl and apply the defaults to that. Will always return the table with defaults(created or not).
__________________
Three things are certain,
Death, taxes and site not found,
You, victim of one.
  Reply With Quote
02-02-10, 11:56 AM   #34
Xrystal
nUI Maintainer
 
Xrystal's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Feb 2006
Posts: 5,892
Hmm,

SDPhantom :
I am pretty sure that in the past I have overridden default values that way when not using the addonName, addonTable = ... system. However, I could have been lucky and it was working the way you said and this time it wouldn't have.

NightCracker:
Meta tables I have read about but sounded a bit too confusing for me to deal with at the moment but then again this new way of working seemed confusing at first so I am sure in time I will get to learn about metatables in some addon somewhere and pass it along to my others once I have understood it.


But, I still feel the addonName, addonTable = ... is definitely a boon to keeping my code tidier and more readable.
__________________
  Reply With Quote
02-02-10, 03:37 PM   #35
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,313
Originally Posted by Xrystal View Post
I am pretty sure that in the past I have overridden default values that way when not using the addonName, addonTable = ... system. However, I could have been lucky and it was working the way you said and this time it wouldn't have.
It's how Lua has always dealt with variables no matter where you got the values from. You probably just stored a pointer to the table and referenced an index inside. There's no such thing as a pointer to a primitive value, no matter if it's a part of the table or in its own variable.
__________________
WoWInterface AddOns
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)
  Reply With Quote
02-02-10, 03:43 PM   #36
Xrystal
nUI Maintainer
 
Xrystal's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Feb 2006
Posts: 5,892
Originally Posted by SDPhantom View Post
It's how Lua has always dealt with variables no matter where you got the values from. You probably just stored a pointer to the table and referenced an index inside. There's no such thing as a pointer to a primitive value, no matter if it's a part of the table or in its own variable.
Ah, probably.
__________________
  Reply With Quote

WoWInterface » Developer Discussions » General Authoring Discussion » Addon file system structuring

Thread Tools
Display Modes

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