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:
OUF_DIABLO_DB_CHAR is my saved variable per character. Basically when I click the reset button I call Code:
db.loadCharacterDataDefaults("HEALTH") Code:
print(# db.default.orb["HEALTH"]) Tuller uses sth like Lua Code:
Maybe I'm missing sth... Oh...his GetDefault funnction is looking like: Lua Code:
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:
|
I feel like I remember seeing "default settings" tables in code, before. I assume they're usually used to initialize, but they could just as easily be used to reset. Nice.
|
One thing you could also do if your options are not very complicated is make use of metatables:
http://www.lua.org/pil/13.4.3.html Then when resetting, just loop over the options database table and set all keys to nil. |
I use the following function to copy a table by value:
Code:
local function copyTable(source, target) |
If that works, then what is this talking about?
Quote:
|
I don't see the problem there. Why not use:
Code:
for key, value in pairs(db.default.orb["HEALTH"]) do |
Ah damn. I think I tried to iterate over the table length. I think I mixed sth up when using # table.
The loop Haleth posted actually does work. Thanks. *edit* Thanks a bunch it is working now! :) |
You can use either pairs or next to loop over a dictionary table.
Lua only returns a length for numerically indexed tables (or at least the highest consecutive index). |
I love it when I go down another path almost to completion to avoid a problem I didn't have when I'm coding. Always fun :).
|
Just incase you missed it ... CopyTable is a global function since quite some time now
Lua Code:
http://wowprogramming.com/utils/xmlb...L/UIParent.lua |
Oh, interesting. There's quite a few useful function in UIParent.lua. However I noticed this...
Code:
if ( tempName == "LeftButton" ) then |
Well, if Button4 ever changes to RightRightButton, we'll be ready.
|
All times are GMT -6. The time now is 05:34 AM. |
vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI