View Single Post
Old 02-23-13, 10:47 AM   #1
zork
A Pyroguard Emberseer
 
zork's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2008
Posts: 1,308
How to create a copy of an object without reference? [SOLVED]

I have a problem that I cannot solve.

For my UI panel I want a "reset" feature. Thus I have a reset button that can be called to load the database defaults.

The problem I'm having is that as soon as I do this the default data becomes a reference. Thus on a second click on reset it will not work anymore. Because of the reference issue.

Here is my code:
Lua Code:
  1. --object container
  2.   local db = CreateFrame("Frame")
  3.   ns.db = db
  4.   db.default = {}
  5.  
  6.   ---------------------------------------------
  7.   --DEFAULTS
  8.   ---------------------------------------------
  9.  
  10.   --default orb setup
  11.   db.default.orb = {
  12.     --health
  13.     ["HEALTH"] = {
  14.       --filling
  15.       filling = {
  16.         texture     = "Interface\\AddOns\\oUF_Diablo\\media\\orb_filling15",
  17.         color       = { r = 1, g = 0, b = 0, },
  18.         colorAuto   = false, --automatic coloring based on class/powertype
  19.       },
  20.     },--health end
  21.     --power
  22.     ["POWER"] = {
  23.       --filling
  24.       filling = {
  25.         texture     = "Interface\\AddOns\\oUF_Diablo\\media\\orb_filling15",
  26.         color       = { r = 0, g = 0, b = 1, },
  27.         colorAuto   = false, --automatic coloring based on class/powertype
  28.       },
  29.     },--power end
  30.   } --default end
  31.  
  32.   db.getDefaultOrbHealth = function() return db.default.orb["HEALTH"] end
  33.   db.getDefaultOrbPower = function() return db.default.orb["POWER"] end
  34.   db.getDefaultOrb = function() return db.default.orb end
  35.  
  36.   ---------------------------------------------
  37.   --CHARACTER DATA
  38.   ---------------------------------------------
  39.  
  40.   --load character data defaults
  41.   db.loadCharacterDataDefaults = function(type)
  42.     if type then
  43.       if type == "HEALTH" then
  44.         OUF_DIABLO_DB_CHAR[type] = db.getDefaultOrbHealth()
  45.         print(addon..": health orb reseted to default")
  46.       elseif type == "POWER" then
  47.         OUF_DIABLO_DB_CHAR[type] = db.getDefaultOrbPower()
  48.         print(addon..": power orb reseted to default")
  49.       end
  50.     else
  51.       OUF_DIABLO_DB_CHAR = db.getDefaultOrb()
  52.       print(addon..": character data defaults loaded")
  53.     end
  54.     db.char = OUF_DIABLO_DB_CHAR
  55.     --update the orb view
  56.     ns.panel.updateOrbView()
  57.   end

OUF_DIABLO_DB_CHAR is my saved variable per character.

Basically when I click the reset button I call
Code:
  db.loadCharacterDataDefaults("HEALTH")
I cannot copy the table data via ipairs. Assoc tables cannot be looped. The following will return 0:
Code:
print(# db.default.orb["HEALTH"])
Any idea?

Tuller uses sth like
Lua Code:
  1. local function copyDefaults(tbl, defaults)
  2.     for k, v in pairs(defaults) do
  3.         if type(v) == 'table' then
  4.             tbl[k] = copyDefaults(tbl[k] or {}, v)
  5.         elseif tbl[k] == nil then
  6.             tbl[k] = v
  7.         end
  8.     end
  9.     return tbl
  10. end

Maybe I'm missing sth...

Oh...his GetDefault funnction is looking like:
Lua Code:
  1. function tullaRange:GetDefaults()
  2.     return {
  3.         normal = {1, 1, 1},
  4.         oor = {1, 0.3, 0.1},
  5.         oom = {0.1, 0.3, 1}
  6.     }
  7. end

Hmmm...so this could be an idea. I do not create a default table variable in first place that could get referenced later on.

I will try that.

Perfect that did it!

What I'm now doing is:
Lua Code:
  1. --default orb setup
  2.   function db:GetOrbDefaults()
  3.     return {
  4.       --health
  5.       ["HEALTH"] = {
  6.         --filling
  7.         filling = {
  8.           texture     = "Interface\\AddOns\\oUF_Diablo\\media\\orb_filling15",
  9.           color       = { r = 1, g = 0, b = 0, },
  10.           colorAuto   = false, --automatic coloring based on class/powertype
  11.         },
  12.       },--health end
  13.       --power
  14.       ["POWER"] = {
  15.         --filling
  16.         filling = {
  17.           texture     = "Interface\\AddOns\\oUF_Diablo\\media\\orb_filling15",
  18.           color       = { r = 0, g = 0, b = 1, },
  19.           colorAuto   = false, --automatic coloring based on class/powertype
  20.         },
  21.       },--power end
  22.     } --default end
  23.   end
__________________
| Simple is beautiful.
| Roth UI | Roth UI FAQ | GoogleCode | Zork | TDMOG

"I wonder what the non-pathetic people are doing tonight?" - Rajesh Koothrappali (The Big Bang Theory)

Last edited by zork : 02-23-13 at 11:18 AM. Reason: SOLVED
zork is online now   Reply With Quote