Switch to a specific item on Interact start

mxkacsa

New member
Hello,

I'm trying to change a specific weapon when the interact starting.

IInteractableTarget's Interact method run too late, but if I try to run the EquipWeapon script here it's not working.

EquipWeapon script:
private void EquipWeapon(GameObject character)
{
var characterLocomotion = character.GetComponent<UltimateCharacterLocomotion>();
var equipUnequipAbilities = characterLocomotion.GetAbilities<EquipUnequip>();
for (int i = 0; i < equipUnequipAbilities.Length; ++i) {

if (equipUnequipAbilities.ItemSetCategoryIndex == categoryIndex) {
Debug.Log("start equip " + changeToWeaponItemSetIndex);
equipUnequipAbilities.StartEquipUnequip(changeToWeaponItemSetIndex, true, true);
return;
}

}
}

This script 100% working. If I call this from IInteractableTarget's CanInteract it's working, but wrong time.

I'm trying to extend the Interact ability and call from the "AbilityStarted" or "DoInteract" methods, but still not work.

Sorry, my english grammar is very poor, so here is a video where I want to switch to crowbar on interact


(Crowbar's hand not animating, but that's another promlem :D)

Where I can call StartEquipUnequip before interact?
 
There isn't a way within the interactor target, but you could subclass the Interact ability and add it within AbilityStarted.
 
Thank you for the quick answer!

I'm trying from the AbilityStarted, but there isn't work,
but now I tryed from the AbilityWillStart method and now working!

Here is my solution if somebody need this ability:

C#:
using UnityEngine;
using Opsive.Shared.Game;
using Opsive.UltimateCharacterController.Character;
using Opsive.UltimateCharacterController.Inventory;
using Opsive.UltimateCharacterController.Character.Abilities;
using Opsive.UltimateCharacterController.Character.Abilities.Items;

public class SwitchItemSetInteract : Interact
{
    [SerializeField]
    protected int categoryIndex;

    [SerializeField]
    protected int switchToWeaponItemSetIndex;

    [SerializeField]
    [Tooltip("required ItemSets to run this interaction")]
    private int[] requiredItemSetIndexes;

    /*protected override void AbilityStarted()
    {
        // NOT WORKING HERE
        EquipWeapon();

        base.AbilityStarted();
    }*/

    public override bool CanStartAbility()
    {
        return HasItemSet() && base.CanStartAbility();
    }

    public override bool AbilityWillStart()
    {
        EquipWeapon();

        return base.AbilityWillStart();
    }

    protected void EquipWeapon()
    {
        var characterLocomotion = m_GameObject.GetCachedComponent<UltimateCharacterLocomotion>();
        var equipUnequipAbilities = characterLocomotion.GetAbilities<EquipUnequip>();
        for (int i = 0; i < equipUnequipAbilities.Length; ++i)
        {
            if (equipUnequipAbilities[i].ItemSetCategoryIndex == categoryIndex)
            {
                equipUnequipAbilities[i].StartEquipUnequip(switchToWeaponItemSetIndex, false, false);
                return;
            }
        }
    }

    protected bool HasItemSet()
    {
        var itemSetManager = m_GameObject.GetCachedComponent<ItemSetManager>();

        for (int i = 0; i < requiredItemSetIndexes.Length; i++)
        {
            if (!itemSetManager.IsItemSetValid(categoryIndex, requiredItemSetIndexes[i], true))
            {
                return false;
            }
        }

        return true;
    }
}
 
Top