What is the difference between eventArg e and ShootAction.OnShootEventArg?

Hello Everyone.
Scuse I 've forgot to reply to my last post according
“another-way-to-test-if-shoot-is-possible”.
So thanks for the reply.
My question today was about why you write a little bit differently the function for the ShootAction and the SwordAction ?

private void ShootAction_OnAnyShoot(object sender, ShootAction.OnShootEventArgs e)

    {

        ScreenShake.Instance.shake();

    }

    private void SwordAction_OnAnySwordHit(object sender, EventArgs e)

    {

        ScreenShake.Instance.shake(2f);

    }

Why don"t a simple EventArg e for the shootAction?
Thanks.

François

Because the OnAnyShoot event is defined to supply ShootAction.OnShootEventArgs. The event will supply details in there.

When we create an event

public event EventHandler OnAnySwordHit;

We are saying that we will fire an event called OnAnySwordAction. EventHandler here is a definition of what that call signature will be, in this case (object sender, EventArgs e).

When we define OnAnyShootAction it is defined as EventHandler<ShootAction.OnShootEventArgs> so the signature will be (object sender, ShootAction.OnShootEventArgs e) and we must match that in order to handle the event.

The OnAnyShoot event supplies the target unit and shooting unit when it fires the event

1 Like

Correct, while we don’t need the OnShootEventArgs for this subscribers, it is needed for other subscribers.

Hello Friends.
Thanks for your replies.
I’ve read them and read the scripts.
Ok for the way to write it but i’ve now another question :slight_smile:
Why do we need to now who is shooting and who is the target?
We don’t need that in the sword action.
We have the targetUnit and the Unit in the SwordAction script.
What is the difference for we need to create the public class OnShootEventArgs in shoot action?

        public class OnShootEventArgs : EventArgs
    {
        public Unit targetUnit;
        public Unit shootingUnit;
    }

Thanks.
François

The non-static OnShoot event uses the event args in UnitAnimator to determine where the target is and where the bullet should go when it is instantiating the bullet projectile. I guess the OnAnyShoot event was just defined to match the OnShoot event. We don’t actually use those values anywhere.

We actually do use the targetUnit in the UnitAnimator handler:

    private void ShootAction_OnShoot(object sender, ShootAction.OnShootEventArgs e)
    {
        animator.SetTrigger("Shoot");

        Transform bulletProjectileTransform = 
            Instantiate(bulletProjectilePrefab, shootPointTransform.position, Quaternion.identity);

        BulletProjectile bulletProjectile = bulletProjectileTransform.GetComponent<BulletProjectile>();

        Vector3 targetUnitShootAtPosition = e.targetUnit.GetWorldPosition(); //Here

        targetUnitShootAtPosition.y = shootPointTransform.position.y;

        bulletProjectile.Setup(targetUnitShootAtPosition);
    }

We don’t use the shootingUnit, however.

Ok,Ok.
It’s because we need to manage the shooter and the target?
but we have too a target in and an hiter in the word action no?
So if I want to throw a knife, it would be better to mimic the ShootAction.
For a spear, a long range hand weapon mimic the sword.
What would happen if we code the shootaction without a OnShootEventArg but a simple
public event EventHandler OnShoot;
and a simple
public Unit targetUnit;
public Unit shootingUnit;

?

Ideally, with events, it is best practice to encapsulate needed data into the event than it is to expose data publicly.
In this case, we could easily replace the OnShootEventArg with

public event EventHandler<Unit> OnShoot; //parameter is target unit

and then the receiving signature would be

public void ShootAction_OnShoot(object sender, Unit targetUnit)

Privacy & Terms