ESOUI

ESOUI (https://www.esoui.com/forums/index.php)
-   Lua/XML Help (https://www.esoui.com/forums/forumdisplay.php?f=175)
-   -   Getting back the "class" of a ZO_Object subclass instance (https://www.esoui.com/forums/showthread.php?t=6070)

sirinsidiator 02/05/16 09:51 AM

Getting back the "class" of a ZO_Object subclass instance
 
Oh great Lua guru, I summon thee, please heed my call! :D

I am in need of some great Lua magic.

I have two questions.
Is it possible to access local variables from a different file that are not exposed in any way? Probably not, but I figured I'd just ask anyway.
UNITFRAME_BAR_STYLES in unitframes.lua does not have any way to access it indirectly.

And is it possible to get the "class" of a ZO_Object subclass back from an instance and create new objects of that type?
Or more specifically UnitFrameBar.
I can for example access an instance of that class with the following code:

Lua Code:
  1. local group1 = ZO_UnitFrames_GetUnitFrame("group1")
  2. group1.healthBar -- this is a UnitFrameBar
  3. -- some magic needs to happen here to get UnitFrameBar
  4. local myUnitFrameBar = UnitFrameBar:New(...)

TinfoilPancakes 02/05/16 10:36 AM

As to your first question, unfortunately no :( local keeps it within it's own scope wether it is file or function or whatever. You could however define a function in global namespace that can access it for you. The data is still there just restricted. I'm not entirely sure if that is what you were hoping for :/

As to the second question: I'm not sure you can subclass directly like that but I thought there was a way to define a control that inherits from another control? Would that work?

haggen 02/05/16 11:53 AM

Very easily. The Subclass method that ZOS have implemented is nothing but a pretty way to update the meta table of a table, so you have acess to that class just by using the 'getmetatable' method.

Incidentally I had to access the same class you're looking for right now, and here's how I've done it:

Lua Code:
  1. local sampleUnitFrame = ZO_UnitFrames_GetUnitFrame("reticleover")
  2. UnitFrame = getmetatable(sampleUnitFrame)
  3. UnitFrameBar = getmetatable(sampleUnitFrame.healthBar)

Please note that changing methods of the class only affect its instances if they haven't overridden said method.

About UNITFRAME_BAR_STYLES you've answered it yourself, you can't change that. But I have been in situations where I had the same need you have now. A nice example would be this one, from the same add-on I took the first snippet:

Lua Code:
  1. local function ImproveTargetUnitFrame()
  2.     local unitFrame = ZO_UnitFrames_GetUnitFrame("reticleover")
  3.  
  4.     local healthBar = unitFrame.healthBar
  5.     local healthBarControl = healthBar.barControls[1]
  6.  
  7.     local healthBarLabel = CreateControlFromVirtual(healthBarControl:GetName().."SiufLabel", healthBarControl, "SiufLabel")
  8.     healthBarLabel:SetText(GetBarText(healthBar.currentValue, healthBar.maxValue))
  9.     healthBarLabel:SetFont("SiufLabelFont")
  10.     healthBarLabel:SetAnchor(CENTER, nil, RIGHT, 0, 0)
  11.  
  12.     local previousValue
  13.  
  14.     function unitFrame:RefreshUnit(unitChanged)
  15.         UnitFrame.RefreshUnit(self, unitChanged)
  16.         if unitChanged then
  17.             previousValue = nil
  18.         end
  19.     end
  20.  
  21.     function healthBar:UpdateText(...)
  22.         -- UnitFrameBar.UpdateText(self, ...)
  23.         healthBarLabel:SetText(GetBarText(self.currentValue, self.maxValue, previousValue))
  24.         previousValue = self.currentValue
  25.     end
  26. end

You can see above how I override some methods of the instance, and call the same method but now from the class to keep the same functionality without having to write it myself.

sirinsidiator 02/05/16 12:02 PM

Thank you very much. Got it to work like I want now.

merlight 02/05/16 07:26 PM

Quote:

Originally Posted by sirinsidiator (Post 25888)
Is it possible to access local variables from a different file that are not exposed in any way? Probably not, but I figured I'd just ask anyway.
UNITFRAME_BAR_STYLES in unitframes.lua does not have any way to access it indirectly.

Not in ESO, because the debug module is not there: http://www.lua.org/manual/5.1/manual...bug.getupvalue

Some locals are accessible by a trick I used when add-on manager (control wrapper) was local in some func -- I hooked one of its class methods, and when it was called, stored self. Basically if the local is passed to some globally accessible function, and you can force (or wait for) the surrounding code to call that function with the local, you can get it ;)


All times are GMT -6. The time now is 12:39 AM.

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