WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Lua/XML Help (https://www.wowinterface.com/forums/forumdisplay.php?f=16)
-   -   How can I hooksecurefunc with colon like this: "MainMenuBarMixin:OnShow" (https://www.wowinterface.com/forums/showthread.php?t=56578)

Nikita S. Doroshenko 08-19-18 04:25 PM

How can I hooksecurefunc with colon like this: "MainMenuBarMixin:OnShow"
 
I spend so much time trying to figure it out by myself, but unfortunately no luck.

This is blizard code mainMenuBar.lua:
Code:

local MAINMENU_SLIDETIME = 0.30;
local MAINMENU_GONEYPOS = 130;        --Distance off screen for MainMenuBar to be completely hidden
local MAINMENU_XPOS = 0;
MAIN_MENU_BAR_MARGIN = 75;                -- number of art pixels on one side, used by UIParent_ManageFramePositions. It's not the art's full size, don't care about the gryphon's tail.

MainMenuBarMixin = { };
function MainMenuBarMixin:OnStatusBarsUpdated()
        self:SetPositionForStatusBars();
end

function MainMenuBarMixin:OnLoad()
        self:RegisterEvent("ACTIONBAR_PAGE_CHANGED");
        self:RegisterEvent("CURRENCY_DISPLAY_UPDATE");
        self:RegisterEvent("UNIT_LEVEL");
        self:RegisterEvent("PLAYER_ENTERING_WORLD");
        self:RegisterEvent("TRIAL_STATUS_UPDATE");
        self:RegisterEvent("DISPLAY_SIZE_CHANGED");
        self:RegisterEvent("UI_SCALE_CHANGED");

        CreateFrame("FRAME", "StatusTrackingBarManager", self, "StatusTrackingBarManagerTemplate");
       
        MAX_PLAYER_LEVEL = MAX_PLAYER_LEVEL_TABLE[GetExpansionLevel()];

        self.state = "player";
        MainMenuBarArtFrame.PageNumber:SetText(GetActionBarPage());
        MicroButtonAndBagsBar:SetFrameLevel(self:GetFrameLevel()+2);
end

function MainMenuBarMixin:OnShow()
        UpdateMicroButtonsParent(MainMenuBarArtFrame);
        MoveMicroButtons("BOTTOMLEFT", MicroButtonAndBagsBar, "BOTTOMLEFT", 6, 3, false);
end

function MainMenuBarMixin:SetYOffset(yOffset)
        self.yOffset = yOffset;
end

function MainMenuBarMixin:GetYOffset()
        return self.yOffset;
end

function MainMenuBarMixin:SetPositionForStatusBars()
        MainMenuBar:ClearAllPoints();
        MainMenuBarArtFrame.LeftEndCap:ClearAllPoints();
        MainMenuBarArtFrame.RightEndCap:ClearAllPoints();
        if ( StatusTrackingBarManager:GetNumberVisibleBars() == 2 ) then
                self:SetYOffset(17);
                MainMenuBarArtFrame.LeftEndCap:SetPoint("BOTTOMLEFT", MainMenuBar, -98, -17);
                MainMenuBarArtFrame.RightEndCap:SetPoint("BOTTOMRIGHT", MainMenuBar, 98, -17);
        elseif ( StatusTrackingBarManager:GetNumberVisibleBars() == 1 ) then
                self:SetYOffset(14);
                MainMenuBarArtFrame.LeftEndCap:SetPoint("BOTTOMLEFT", MainMenuBar, -98, -14);
                MainMenuBarArtFrame.RightEndCap:SetPoint("BOTTOMRIGHT", MainMenuBar, 98, -14);
        else
                self:SetYOffset(0);
                MainMenuBarArtFrame.LeftEndCap:SetPoint("BOTTOMLEFT", MainMenuBar, -98, 0);
                MainMenuBarArtFrame.RightEndCap:SetPoint("BOTTOMRIGHT", MainMenuBar, 98, 0);
        end
        if ( IsPlayerInWorld() ) then
                UIParent_ManageFramePositions();
        end
end

I would like to add my code after function MainMenuBarMixin:SetPositionForStatusBars(), so i thought I could just use something like this:
Code:

local function Test()
        print("test")
end
hooksecurefunc(MainMenuBarMixin, "SetPositionForStatusBars", Test);

I tried lots of methods and google alot, the last this I made, came here. Will apreciate any help.

Kanegasi 08-19-18 04:36 PM

Anything with "mixin" in it's name isn't actually used directly, so if you were to hook that function, the hook won't get called. You'll need to hook the actual method on the frame that uses the mixin.

Mixins are a template of methods that frames then inherit. Blizzard gives frames a mixin template in two ways: calling frame = CreateFromMixins(mixin) or in xml using the mixin="" attribute. In this case, MainMenuBar gets its methods from MainMenuBar.xml:

XML Code:
  1. <Frame name="MainMenuBar" enableMouse="true" parent="UIParent" mixin="MainMenuBarMixin">

You should be able to do this:

Lua Code:
  1. hooksecurefunc(MainMenuBar, "SetPositionForStatusBars", Test)

Nikita S. Doroshenko 08-19-18 04:53 PM

Quote:

Originally Posted by Kanegasi (Post 329714)
Anything with "mixin" in it's name isn't actually used directly, so if you were to hook that function, the hook won't get called. You'll need to hook the actual method on the frame that uses the mixin.

Mixins are a template of methods that frames then inherit. Blizzard gives frames a mixin template in two ways: calling frame = CreateFromMixins(mixin) or in xml using the mixin="" attribute. In this case, MainMenuBar gets its methods from MainMenuBar.xml:

XML Code:
  1. <Frame name="MainMenuBar" enableMouse="true" parent="UIParent" mixin="MainMenuBarMixin">

You should be able to do this:

Lua Code:
  1. hooksecurefunc(MainMenuBar, "SetPositionForStatusBars", Test)


Kanegasi you saved my day! Thank you very much for explanation.

I hope this thread will be helpful for all beginners or coming back modders.
Very appreciate your help.

I think it's new thing from Legion maybe, don't remember mixins in Pandaria-Warlords of Draenor.

Ammako 08-19-18 07:39 PM

Quote:

Originally Posted by Kanegasi (Post 329714)
Anything with "mixin" in it's name isn't actually used directly, so if you were to hook that function, the hook won't get called. You'll need to hook the actual method on the frame that uses the mixin.

Mixins are a template of methods that frames then inherit. Blizzard gives frames a mixin template in two ways: calling frame = CreateFromMixins(mixin) or in xml using the mixin="" attribute. In this case, MainMenuBar gets its methods from MainMenuBar.xml:

XML Code:
  1. <Frame name="MainMenuBar" enableMouse="true" parent="UIParent" mixin="MainMenuBarMixin">

You should be able to do this:

Lua Code:
  1. hooksecurefunc(MainMenuBar, "SetPositionForStatusBars", Test)


It's interesting to me that you would say that, because this works for me:

lua Code:
  1. hooksecurefunc(ExpBarMixin, "Update", function(self)
  2.     TextStatusBar_UpdateTextString(self)
  3.     self:ShowText(self)
  4.     self:UpdateCurrentText()
  5. end)

Maybe it depends on the mixin? idk

Gethe 08-19-18 10:35 PM

It depends on 1) when the mixin is created 2) when the function is hooked and 3) when the frame has the mixin applied.

