Skip to content

Quick Custom Card Variables

Alchyr edited this page Nov 21, 2024 · 14 revisions

In addition to the standard !D!, !B!, and !M! used to display card values in their text, BaseMod supports creating your own custom dynamic variables. BasicMod is set up to allow quick and easy creation of these variables without manually creating classes for them.

First, decide the name for the variable. For this example, "beep" will be used.

Defining and Using the Variable in Code

In the constructor of a card that extends BaseCard, you can use the setCustomVar method to set the base (and upgrade) amount for one of these variables.

    public ExampleCard() {
        super(ID, info);

        setDamage(DAMAGE); //Setting a normal variable
        setCustomVar("beep", 6, 3); //Setting a custom variable with a base value of 6, increased by 3 on upgrade
    }

To get the variable's current value in the use method or elsewhere, use customVar("beep").

    @Override
    public void use(AbstractPlayer p, AbstractMonster m) {
        addToBot(new DamageAction(m, new DamageInfo(p, damage, damageTypeForTurn), AbstractGameAction.AttackEffect.FIRE));
        //Using the custom variable
        addToBot(new GainBlockAction(p, p, customVar("beep"));
    }

If you want your variable to have some special calculations applied to it, such as being increased by certain buffs, you can use the setVarCalculation method or the other overloads of setCustomVar.

If isMultiDamage is set to true on a card, the variable will also be calculated for each individual enemy and stored in an array, similar to multiDamage. This can be accessed using customVarMulti("variable key"). This includes non-damage variables.

        setCustomVar("beep", 6, 3); //Setting a custom variable with a base value of 6, increased by 3 on upgrade
        setVarCalculation("beep", (card, m, base)->{ //For the "beep" variable, given some monster target and the base value:
            if (m instanceof Byrd) {
                base *= 2; //Double it if targeting a Byrd
            }
            /* Some extra examples:
            AbstractPower p = AbstractDungeon.player.getPower(DexterityPower.POWER_ID);
            if (p != null) base += p.amount; //Increased damage by player's dexterity
            
            p = m != null ? m.getPower(WeakPower.POWER_ID) : null; //The monster will be null when not actively targeting an enemy.
            if (p != null) base += p.amount; //Increased damage by the target's weak
            */

            return base; //Otherwise, just the original value.
        });

        setCustomVar("damage2", VariableType.DAMAGE, 10, 5); //A variable that will be calculated as damage.
        //Options are DAMAGE, BLOCK, and MAGIC. This constructor is generally not useful for the MAGIC type as this just applies no calculation.
        //You can also add extra calculation to be done before and/or after the chosen calculation type, to mimic effects like Heavy Blade.
        /*
        setCustomVar("heavyblade", VariableType.DAMAGE, 6, 3,
                (card, m, val)->{ //Pre-damage calc
                    AbstractPower strength = AbstractDungeon.player.getPower(StrengthPower.POWER_ID);
                    if (strength != null) {
                        strength.amount *= card.magicNumber;
                    }
                    return val;
                },
                (card, m, val)->{ //Post-damage calc
                    AbstractPower strength = AbstractDungeon.player.getPower(StrengthPower.POWER_ID);
                    if (strength != null) {
                        strength.amount /= card.magicNumber;
                    }
                    return val;
                });
        */

This will result in the value obtained using customVar("beep") being modified.

    @Override
    public void use(AbstractPlayer p, AbstractMonster m) {
        addToBot(new DamageAction(m, new DamageInfo(p, damage, damageTypeForTurn), AbstractGameAction.AttackEffect.FIRE));
        //Using the custom variable
        addToBot(new DamageAction(m, new DamageInfo(p, customVar("beep"), DamageInfo.DamageType.HP_LOSS), AbstractGameAction.AttackEffect.NONE));
    }

Adding the Variable to Card Text

The variable is used in card text similarly to !D! or !M!; it will be an ID between exclamation points. The ID in this case will be the mod's ID and the name you chose.

  "${modID}:ExampleCard": {
    "NAME": "Why Byrd",
    "DESCRIPTION": "Deal !D! damage. NL Enemy loses !${modID}:beep! HP. Byrds lose twice as much."
  }

Extras

After setting a custom variable, you can use colorCustomVar to change the color the number will be rendered in (Added October 9, 2023).

The normal colors for card text are Settings.CREAM_COLOR, Settings.GREEN_TEXT_COLOR, and Settings.RED_TEXT_COLOR. cpy() does not need to be used on the colors used in this method.

Clone this wiki locally