Thread Tools Display Modes
04/23/15, 05:16 PM   #1
Phinix
 
Phinix's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 186
Help with global saved vars...

I want to be able to dynamically create keys in a global saved variables table based on character name.

Code:
SomeAddon = {}
SomeAddon.Name = "SomeAddonName"

SomeAddon.GlobalTable = { }

...do stuff...

local function OnAddonLoaded(event, addonName)
	if addonName ~= SomeAddon.Name then return end
	EVENT_MANAGER:UnregisterForEvent("SomeAddon.Name", EVENT_ADD_ON_LOADED)
	SomeAddon.AccountSavedVariables = ZO_SavedVars:NewAccountWide("SomeAddonName", 1, nil, SomeAddon.GlobalTable)
end

EVENT_MANAGER:RegisterForEvent(SomeAddon.Name, EVENT_ADD_ON_LOADED, OnAddonLoaded)
I want to check this table for a key which contains a value that == the current character name, and if not create it. The first value in each key would be the character name, followed by a set of values I pass based on functions.

So for example:

Code:
SomeAddon.GlobalTable[1] = {name= "character", var1 = "0", var2 = "0", var3 = "0"}
I also need to be able to determine the size of this global variable table so that if I have to add a new key for the current character (based on a check if they already exist in the table), I can make the new key tablesize+1.

The trouble I am having is the syntax. Whether it is using for k,v in pairs(AccountSavedVariables.GlobalTable) and a variable + 1 to count the table size, or using the above format to add a key, I am getting "attempt to index a nil value" errors.

Last edited by Phinix : 04/23/15 at 05:27 PM.
  Reply With Quote
04/23/15, 08:12 PM   #2
Ayantir
 
Ayantir's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2014
Posts: 1,019
Hello,

First, you can use

Lua Code:
  1. ZO_SavedVars:New("SomeAddonName", 1, nil, defaultValues)

If you want, it will be a per character setting.

If you don't want, just do :
Lua Code:
  1. local defaultValues = {}
  2. defaultValues.name = GetUnitName("player") -- returns local player name
  3. SomeAddon.AccountSavedVariables = ZO_SavedVars:NewAccountWide("SomeAddonName", 1, nil, defaultValues)
  4.  
  5. d(SomeAddon.AccountSavedVariables.name) -- will print the character name.


In your exemple :

Lua Code:
  1. local defaultValues  = {}
  2. table.insert(defaultValues, {name= GetUnitName("player"), var1 = 0, var2 = 0, var3 = 0})
  3.  
  4. SomeAddon.AccountSavedVariables = ZO_SavedVars:NewAccountWide("SomeAddonName", 1, nil, defaultValues)
  5.  
  6.  
  7.  
  8. for k, v in ipairs(SomeAddon.AccountSavedVariables)
  9.     d(v.name) -- will print your name
  10. end

If SomeAddon.AccountSavedVariables is not created, it will be created using the template defaultValues. If it already exists, the defaultValues won't be used

Last edited by Ayantir : 04/23/15 at 08:17 PM.
  Reply With Quote
04/25/15, 05:35 AM   #3
Phinix
 
Phinix's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 186
Thanks, Ayantir. What I ended up doing in this case was to declare the empty structure of the global database and define it as the object I used as the template for the saved variables reference. After a test reload it is indeed getting the data now.

Just thought you might like to see the finished project. Well, the 1.0 release anyway.

Achievement Collection Info
  Reply With Quote

ESOUI » Developer Discussions » Lua/XML Help » Help with global saved vars...


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