onStartFire / onFire event

Absent83

Member
Hey Justin!

Can you explain how to trigger an event when shooting with shootable weapon?

I need to be able to listen only to the first shot or each shot on a fully automatic weapon.


Thank!
 
You could listen to the OnItemAbilityActive event to determine when an item ability has been activated, and get the Item Action from that. The second parameter indicates if the ability is starting.
 
In the case of OnItemAbilityActive, I must: define:

- detect type of ItemAbility (is it a "use" ability).
- detect what weapon is shooting now? because of my action on a shot depends on the current type of weapon.


It would be easier for me to listen to shots on each weapon separately, rather than OnItemAbilityActive (an event for the entire character, which I must analyze).


Can I listen to weapons "use" events? Separately each weapon or only a current weapon.

It will be ideal, if i could add component to every item (weapon) wich can listen to "use" (shot) events of corresponding item (weapon) and trigger method, for example debug.log("PMDefault shot") and so on for each weapon.

1551810046497.png
 
Last edited:
The specific weapon Item Actions do not have any events when they fire. I can add this to my feature request list though.
 
Hi, are you sure? :)

I add this component to all shootableItems and it works good!

please review this code


C#:
[RequireComponent(typeof(ShootableWeapon))]
public class ShootableWeaponUccMakeNoise : MonoBehaviour
{
    private GameObject m_Character;
    private IUsableItem _usableItem;


    private void Awake()
    {
        var characterLocomotion = gameObject.GetCachedParentComponent<UltimateCharacterLocomotion>();
        m_Character = characterLocomotion.gameObject;
        _usableItem = GetComponent<IUsableItem>();
    }

    private void OnEnable()
    {
        EventHandler.RegisterEvent<IUsableItem, bool>(m_Character, "OnItemStartUse", ItemStartUse);

    }

    private void ItemStartUse(IUsableItem usableItem, bool isStart)
    {
        if (isStart && _usableItem == usableItem)
        {
            Debug.Log("shot: " + _usableItem);
        }
    }


    private void OnDisable()
    {
        EventHandler.UnregisterEvent<IUsableItem, bool>(m_Character, "OnItemStartUse", ItemStartUse);
    }
}
 
Hi, are you sure? :)

I add this component to all shootableItems and it works good!

please review this code


C#:
[RequireComponent(typeof(ShootableWeapon))]
public class ShootableWeaponUccMakeNoise : MonoBehaviour
{
    private GameObject m_Character;
    private IUsableItem _usableItem;


    private void Awake()
    {
        var characterLocomotion = gameObject.GetCachedParentComponent<UltimateCharacterLocomotion>();
        m_Character = characterLocomotion.gameObject;
        _usableItem = GetComponent<IUsableItem>();
    }

    private void OnEnable()
    {
        EventHandler.RegisterEvent<IUsableItem, bool>(m_Character, "OnItemStartUse", ItemStartUse);

    }

    private void ItemStartUse(IUsableItem usableItem, bool isStart)
    {
        if (isStart && _usableItem == usableItem)
        {
            Debug.Log("shot: " + _usableItem);
        }
    }


    private void OnDisable()
    {
        EventHandler.UnregisterEvent<IUsableItem, bool>(m_Character, "OnItemStartUse", ItemStartUse);
    }
}
This is working great, @Absent83 how would you change the code to register the stop event too?

Regards,
Markus
 
Thanks for this! This still works today March 29th/2021. Really wish this kind of info was in the documentation at this level of detail but this is exactly what I was looking for!!!!
 
Last edited:
Top