How to quit this if-mess?

HandleDamage(DamageResult : damage_result):void=
        if (TargetCharacter := fort_character[DamageResult.Target]):
            if (Instigator := DamageResult.Instigator?):
                if (InstigatorAgent := Instigator.GetInstigatorAgent[]):
                    if (InstigatorCharacter := fort_character[InstigatorAgent]):
                        if (TargetCharacter <> InstigatorCharacter):
                            if (ScoreTotal := Round[DamageResult.Amount * ScorePerPointOfDamage]):
                                ScoreManager.SetScoreAward(ScoreTotal)
                                ScoreManager.Activate(InstigatorAgent)

Is there any way to handle this? It seems like due to the requirement of the Verse to be sure no expression fails I have to do this infinite nesting? Is it possible to make it look more like this (it’s C# code):

private void HandleDamage(DamageResult damageResult)
    {
        FortCharacter targetCharacter = damageResult.Target;
        if (damageResult.Instigator == null)
            return;
        Agent instigatorAgent = damageResult.Instigator.GetInstigatorAgent();
        FortCharacter instigatorCharacter = instigator.GetFortCharacter();
        if (targetCharacter == instigatorCharacter)
            return;
        int scoreTotal = (int) Math.Round(damageResult.Amount * _scorePerPointOfDamage);
        _scoreManager.SetScoreAward(scoreTotal);
        _scoreManager.Activate(instigatorAgent);
    }

Is this what you had in mind?

HandleDamage(DamageResult : damage_result): void =

    if:
        TargetCharacter := fort_character[DamageResult.Target]
        InstigatorAgent := DamageResult.Instigator?.GetInstigatorAgent[]
        InstigatorCharacter := InstigatorAgent.GetFortCharacter[]
        TargetCharacter <> InstigatorCharacter
        ScoreTotal := Round[DamageResult.Amount * ScorePerPointOfDamage]
    then:
        ScoreManager.SetScoreAward(ScoreTotal)
        ScoreManager.Activate(InstigatorAgent)

You can also return from the function in the same way as your other example.

HandleDamage(DamageResult : damage_result): void =

    if. not DamageResult.Instigator?.GetInstigatorAgent[] then. return
1 Like

Didn’t know about the first option, thank you!

About return. Doesn’t this line mean that TargetCharacter variable will only be available in "if "scope and not in method/function afterwards?

if (not (TargetCharacter := fort_character[Result.Target])): return

No worries. It does, yea. I believe the first option is needed if you want to use the variable.

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