Loops

General

There are several ways to loop through flags (e.g. formula.target.statusEffects)

Let's say you wanted to create a formula function that doubles the formula's power for every status effect the target has. To do that, you could write something like this:

local function func(formula, caster, target, functions, classes, helpers)
  for i, v in pairs(formula.target.statusEffects) do
    if v == 1 then
      formula.power = formula.power * 2
    end
  end
end

return func

Now let's exclude the status effects protect and shell as another requirement. Since you have access to the status effect identifiers, you can easily add them to the current condition.

local function func(formula, caster, target, functions, classes, helpers)
  for i, v in pairs(formula.caster.statusEffects) do
    if i ~= 18 and --protect
       i ~= 19 and --shell
       v == 1 then
      formula.power = formula.power * 2
    end
  end
end

return func

As the next step, you could expect the caster to also have the same status effects. There are multiple ways to accomplish this, some a bit more readable than others.

For example, instead of looping through the status effects table, you could also loop through the status effect bits table directly. That way you have access to not only a status effect's identifier, but also its name.

local function func(formula, caster, target, functions, classes, helpers)
  for name, id in pairs(helpers.statusEffectBits) do
    if name ~= "protect" and
       id ~= 19 and --shell
       formula.caster.statusEffects[name] == 1 and
       formula.target.statusEffects[id] == 1 then
      formula.power = formula.power * 2
    end
  end
end

return func

The flags here are accessed via different ways for demonstration purposes. You can choose whichever way suits your coding style more.

Notes

Last updated