View Single Post
01-24-14, 10:47 PM   #1
Jonisaurus
An Aku'mai Servant
 
Jonisaurus's Avatar
Join Date: Aug 2011
Posts: 35
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 ]]

local UnitFontString = {};
UnitFontString.alpha = 0.5;
UnitFontString.frameStrata = "BACKGROUND";
UnitFontString.width = 228;
UnitFontString.height = 64;
UnitFontString.point = {"CENTER", 0, 0};
UnitFontString.font = {"Fonts\\FRIZQT__.TTF", 220, "THICKOUTLINE"};

-- Constructor function
function UnitFontString.new(self, displayType, unitID, event1, event2, event3)
    local o = {};
    setmetatable(o, self);
    self._index = self;
    
    o.frame = CreateFrame("Frame", nil, UIParent);
    o.frame:SetFrameStrata(self.frameStrata);
    o.frame:SetWidth(self.width);
    o.frame:SetHeight(self.height);
    o.frame:SetPoint(self.point[1], self.point[2], self.point[3]);
    o.frame:Hide();
    
    o.fontString = o.frame:CreateFontString();
    o.fontString:SetFont(self.font[1], self.font[2], self.font[3]);
    o.fontString:SetAllPoints(o.frame);
    
    -- register events according to number of event parameters passed
    if event1 and event2 and event3 then
        o.frame:RegisterEvent(event1);
        o.frame:RegisterEvent(event2);
        o.frame:RegisterEvent(event3); 
    elseif event1 and event2 then
        o.frame:RegisterEvent(event1);
        o.frame:RegisterEvent(event2);
    elseif event1 then
        o.frame:RegisterEvent(event1);            
    -- if no event parameter was passed then return nothing for construction
    else
        return;
    end
    
    -- set up event handler function according to displayType and unitID parameters (purpose and unit)
    if unitID and displayType == "name" then
        function o.eventHandler(self, event, ...)
            o.frame:Hide();
            o.fontString:SetText(UnitName(unitID));
            o.frame:Show();
        end
    elseif unitID and displayType == "level" then
        function o.eventHandler(self, event, ...)
            o.frame:Hide();
            o.fontString:SetText(UnitLevel(unitID));
            o.frame:Show();
        end
    elseif unitID and displayType == "hp" then
        function o.eventHandler(self, event, ...)
            o.frame:Hide();
            o.fontString:SetText(UnitHealth(unitID));
            o.frame:Show();
        end
    elseif unitID and displayType == "power" then
        function o.eventHandler(self, event, ...)
            o.frame:Hide();
            o.fontString:SetText(UnitPower(unitID));
            o.frame:Show();
        end
    -- if no displayType or unitID parameter was passed then return nothing
    elseif not unitID or not displayType then
        return;
    end
    
    o.frame:SetScript("OnEvent", o.eventHandler);
    return o;
end


--[[ INSTANCE METHODS ]]

function UnitFontString.setAlpha(self, v)
    self.alpha = v;
    self.frame:SetAlpha(v);
end

function UnitFontString.setFrameStrata(self, v)
    self.frameStrata = v;
    self.frame:SetFrameStrata(v);
end

[...] [SHORTENED FOR FORUM]

-- [[ CLASS declaration END ]]


-- construct test object, print instance variable and call method
targetNameTest = UnitFontString:new("name", "target", "PLAYER_TARGET_CHANGED");
print(targetNameTest.width);
targetNameTest:setAlpha(0.5);

Last edited by Jonisaurus : 01-24-14 at 11:17 PM.
  Reply With Quote