1
\$\begingroup\$

I'm trying to trigger poison effects at the end of each turn in a turn based game. Each Actor in battle has a list of active effects, each of which could be a poison. I simply want to Execute() all of those Poisons.

See my code below; is there a way I can combine my two queries or make them more efficient so that the moment a Poison is found, that Poison executes?

var poisonedActors =
            from Actor actor in ins.BattleParticipants
            where actor.ActiveEffects.Any(activeEffect => activeEffect is Poison)
            select actor;
var poisons =
            from ActiveEffect activeEffect in poisonedActors
            where activeEffect is Poison
            select activeEffect;
foreach (Poison poison in poisons)
            poison.Execute();

EDIT: Is this more efficient? Is there a better way to do things?

    foreach (Actor actor in ins.BattleParticipants)
        foreach (ActiveEffect activeEffect in actor.ActiveEffects)
            if (activeEffect is Poison)
                activeEffect.Execute();
\$\endgroup\$
  • \$\begingroup\$ Does a poison effect disappear once it's triggered? Is it just poison effects that are "triggered"? \$\endgroup\$ – clabe45 Aug 21 '17 at 23:14
  • \$\begingroup\$ No. I just want to trigger all poison effects at the same time. Other effects can be triggered as well, but that's not the focus right now. \$\endgroup\$ – reincarnationofstackexchange Aug 21 '17 at 23:16
  • 1
    \$\begingroup\$ The select statements are from LINQ (Language Integrated Query), a very readable database language integrated into C#. They are indeed code! \$\endgroup\$ – reincarnationofstackexchange Aug 22 '17 at 3:13
  • 1
    \$\begingroup\$ How many entities are there going to be? This might be premature optimization. If you just want to know which is faster for curiosity's sake, I would suggest cranking the number of entities up really high and timing it, or examining the generated CLR code. It's possible the compiler crunches those examples to the same thing in the end! \$\endgroup\$ – Ryan1729 Aug 22 '17 at 5:31
  • 1
    \$\begingroup\$ In the first example you check the same activeeffects twice, in the second only once. So it looks like the second could be more efficient, but that is not much more than a guess without some testing. \$\endgroup\$ – SpacemanSpiff Aug 22 '17 at 8:40
2
\$\begingroup\$

A "performance critical" part of your code is the check if (activeEffect is Poison)

It looks as though Poison inherits from ActiveEffect but that it is different from the other classes that inherit from ActiveEffect since you want to call execute separately.

I would do this if performance was critical: create a separate list (ActiveEffectsExecuteOnTurnEnd) in 'Actor' where you only put stuff that will execute at the end of the turn.

\$\endgroup\$
  • \$\begingroup\$ To clarify, all ActiveEffects have an Execute function. However, I want to trigger only the Poison's Execute functions. \$\endgroup\$ – reincarnationofstackexchange Aug 23 '17 at 19:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.