Auto-equip last weapon when Fire1 pressed?

hoodust

Member
How would I make the character equip and attack with the last used weapon when pressing Fire1 from an unequipped state? An item ability maybe?
 
Sorry, you mean the Equip script near the bottom? I was looking at this page but thought I was in the wrong place when I read "Equip and unequip is performed on the Item Set Manager."
 
Also, would `GetActiveCharacterItem(slotID)` return the last used weapon, or would I need to keep track of that in a script variable?
 
Yes, that's correct. You will want to call StartEquipUnequip with the index within the ItemSetManager that you want to equip.

Also, would `GetActiveCharacterItem(slotID)` return the last used weapon, or would I need to keep track of that in a script variable?
GetActiveCharacterItem returns the current item that is equipped, not necessarily used. You can subscribe to the OnUseAbilityUsedItem event to get the last used item: https://opsive.com/support/document...ller/programming-concepts/events/event-names/
 
Actually last equipped is what I meant to say. I'm guessing GetActiveCharacterItem only returns the item if it's currently equipped, but I can keep track of that index easily enough in a script.

How do I make an ability call a script? I thought I'd have to drop the script on the character and subscribe to an event or something.
 
Wouldn't I attach something to the Use script, or else make an altered version? Otherwise I'm not understanding how I get the "attack" state, unless I scan for the raw input.
 
For tracking last equipped you can do something like this. You subscribe to the event. You could also use any of the other events fired when an item is used or reloaded etc

Code:
        /// <summary>
        /// Used to keep track of items equipped
        /// </summary>
        /// <param name="equipItem">The item being equipped</param>
        /// <param name="slotID">The slot the item is being equipped to</param>
        private void AbilityWillEquipItem(CharacterItem equipItem, int slotID)
        {
            int itemSetIndex = m_EquipUnequipAbility.ActiveItemSetIndex;
            if (itemSetIndex != -1)
                m_CurrentItemSetIndex = itemSetIndex;
            //Debug.LogFormat("AbilityWillEquipItem: {0}, slot: {1}, ActiveItemSetIndex: {2}, m_CurrentItemSetIndex: {3}", equipItem.name, slotID, m_EquipUnequipAbility.ActiveItemSetIndex, m_CurrentItemSetIndex);
        }
 
As Justin stated the OnUseAbilityUsedItem event would be appropriate, and if you want to exclude caching that weapon, you can use a check for it before caching it.
 
Appreciate it, but that was a bit over my head. I don't know the code in and out like you guys, so I studied the ToggleEquip item ability (since that almost does what I need already). Now I sorta get what you were saying... it's just I don't know all these classes and variables. But I made an EquipOnAttack item ability based on ToggleEquip (just much simplified)...

Code:
namespace Opsive.UltimateCharacterController.Character.Abilities.Items

{
    using Opsive.UltimateCharacterController.Utility;

    /// <summary>
    /// The EquipOnAttack ability will equip the current (last equipped) ItemSet automatically when the player attacks.
    /// EquipOnAttack just specifies which ItemSet should be equipped and then will let the EquipUnequip ability to do the actual equip.
    /// </summary>
    [DefaultStartType(AbilityStartType.ButtonDown)]
    [DefaultInputName("Attack")]
    [AllowDuplicateTypes]
    public class EquipOnAttack : EquipSwitcher
    {
        private int m_PrevItemSetIndex = -1;

        /// <summary>
        /// The EquipUnequip ability has changed the active ItemSet. Store this value so EquipOnAttack knows which ItemSet to equip after the unequip.
        /// </summary>
        /// <param name="itemSetIndex">The updated active ItemSet index value.</param>
        protected override void OnItemSetIndexChange(int itemSetIndex)
        {
            if (!Enabled)
            {
                return;
            }

            if (itemSetIndex == -1)
                itemSetIndex = 1; // player can't drop or change first item, so default to this
            m_PrevItemSetIndex = itemSetIndex;
        }

        /// <summary>
        /// The ability has started.
        /// </summary>
        protected override void AbilityStarted()
        {
            base.AbilityStarted();

            // Start the EquipUnequip ability and then stop the ability. The EquipUnequip ability will do the actual work of equipping or unequipping the items.
            m_EquipUnequipItemAbility.StartEquipUnequip(m_PrevItemSetIndex, false, false);
            StopAbility();
        }
    }
}

And it seems to do what I need quite nicely, but let me know if I have any pitfalls here (e.g. so far I only have one weapon to test with!).

Thanks for the help!!
 
If it works it works! :) thats great, and yes there is always more than one way to skin a beast... You found something that works which is awesome.
I may suggest if you're wanting to keep the old previous itemset rather than default to 1 in the case of -1 you can just replace > itemSetIndex = 1; // player can't drop or change first item, so default to this
to > return;

I really do suggest to equip yourself with knowledge of the event system, it is insanely powerful, and you can pretty much assume there is an event for everything in UCC that gets executed (EventHandler.ExecuteEvent) you can subscribe to (EventHandler.RegisterEvent), and you can use your own too. Just search through some of the code for example usage. "OnDeath" maybe a good one to search for to kick off.
 
Just search through some of the code for example usage. "OnDeath" maybe a good one to search for to kick off.
Thanks! I will do that.

Speaking of OnDeath... the character's hand is still cupped like they're holding the weapon after they respawn. I'm sure I missed something stupid.
 
N/M, I checked Remove All On Death in the Inventory component and that seems to have done the trick. Now I need to figure out how to make the weapon not just disappear when they ragdoll.
 
Top