Thread Tools Display Modes
05/15/14, 06:17 AM   #1
Edda
 
Edda's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 60
Saved var size always 0

Hi,

Facing a strange issue here but first the code :

Lua Code:
  1. RJ.Memory = {}
  2. RJ.Memory = ZO_SavedVars:New("RJMem", math.floor(RJ.varVersion), "Memory", RJ.Memory, nil);
  3.  
  4. --add some stuff to RJ.Memory
  5.  
  6. d(#RJ.Memory); --> ALWAYS 0 !!

RJ.Memory size is always 0 even tho I add stuff to it. And the savedVars file correctly displays saved data, and everything is working correctly but not the var size check.

What I am trying to do is to display the RJ.Memory content (if size > 0) but it seems RJ.Memory is hooked to the top of the the saved var tree rather than the "Memory" namespace (notice I used a "Memory" namespace because I use another "Options" namespace in my saved vars).

Full code can be found here : http://www.esoui.com/downloads/info4...emberJunk.html

What I am obviously trying to loop thru the "Memory" table.
  Reply With Quote
05/15/14, 06:32 AM   #2
Wobin
 
Wobin's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 78
#table only works for consecutively assigned numerical indexes, so t = {"a", "b", "c"} or t={[1] = "a", [2] = "b", [3] = "c"}

As you're using itemIds as the index, you're ending up with nonconsecutive elements. Use pairs(t) to iterate through and count elements

Code:
local count = 0
for k in pairs(t) do count = count + 1 end

Last edited by Wobin : 05/15/14 at 06:35 AM.
  Reply With Quote
05/15/14, 06:38 AM   #3
Edda
 
Edda's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 60
Originally Posted by Wobin View Post
#table only works for consecutively assigned numerical indexes, so t = {"a", "b", "c"} or t={[1] = "a", [2] = "b", [3] = "c"}

As you're using itemIds as the index, you're ending up with nonconsecutive elements. Use pairs(t) to iterate through and count elements

Code:
local count = 0
for k in pairs(t) do count = count + 1 end
Hmm ok I see. Will have to create my own count() func then

Will try this and see how it works. Thanks !
  Reply With Quote
05/15/14, 07:20 AM   #4
Garkin
 
Garkin's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 832
SavedVariables are bit tricky. It is not a simple table, it uses metatable and some default values.
In your case there will be always added key "version", so you have to be sure that you exclude this key from your count.
By the way if you want to get just values that are really present in table and not values returned from metatable use rawget(table, key).

EDIT:
If you want to make it simple, add subtable to your saved variables. Do not store items as RJ.Memory[itemData.itemId], but use something like RJ.Memory.data[itemData.itemId]. It will save you lots of troubles.

Last edited by Garkin : 05/15/14 at 07:24 AM.
  Reply With Quote
05/15/14, 07:46 AM   #5
BadVolt
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 74
I had similar problem some time ago. I guess, you are initializing your addon from XML?
  Reply With Quote
05/15/14, 09:02 AM   #6
Edda
 
Edda's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 60
Originally Posted by Garkin View Post
SavedVariables are bit tricky. It is not a simple table, it uses metatable and some default values.
In your case there will be always added key "version", so you have to be sure that you exclude this key from your count.
By the way if you want to get just values that are really present in table and not values returned from metatable use rawget(table, key).

EDIT:
If you want to make it simple, add subtable to your saved variables. Do not store items as RJ.Memory[itemData.itemId], but use something like RJ.Memory.data[itemData.itemId]. It will save you lots of troubles.
Hmm ok. I dont remember if the 'version' key is at the same level as namespaces (when using namespace), but I think it is (hence you shouldn't be needing to 'skip' this key). Will check it this evening.

About my way of creating the saved variable :

RJ.Memory["foo"] = "bar" works fine (creates key/value in saved variable)
RJ.Memory["foo"] = nil works fine (deletes key/value in saved variables)

Things only screw when I do i.e.

for k, v in RJ.Memory do d(k .. " : " .. v) end

Suddenly it seems I am not handling with the same variable anymore !

It sucks because I liked the idea of namespaces

*** EDIT ***

rawget(table, key) : I really don't see the point : example plz

Last edited by Edda : 05/15/14 at 09:09 AM.
  Reply With Quote
05/15/14, 09:08 AM   #7
Garkin
 
Garkin's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 832
Originally Posted by Edda View Post
Hmm ok. I dont remember if the 'version' key is at the same level as namespaces (when using namespace), but I think it is (hence you shouldn't be needing to 'skip' this key). Will check it this evening.

About my way of creating the saved variable :

RJ.Memory["foo"] = "bar" works fine (creates key/value in saved variable)
RJ.Memory["foo"] = nil works fine (deletes key/value in saved variables)

Things only screw when I do i.e.

for k, v in RJ.Memory do d(k .. " : " .. v) end

Suddenly it seems I am not handling with the same variable anymore !

It sucks because I liked the idea of namespaces

*** EDIT ***

rawget(table, key) >FOR> table[key] is another table ???
Ah, I did not try how it works with namespace, I have to check it.

In your code above is missing pairs:
Lua Code:
  1. for k, v in pairs(RJ.Memory) do d(k .. " : " .. v) end
  Reply With Quote
05/15/14, 09:11 AM   #8
Edda
 
Edda's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 60
Originally Posted by Garkin View Post
In your code above is missing pairs:
Lua Code:
  1. for k, v in pairs(RJ.Memory) do d(k .. " : " .. v) end
Yeah whatever (just typed it quick here - not pasted the actual code).

Point is :

Lua Code:
  1. for k, v in pairs(RJ.Memory) do d(k .. " : " .. v) end

Doesn't return RJ.Memory pairs.

Will retest it this evening.
  Reply With Quote
05/15/14, 09:20 AM   #9
Roupine
Join Date: May 2014
Posts: 18
When is this function running? Calling d() too soon after a reloadui won't do anything as the chat windows aren't initialized
  Reply With Quote
05/15/14, 09:20 AM   #10
Garkin
 
Garkin's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 832
Originally Posted by Edda View Post
rawget(table, key) : I really don't see the point : example plz
Metatables magic
http://www.youtube.com/watch?v=CYxMfVy5W00

http://nova-fusion.com/2011/06/30/lu...bles-tutorial/

EDIT (example):
Lets set simple empty table called "t" with metatable "mt":
lua Code:
  1. local defaults = { a = "hello world" }
  2. local mt = { __index = defaults }
  3. local t = {}
  4.  
  5. setmetatable(t, mt)
Now if you try:
lua Code:
  1. d(t["a"])
It will print "hello world".
Key "a" is not present in table "t", but because we have defined metamethod __index in metatable, you will get value from "defaults" table.

If you try:
lua Code:
  1. d(rawget(t, a))
It will print "nil", because rawget returns the real value without invoking any metamethod.

Last edited by Garkin : 05/15/14 at 09:43 AM.
  Reply With Quote
05/15/14, 09:22 AM   #11
Edda
 
Edda's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 60
Originally Posted by Garkin View Post
Haha will check that. Is that you ?
  Reply With Quote
05/15/14, 09:24 AM   #12
Edda
 
Edda's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 60
Originally Posted by Roupine View Post
When is this function running? Calling d() too soon after a reloadui won't do anything as the chat windows aren't initialized
d() prints correctly, just not expected values. I'm calling the 'list memory' func from a slash command (so way after UI is loaded).
  Reply With Quote
05/15/14, 09:39 AM   #13
Roupine
Join Date: May 2014
Posts: 18
Hmm. The only thing I can think of without being able to get into the game and look into it myself would be using your memory table as its own default table. That's a wild guess though.
  Reply With Quote
05/15/14, 12:27 PM   #14
Edda
 
Edda's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 60
Originally Posted by Roupine View Post
Hmm. The only thing I can think of without being able to get into the game and look into it myself would be using your memory table as its own default table. That's a wild guess though.
Instancing memory with itself as default value just instances it as '{}' if no memory is found on the user's local machine. I like doing it this way.

Another example. Instancing Addon.Options with Addon.Options as default value gives all the default values stored in Addon.Options to... itself (so Addon.Options == Addon.Options default value given no data is found locally - and option default values are written locally). If data is found locally then Addon.Options gets rewritten with local values. Saves you the hassle from read/write functions to user's saved variable i.e. if user changes an option then the addon bears with it AND it's saved locally. You know what I mean
  Reply With Quote
05/15/14, 12:45 PM   #15
Edda
 
Edda's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 60
@Garkin

I was wrong

'Version' value is popped in every single namespace...
  Reply With Quote
05/18/14, 10:52 AM   #16
zgrssd
AddOn Author - Click to view addons
Join Date: May 2014
Posts: 280
Originally Posted by Edda View Post
d(#RJ.Memory); --> ALWAYS 0 !![/highlight]

RJ.Memory size is always 0 even tho I add stuff to it. And the savedVars file correctly displays saved data, and everything is working correctly but not the var size check.
# does not work on the "top" layer of int indexes for the Settings Object itself (propably because they work via metatables and the "index not found" action). The trick is to just create a string index and store your table one index level "deeper" (taken from one of my addons):
Code:
--Generate default CCD, then try to load saved Container from disk
	local defaultCCD = {}
	defaultCCD.CCD = getCCD(1,TopContainerIndex)
	PER_GL_CCD = ZO_SavedVars:NewAccountWide("UTC_Persistent", 1, "UCT_ChatContainerData", defaultCCD, nil)
I can itterate over defaultCCD.CCD as if it was any other table from any other source and usually jsut hand/assign it directly.
In fact the only times I use PER_GL_CCD at all is the get/set the String Index "CCD".

When is this function running? Calling d() too soon after a reloadui won't do anything as the chat windows aren't initialized
One of libDebugs functions is to buffer those "early d() messages", to give them out when the chat is ready.
Note the bug with swallowed .lua parsing errors however (as long as it is not fixed).
  Reply With Quote

ESOUI » Developer Discussions » Lua/XML Help » Saved var size always 0

Thread Tools
Display Modes

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