Thread Tools Display Modes
11-30-10, 02:08 PM   #1
Rainrider
A Firelord
AddOn Author - Click to view addons
Join Date: Nov 2008
Posts: 454
Meta methods

Hello,

I've written a small addon for tracking reputation changes as an exercise for learning lua. It is basically a rewirte of Nireks' Rep v2, but it also tracks the total amount of rep gained in a dungeon. However there seems to be an issue with printing the table containing the rep changes and I'm unable to track the problem down. Strangely enough the report slash command, which itterates thorugh the table, functions as expected but the __tostring meta method always prints the table address and not the keys and the corresponding values. Any help on this would be very appreciated.

You could preview the code at https://github.com/Rainrider/rainRep

Thank you very much in advanse
Rain
  Reply With Quote
12-01-10, 05:56 PM   #2
Dorwido
A Wyrmkin Dreamwalker
AddOn Author - Click to view addons
Join Date: Apr 2006
Posts: 54
installed your addon, pseudo filled the instancegainlist and used the report function with only rainRep:Print(rainRepDB.instanceGainList), commented the loop out and it gave me faction-number not the table pointer
__________________
Auction Analytics
http://www.wowauction.org/
  Reply With Quote
12-02-10, 03:35 AM   #3
Rainrider
A Firelord
AddOn Author - Click to view addons
Join Date: Nov 2008
Posts: 454
The addon should print instanceGainList on PLAYER_ENTERING_WORLD when the previous zone was a dungeon and the current is not or is a different dungeon. The problem is that __tostring does not even get called, which I can't explain. When I try "/rrep db" after leaving the dungeon it has the following output:

rainRep: debug: meta print

instanceGainList: table: 17DE2908
currLoc: world
prevName: Grundak
playerWasDead: false
currName: Eastern Kingdoms
prevLoc: instance

instanceGainList is a table within the table containing the SavedVariables. Both tables get metaPrint as metatable at ADDON_LOADED.

Everything functions as expected on intial login and subsequent /console reloadui, but not when I leave a dungeon. It appears that instanceGainList doesn't get its meta table set and I fail to understand the reason for it.
  Reply With Quote
12-02-10, 09:31 AM   #4
Dorwido
A Wyrmkin Dreamwalker
AddOn Author - Click to view addons
Join Date: Apr 2006
Posts: 54
rainRepDB.instanceGainList = {}

thats your problem, this is kinda creating a new table and so you loose your metatable as well, just use

wipe(rainRepDB.instanceGainList)

this only clears the content of your table and keeps the metatable intact.
__________________
Auction Analytics
http://www.wowauction.org/
  Reply With Quote
12-03-10, 08:58 AM   #5
Rainrider
A Firelord
AddOn Author - Click to view addons
Join Date: Nov 2008
Posts: 454
You are right, this solves the problem. wipe(tbl) is a blizz function though, which makes it kinda strange that a language like lua, based mainly around tables, does behave like this. This means that I have to reattach the meta tables in my reset slash command too.

Thank you, your help is much appreciated
Cheers
Rain
  Reply With Quote
12-03-10, 12:14 PM   #6
Dorwido
A Wyrmkin Dreamwalker
AddOn Author - Click to view addons
Join Date: Apr 2006
Posts: 54
well it would be weird if you create a new table and a metatable of an old table would be still there then it wouldnt be a new one :P
__________________
Auction Analytics
http://www.wowauction.org/
  Reply With Quote
12-03-10, 03:31 PM   #7
Rainrider
A Firelord
AddOn Author - Click to view addons
Join Date: Nov 2008
Posts: 454
I don't know if I'm right, but tbl = {} is the standart lua way to empty a table, isn't it. If so, then I find it strange that I have to reattach the meta tables. And if I unterstand this correctly, I'll have to do this too if I reset a given table to a table containing default values like tbl = default. I'm still learning the languange, please excuse me if I'm talking crap
  Reply With Quote
12-03-10, 03:48 PM   #8
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
Featured
Join Date: Oct 2006
Posts: 10,860
No, tbl = {} creates a brand new table (with a new reference in memory) and assigns it to your variable tbl. The previous table you were using is no longer assigned to tbl. Since it is not referenced by a variable, it goes on the heap to be garbage collected.

If you're doing this often, it creates tons of extra, new tables, and makes the garbage collector churn repeatedly to get rid of the old ones.

Best bet is to wipe the first one and reuse it.


/edit: here, see this example:
Code:
> tbl = {}
No output
> tbl.a = "one"
No output
> tbl.b = "two"
No output
> print(tbl)
table: 0x932ed18
> tbl = {}
No output
> print(tbl)
table: 0x82c0c88
You can use this to see for yourself: http://wowprogramming.com/utils/weblua

/edit-edit: you're right, that normally we would do tbl = {}, but Blizzard added the wipe() function for the purpose of eliminating the garbage.
__________________
"You'd be surprised how many people violate this simple principle every day of their lives and try to fit square pegs into round holes, ignoring the clear reality that Things Are As They Are." -Benjamin Hoff, The Tao of Pooh


Last edited by Seerah : 12-03-10 at 03:53 PM.
  Reply With Quote
12-04-10, 07:38 AM   #9
Rainrider
A Firelord
AddOn Author - Click to view addons
Join Date: Nov 2008
Posts: 454
I understand that. I just wonder why there is no wipe function in stadard lua and how would one go about it trying to minimize garbage collection. Is there a way to see blizzards implementation of wipe()?

EDIT: Seerah, I think I'm starting to make the impression I'm one of those people Benjamin Hoff talks about

Last edited by Rainrider : 12-04-10 at 07:41 AM.
  Reply With Quote
12-04-10, 01:28 PM   #10
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
Featured
Join Date: Oct 2006
Posts: 10,860
iirc, that function is defined C-side.
__________________
"You'd be surprised how many people violate this simple principle every day of their lives and try to fit square pegs into round holes, ignoring the clear reality that Things Are As They Are." -Benjamin Hoff, The Tao of Pooh

  Reply With Quote
12-05-10, 09:40 AM   #11
Dorwido
A Wyrmkin Dreamwalker
AddOn Author - Click to view addons
Join Date: Apr 2006
Posts: 54
That is prolly nothing else then iterating through all the data in the table and delete it and stuff which have variable length has always garbage.

People think anyway way too much about cpu,memory etc...wondering when they write own programs if they use then assembler to write something.
__________________
Auction Analytics
http://www.wowauction.org/
  Reply With Quote

WoWInterface » Developer Discussions » General Authoring Discussion » Meta methods


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