In Ammako's case, ExpBarMixin is created and is applied to a template in the frame xml, before any addon gets loaded. The template however is not used for instantiating a frame until PLAYER_ENTERING_WOLRD fires, which is typically after most addons are loaded. As such, any hooks applied to the mixin before PLAYER_ENTERING_WOLRD fires will propagate the the new frame.

In Nikita's case, MainMenuBarMixin is created and immediately used in the creation of MainMenuBar. This means there is no allowance for any hooks to be made and applied via the mixin.

Nikita S. Doroshenko 08-21-18 06:17 PM

Quote:

Originally Posted by Gethe (Post 329720)
It depends on 1) when the mixin is created 2) when the function is hooked and 3) when the frame has the mixin applied.

In Ammako's case, ExpBarMixin is created and is applied to a template in the frame xml, before any addon gets loaded. The template however is not used for instantiating a frame until PLAYER_ENTERING_WOLRD fires, which is typically after most addons are loaded. As such, any hooks applied to the mixin before PLAYER_ENTERING_WOLRD fires will propagate the the new frame.

In Nikita's case, MainMenuBarMixin is created and immediately used in the creation of MainMenuBar. This means there is no allowance for any hooks to be made and applied via the mixin.

The thing that is confusing me, is hoocsecurefunc sometimes toggle lua errors like "funxtionX is not a function"
For example, I tried to hook some functions like function PlayerTalentFrame_UpdateSpecFrame(self, spec) from Blizzard_TalentUI.lua:

Lua Code:
  1. function PlayerTalentFrame_UpdateSpecFrame(self, spec)
  2.     local playerTalentSpec = GetSpecialization(nil, self.isPet, specs[selectedSpec].talentGroup);
  3.     local shownSpec = spec or playerTalentSpec or 1;
  4.     local numSpecs = GetNumSpecializations(nil, self.isPet);
  5.     local petNotActive = self.isPet and not IsPetActive();
  6.     local sex = self.isPet and UnitSex("pet") or UnitSex("player");
  7.     -- and so on... very large function
  8. end
So I used this code:
Lua Code:
  1. local function Test(self, spec)
  2.     print("test")
  3.     print(self)
  4.     print(spec)
  5. end
  6. hooksecurefunc('PlayerTalentFrame_UpdateSpecFrame', Test)
And got this: Error. hooksecurefunc(): PlayerTalentFrame_UpdateSpecFrame is not a function

At the same time, for example:
Lua Code:
  1. local function Test(self, spec)
  2.     print("test")
  3.     print(self)
  4.     print(spec)
  5. end
  6. hooksecurefunc('SpellBookFrame_UpdateSkillLineTabs', Test)
Works perfectly.

Can't understand why this happens, and will appreciate any advises how to hook functions with "funxtionX is not a function" error.

Ammako 08-21-18 06:24 PM

Quote:

Originally Posted by Nikita S. Doroshenko (Post 329754)
The thing that is confusing me, is hoocsecurefunc sometimes toggle lua errors like "funxtionX is not a function"
For example, I tried to hook some functions like function PlayerTalentFrame_UpdateSpecFrame(self, spec) from Blizzard_TalentUI.lua:

-snip-
So I used this code:
-snip
And got this: Error. hooksecurefunc(): PlayerTalentFrame_UpdateSpecFrame is not a function

At the same time, for example:
-snip
Works perfectly.

Can't understand why this happens, and will appreciate any advises how to hook functions with "funxtionX is not a function" error.

Blizzard_TalentUI is LoadOnDemand, you need to wait for ADDON_LOADED and check for Blizzard_TalentUI, or set your addon to LoadOnDemand and LoadWith Blizzard_TalentUI, depending on your use case.


All times are GMT -6. The time now is 08:07 AM.

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