Turn Based Strategy course - Ragdoll Lesson.... Variables reset by an event?

Hello

In the Ragdoll lesson, Turn based course, I wanted to give a go at modifying the AddExplosionForce position.
Instead of setting it at the foot of the target, I wanted to capture the firing angle and offset the explosion position in the opposite direction of the firing angle, by a couple of units outside of the model and 1 unit up.

In the RagdollSpawner script, I created class members to record and store the firing angles
private Vector3 firingAngle = new Vector3(0, 0, 0);// PG
private List listShotsFired = new List();
They will be update by the OnShoot event - see below.

I then suscribed to OnShoot event in the Awake - ideal as EventArgs stores the shooter and target- and defined the function:
All the code is working fine, the debug shows I can record the number of shots fired and their angles, including the last shot killing the enemy… No problem so far…
private void ShootAction_OnShoot(object sender, ShootAction.OnShootEventArgs e)
{
Debug.Log("E.Shooting position = " + e.shootingUnit.transform.position);
Debug.Log("E.Target position = " + e.targetUnit.transform.position);
firingAngle = (e.targetUnit.transform.position - e.shootingUnit.transform.position).normalized;
Debug.Log("FiringAngle = "+ firingAngle);
listShotsFired.Add(firingAngle);
for(int i =0; i<listShotsFired.Count;i++)
{
Debug.Log("Shot " + i + " angle: " + listShotsFired[i]);
}
}

It all goes wrong when the existing HealthSystem_OnDead fires up in the same class.
When this event happens, it is time to use those variables we were recording…
Unfortunately, they are all null now !!!
private void HealthSystem_OnDead(object sender, EventArgs e)
{
int shotsFired = listShotsFired.Count - 1;// the list is now empty, all reset to 0!!!
firingAngle = listShotsFired[shotsFired];// listShotsFired now empty * CRASH *

    Transform ragdollTransform = Instantiate(ragdollPrefab, transform.position, transform.rotation);
    UnitRagdoll unitRagdoll =ragdollTransform.GetComponent<UnitRagdoll>();
    unitRagdoll.Setup(originalRootBone, firingAngle);// runs fine but passes a null firingAngle
}

All the variables I had created seem to have lost their values, why is this happening?
There is no other function or event in this class, just the awake to subscribe and both events above.

Thanks for your help.

Is all that code in the same class? Which OnShoot did you subscribe to?
Sounds like you’re subscribing to the OnShoot attached to that same object, which is going to be the Shooter Unit, but them for the HealthSystem, that one is being captured on the Target Unit.

So you’re storing both things in 2 separate places, you can add a simple Debug.Log(transform) to verify, the Shoot event will show the long in one unit and the dead event on another unit.

You need the Target Unit to know when it was shot, so you want to either modify the Shoot Action to trigger some function on the target unit, or listen to the OnDamaged event on the HealthSystem to know when that unit took damage.

1 Like

Thanks Hugo for replying quickly

Yes, the code is in the same class, and I understood my error when I read your comment…
Of course, the Debug confirmed ALL your assumptions.

I have tried your first suggestion, I actually passed the identity of the shooting unit in the damage function, works like a charm and I don’t see anything illogical in passing this info in Damage().

Point is: I am glad I made this mistake and realised that events do not replace function calls and must be used sparingly, properly and only when needed!

Great course by the way.

1 Like

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms