Instance methods and variables don't work - Metatable code faulty?
Hey!
This is my first OOP test with Lua and I'm a beginner so please be easy on me with bad, ugly code. I'm mainly trying to test functionality in order to understand everything. For my OOP practice I made a class (prototype) that can display font strings of units and their different values. Like level, name, HP, power of the specified units. The problem I have is that none of my methods or variables are being inherited by the instance object. "targetNameTest" does not inherit from its class "UnitFontString". My test method call and test instance variable print give me a Lua error and 'nil'. Thanks for the help Code:
-- [[ CLASS: Unit Font String ]] |
You are making it way more complicated than it needs to be, however your error is a simple fix:
Code:
self._index = self Code:
self.__index = self |
My god what a stupid mistake.
Thanks a lot for that though! By complicated what do you mean specifically? A few things in there I wrote simply for syntax practice, but the whole thing is probably way too big, huh? |
Quote:
* The maximum font size is specific to the font file you're using, and is closer to 32 for most fonts, but can be much lower for some. It is never higher than 36. Here's how I would write the same thing, in a more straightforward and more easily extensible way: Code:
local NewUnitFontString Code:
local targetName = NewUnitFontString("target", "name") |
Reading alternative code like that really helps. I have a few questions:
1. The pairs() function. I don't quite understand it. Earlier you declare 'events' to be the result of the 'displayEvents[displayType]' table access. Where does 'unitSpecific' come from and what do 'event' and 'unitSpecific' do exactly in 'events'? Code:
for event, unitSpecific in pairs(events) do Code:
local displayEvents = { Code:
SetText(hp and hp > 0 and hp or nil) How would the handler function ever receive the third parameter 'unit' directly though? When would '(unit or self.unit)' ever resort to using 'unit'? Code:
health = function(self, event, unit) |
1: The pairs function returns the key/value pairs from a table. The "unitSpecific" refers to a variable which is holding the value for that particular key/value entry in the loop. It could very well have been named "flufferNutter."
2: The value for the table is set to "true" simply to give it a value to iterate over. When making a table, if the key is a non-space-separated string, the quotation marks and brackets are optional. 3: The expression Code:
SetText(hp and hp > 0 and hp or nil) 4: I'll let someone else answer this one, as I've not been reading this thread to be familiar with the posted code. |
I found Phanx's explanation interesting myself and have dabbled with that side of things today to see how it all worked as I've never used tables to hold event and handler functions before so handy to know how they can be used. Here follows my understanding of how it all works. I'm sure others will be able to offer better explanations but here goes anyway.
1. When you use the pairs function you are asking it to grab a key,value pair from the table listed. Lua Code:
In this example the keys are health and name and the values are the values after the '=' in otherwords the table of events and whether they are unitSpecific ( true/false). As far as I am aware these could be written as : displayEvents[health][UNIT_HEALTH] = true displayEvents["health"]["UNIT_HEALTH"] = true displayEvents.health["UNIT_HEALTH"] = true displayEvents.health.unit_health = true However if health or UNIT_HEALTH had been previously set to other values such as health = 123. Then displayEvents[health] may ( I think ) be translated as displayEvents[123] but I could be wrong. From what I understand words do not need to be contained in speech marks as it is automatically treated as a string. edit: Torhal's explanation was a might bit better than mine. Lua Code:
In this example we are asking the addon to look at the events list for selected key earlier requested and check to see if the event ( key ) is a unitSpecific event ( value ). Dependant on that result will be whether it registers the unit event or the regular event. 2. Is answered in the above explanation. 3. I am not sure myself why the hp is queried twice but the hp and hp > 0 is just making sure that there is a hp value and if there is a value that it is more than 0. 4. You are correct in that statement. The frame is passed to the event handler and if the event passes the unit to the handler itself it will use that, otherwise it will use the one that was stored when creating the frame. I hope that helps you understand things a bit more. The wowpedia website has a link to the lua functions wow uses and a link to the lua manual itself so that you can research some more on that subject. |
Quote:
Line 1 is using the values of the variables health and UNIT_HEALTH as keys instead of the strings health and UNIT_HEALTH like line 2 and 3. Line 4 is using the string unit_health as a key instead of UNIT_HEALTH. Lua is case-sensitive, so these are two completely separate keys. If it was in uppercase, it would be the same as lines 2 and 3. Quote:
|
Thanks for clarifying Choonster ..
line 4 I did mess up, forgot to put caps on the UNIT_HEALTH part. line 1 I thought was the same as ["health"]["UNIT_HEALTH"] if, and only if, there is no variable set with those names but it does make sense that it wouldn't assume they were text values. I always use the speech marks just to avoid the confusion. |
Quote:
Code:
local myTable = { Code:
local class = UnitClass("player") |
All times are GMT -6. The time now is 02:17 AM. |
vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI