Thread Tools Display Modes
01-06-15, 03:35 PM   #1
sticklord
A Wyrmkin Dreamwalker
AddOn Author - Click to view addons
Join Date: Aug 2014
Posts: 57
Lua table addresses

Hello!

Got some questions about lua addresses, not sure how I can explain it but I'll give it a go Not gonna give my whole code though, since it's a pretty general question. I'll just jump to it:

Let's say I have an addon with 2 files. The first one looks like this, it changed profiles:
Lua Code:
  1. local _, ns = ...
  2.  
  3. function SetID(id)
  4.      ns.settings = _G.SavedVars[id]
  5. end
  6.  
  7. ns.settings = _G.SavedVars['default']

The second on wants to use the ns.settings variable:
Lua Code:
  1. local _, ns = ...
  2. local x = ns.settings

Lets say i call SetID('volvo'), isn't the local variable "x" supposed to point to SavedVars['volvo'] then, or is it pointing to the old value of ns.settings? It seemed like it was the latter, but i thought it would be opposite.

Thanks for your time!

Edit // When I explained it that well to myself it makes sense to point to the old ns.setting variable, dumb me.

Last edited by sticklord : 01-06-15 at 05:03 PM. Reason: Enlightenment
  Reply With Quote
01-06-15, 04:13 PM   #2
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,323
Just addressing the theory of what's happening. I seriously hope you're not using that code in your actual addon.

To answer the question, it depends on what is stored at the index you're referring to. If SavedVars['volvo'] is a table, it will store a pointer to the table and anything done to it will reflect changes in both variables. If it's any basic value (nil, number, string, boolean), it will just copy the value.

Note, other value types like functions, threads, and uservalues are stored by pointers like tables, but are unmodifiable. This gives them the illusion of being passed by value like the basic types, but they are by reference.
__________________
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)

Last edited by SDPhantom : 01-06-15 at 04:20 PM.
  Reply With Quote
01-06-15, 05:16 PM   #3
sticklord
A Wyrmkin Dreamwalker
AddOn Author - Click to view addons
Join Date: Aug 2014
Posts: 57
Thanks for the explanation, and no it was just an example
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Lua table addresses


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