Thread Tools Display Modes
02/05/16, 09:51 AM   #1
sirinsidiator
 
sirinsidiator's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 1,578
Getting back the "class" of a ZO_Object subclass instance

Oh great Lua guru, I summon thee, please heed my call!

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(...)
  Reply With Quote
02/05/16, 10:36 AM   #2
TinfoilPancakes
AddOn Author - Click to view addons
Join Date: Jan 2016
Posts: 2
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?
  Reply With Quote
02/05/16, 11:53 AM   #3
haggen
 
haggen's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2015
Posts: 137
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.
  Reply With Quote
02/05/16, 12:02 PM   #4
sirinsidiator
 
sirinsidiator's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 1,578
Thank you very much. Got it to work like I want now.
  Reply With Quote
02/05/16, 07:26 PM   #5
merlight
AddOn Author - Click to view addons
Join Date: Jul 2014
Posts: 671
Originally Posted by sirinsidiator View Post
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
  Reply With Quote

ESOUI » Developer Discussions » Lua/XML Help » Getting back the "class" of a ZO_Object subclass instance


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