WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Lua/XML Help (https://www.wowinterface.com/forums/forumdisplay.php?f=16)
-   -   SavedVariables (https://www.wowinterface.com/forums/showthread.php?t=2462)

shouryuu 11-21-05 02:51 PM

SavedVariables
 
Hello everyone, it's your favorite forum troll, Shouryuu!
Well once more I need some help with some code... I've managed to write a neat little add-on but sadly enough, I just cannot manage to save variables bewtween sessions. I've put the table's names in the TOC, saved the TOC, exited the game, reload the game blah blah blah done it all and yet still no variables saved... So I'm looking for a benevolent soul to either try and walk me throught this difficult period, or just take a look at my code (send me a PST if you want the code, I'll send it to you on either mIRC or via e-mail) and see what's wrong!
Thank you very much!
(I'm going to try my luck on the official forums but it's probably going to get drowned in a sea of Nerf Decursive!11one!onoeneoeneleven! threads soooooooo)

DSanai 11-21-05 03:06 PM

In the TOC file, you will add a statement like this:

## SavedVariables: YourMod_Data

In your LUA file, you will want to ACCESS the variables by using a table structure based on that base variable, YourMod_Data.

ANYTHING you assign to that table will be saved AUTOMATICALLY by WoW.

Therefore, don't be using a bunch of other variables, but rather access the SV data and save the data to them.

In your VARIABLES_LOADED event handler, you can do this:

if (not YourMod_Data) then YourMod_Data = {}; end

This will initialize the main table, if it doesn't already exist. After that, you need to decide if you want to save the variables by-character or by-server.

local serverName = GetCVar("realmName");
local charName = UnitName("player");
svName = charName.." of "..serverName; -- This should be a persistant variable, either local to the ENTIRE LUA, or global

if (not YourMod_Data[svName]) then YourMod_Data = { -- Set Defaults Here
["show"] = true,
["something"] = 15,
}; end

Any time they change something (such as the "show" variable, above), change it directly:
YourMod_Data[svName]["show"] = false;

And any time you need the value of it, read it directly:
if (YourMod_Data[svName]["show"]) then

Anything in the table YourMod_Data will automatically save, because the TOC file specifies that table as something TO save.

Also of note, you can now use the line ## SavedVariablesPerCharacter in the TOC file, to save stuff for ONLY that character -- BUT you cannot ever access the settings for any other character if you do so (such as for 'copying' one char's settings to another, like Titan Panel 2 does).

Hope this helps.

shouryuu 11-22-05 12:39 AM

Thanks a lot man! My problem was solved when Iriel told my variables shouldn't be local :P Stupid 'lil 'ol me :D But thanks though, I did learn a few things with your post, keeping it for future reference.


All times are GMT -6. The time now is 11:19 AM.

vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI