Is Lambda Function Necessary?

I’m curious here why Sam didn’t make user a member variable? The Lambda function seems to keep the methods stateless but wondering if we needed that? Is there something I might be misunderstanding maybe if multiple users want to use the same Ability? I don’t think there’s anything multithreaded going on here but I could imagine some hypothetical use case where you want this.

I’m not an engineer, so the lambda functions combined with method passing is just a little hard for me to understand what’s happening when I try to read it without Sam’s walkthrough.

public override void Use(GameObject user)
{
    this.user = user;
    targetingStrategy.StartTargeting(user, TargetAcquired);
}

private void TargetAcquired(IEnumerable<GameObject> targets)
{
    foreach (var filterStrategy in filterStrategies)
    {
       targets = filterStrategy.Filter(targets);
    }
    foreach (var effect in effectStrategies)
    {
       effect.StartEffect(user, targets, EffectFinished);
    }
}

I think I may have answered my own question. Is this passing of “user” needed because we’re using coroutines and each Ability is a single scriptable object which can be used by multiple users? e.g. maybe enemies want to use this ability too.

Privacy & Terms