1. We have added a Gift Upgrades feature that allows you to gift an account upgrade to another member, just in time for the holiday season. You can see the gift option when going to the Account Upgrades screen, or on any user profile screen.
    Dismiss Notice

Remove button from Action

Discussion in 'Mod Creation Help' started by americanslon, Apr 3, 2019.

Tags:
  1. americanslon

    americanslon Chieftain

    Joined:
    Apr 3, 2019
    Messages:
    14
    Gender:
    Male
    I making a mod where a button should show up based on the type of unit is selected.
    I "borrowed" some code to display the button from another mod:

    If correct unit is detected it calls two functions

    First:
    Code:
    function CreateHurryProductionButton()
        hurryProductionIM:DestroyInstances();
    
        local instance = hurryProductionIM:GetInstance();
    
        instance.UnitActionIcon:SetIcon("ICON_UNITCOMMAND_WONDER_PRODUCTION");
        instance.UnitActionButton:SetDisabled((not isHurryable));
        instance.UnitActionButton:SetAlpha((not isHurryable and 0.4) or 1);
        instance.UnitActionButton:SetToolTipString("Hurry Production");
        instance.UnitActionButton:RegisterCallback(Mouse.eLClick,
            function(void1, void2)      
                if isHurryable then             
                    HurryProduction();
                end
            end
        );
    end
    
    and then
    Code:
    function AttachHurryButtonToUnitPanelActions ()
        local SecondaryActionsStack = ContextPtr:LookUpControl("/InGame/UnitPanel/SecondaryActionsStack");
         
        Controls.HurryProductionSecondaryActionsStack:ChangeParent(SecondaryActionsStack);
     
        if SecondaryActionsStack ~= nil then
            SecondaryActionsStack:AddChildAtIndex(Controls.HurryProductionSecondaryActionsStack, 0);
            SecondaryActionsStack:CalculateSize();
            SecondaryActionsStack:ReprocessAnchoring();
        end 
    end
    
    Accompanying XML
    Code:
    <Context Name="HurryProduction" >
    
        <Stack ID="HurryProductionSecondaryActionsStack" Anchor="C,B" StackGrowth="Right" Padding="2" ConsumeMouse="1" />
    
        <Instance Name="UnitActionInstance" >
            <Button    ID="UnitActionButton" Anchor="L,T" Size="44,53" Texture="UnitPanel_ActionButton">
                <Image ID="UnitActionIcon" Anchor="C,C" Offset="0,-2" Size="38,38" Texture="UnitActions"/>
            </Button>
        </Instance>
    
    </Context>
    
    I sorta get what this is doing because I can see the result. But don't get it enough to figure out how to remove the button if I shift focus to another unit that shouldn't have the button.
     
    Last edited: Apr 3, 2019
  2. americanslon

    americanslon Chieftain

    Joined:
    Apr 3, 2019
    Messages:
    14
    Gender:
    Male
    If anyone needs this later this is how I did it.

    Code:
    function DetachHurryButtonToUnitPanelActions()
    	local actionsStack = ContextPtr:LookUpControl("/InGame/UnitPanel/SecondaryActionsStack");
    
    	if actionsStack ~= nil then
    		actionsStack:DestroyChild(Controls.HurryProductionActionsStack);
    	end	
    end
    
     
    Infixo likes this.

Share This Page