In component-based spell system, how to set spell actions with the data they need?

IMO you should move the parameters you're trying to put on apply into the construction of the spells/actions using some kind of factory pattern. This is just off the top of my head and maybe not quite the best implementation but conceptually you'd have something like this:

class SpellFactory
    Spell CreateSpell(SpellType type, Actor source, Actor target)
        var actionTypes = spellActionsForType[type] // assuming this is defined somewhere
        var spell = new Spell()
        foreach (var action in actionTypes)
            spell.AddAction(CreateSpellAction(action, source, target))
        return spell

    SpellAction CreateSpellAction(SpellActionType type, Actor source, Actor target)
        switch (type)
            case HealAction:
                return new HealSpellAction(source)
            case DamageAction:
                return new DamageAction(target)
            ...

Then the only place that knows about all the parameters are the factory methods. Each individual action only takes in the data it needs (and stores it as a member). Then your apply method doesn't need any parameters since all parameters were passed into the construction of the object keeping your Spell and base SpellAction classes nice and clean.

Of course this does mean that you have to create a new instance of the spell each time you cast it, but you would still only need to setup the definitions of the spells once. However this pattern helps ensure that the common part of your API (namely the apply method that your base Spell calls on each SpellAction) doesn't leak the requirements of each action.

/r/gamedev Thread