ReplaceUIScript UnitPanel not having any effect

LS97

Chieftain
Joined
Dec 12, 2022
Messages
1
I'm trying to add an action button to the unit panel defined in UnitPanel.lua. I have copied over the example from the BlackDeathScenario, where they use ReplaceUIScript in modinfo to extend the GetUnitActionsTable function (see UnitPanel_BlackDeathScenario.lua). My mod is definitely getting loaded (I can see it through Firetuner) but the lua script that's supposed to run in the UnitPanel context is having no effect. I can't even get any logs from it if I put a print call right at the top of the file. Any ideas on what I'm missing?

I've attached the full compiled mod folder for reference, but here are the main relevant snippets:

modinfo

XML:
    <AddGameplayScripts id="SleepModUnitCommands">
      <File>Scripts/SleepMod_UnitCommands.lua</File>
    </AddGameplayScripts>
    <ReplaceUIScript id="SleepModUnitPanel">
      <Properties>
        <LuaContext>UnitPanel</LuaContext>
        <LuaReplace>UI/SleepMod_UnitPanel.lua</LuaReplace>
      </Properties>
    </ReplaceUIScript>
    <ImportFiles id="SleepModImportFiles">
      <File>Scripts/SleepMod_UnitCommandDefs.lua</File>
      <File>UI/SleepMod_UnitPanel.lua</File>
    </ImportFiles>

SleepMod_UnitPanel.lua

Code:
-- ===========================================================================
--    Unit Panel Replacement/Extension
-- ===========================================================================

include "UnitPanel"
include "SleepMod_UnitCommandDefs"

-- ===========================================================================
--    CACHE BASE FUNCTIONS
-- ===========================================================================
BASE_GetUnitActionsTable = GetUnitActionsTable;

-- ===========================================================================
--    OVERRIDE BASE FUNCTIONS
-- ===========================================================================

function GetUnitActionsTable(pUnit : object)
    local pBaseActionsTable : table = BASE_GetUnitActionsTable(pUnit);

    for sCommandKey, pCommandTable in pairs(m_ModUnitCommands) do
        
            local bVisible : boolean = true;
            if (pCommandTable.IsVisible ~= nil) then
                bVisible = pCommandTable.IsVisible(pUnit);
            end
            if (bVisible) then

                if (pCommandTable.CanUse ~= nil and pCommandTable.CanUse(pUnit) == true) then

                    local bIsDisabled : boolean = false;
                    if (pCommandTable.IsDisabled ~= nil) then
                        bIsDisabled = pCommandTable.IsDisabled(pUnit);
                    end
            
                    local sToolTipString : string = pCommandTable.ToolTipString or "Undefined Unit Command";

                    local pCallback : ifunction = function()
                        local pSelectedUnit = UI.GetHeadSelectedUnit();
                        if (pSelectedUnit == nil) then
                            return;
                        end

                        local tParameters = {};
                        tParameters[UnitCommandTypes.PARAM_NAME] = pCommandTable.EventName or "";
                        UnitManager.RequestCommand(pSelectedUnit, UnitCommandTypes.EXECUTE_SCRIPT, tParameters);
                    end

                    if (bIsDisabled and pCommandTable.DisabledToolTipString ~= nil) then
                        sToolTipString = sToolTipString .. "[NEWLINE][NEWLINE]" .. pCommandTable.DisabledToolTipString;
                    end

                    AddActionToTable(pBaseActionsTable, pCommandTable, bIsDisabled, sToolTipString, UnitCommandTypes.EXECUTE_SCRIPT, pCallback);
                end
            end
        end

    return pBaseActionsTable;
end
 

Attachments

  • NewUnit1.zip
    7.4 KB · Views: 15
I know this is months late, and I don't really understand ModBuddy very well yet, but you might want to take a look at this post. Definitely helped me out a bit with my mod.
 
Annoyingly, the expansions already replace UnitPanel.lua with their own versions (UnitPanel_Expansion1.lua, UnitPanel_Expansion2.lua)

so your mod probably works in vanilla but not with expansions.
 
Top Bottom