ESOUI

ESOUI (https://www.esoui.com/forums/index.php)
-   Lua/XML Help (https://www.esoui.com/forums/forumdisplay.php?f=175)
-   -   Saved var size always 0 (https://www.esoui.com/forums/showthread.php?t=1511)

Edda 05/15/14 06:17 AM

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.

Wobin 05/15/14 06:32 AM

#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


Edda 05/15/14 06:38 AM

Quote:

Originally Posted by Wobin (Post 7797)
#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 !

Garkin 05/15/14 07:20 AM

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.

BadVolt 05/15/14 07:46 AM

I had similar problem some time ago. I guess, you are initializing your addon from XML?

Edda 05/15/14 09:02 AM

Quote:

Originally Posted by Garkin (Post 7802)
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 ! :confused:

It sucks because I liked the idea of namespaces :(

*** EDIT ***

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

Garkin 05/15/14 09:08 AM

Quote:

Originally Posted by Edda (Post 7807)
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 ! :confused:

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

Edda 05/15/14 09:11 AM

Quote:

Originally Posted by Garkin (Post 7808)
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.

Roupine 05/15/14 09:20 AM

When is this function running? Calling d() too soon after a reloadui won't do anything as the chat windows aren't initialized

Garkin 05/15/14 09:20 AM

Quote:

Originally Posted by Edda (Post 7807)
rawget(table, key) : I really don't see the point : example plz :D

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.

Edda 05/15/14 09:22 AM

Quote:

Originally Posted by Garkin (Post 7811)

Haha will check that. Is that you ? :D

Edda 05/15/14 09:24 AM

Quote:

Originally Posted by Roupine (Post 7810)
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).

Roupine 05/15/14 09:39 AM

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.

Edda 05/15/14 12:27 PM

Quote:

Originally Posted by Roupine (Post 7815)
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 :D

Edda 05/15/14 12:45 PM

@Garkin
 
I was wrong :(

'Version' value is popped in every single namespace...

zgrssd 05/18/14 10:52 AM

Quote:

Originally Posted by Edda (Post 7796)
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".

Quote:

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).


All times are GMT -6. The time now is 10:20 AM.

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