How to make the most of custom formulae. Part #1

This tutorial can also be found on my blog at: http://cobbtocs.co.uk/wp/?p=271 || Archive Link

So earlier today in rpgmaker's irc chat the conversation moved to the scripts someone desired for their game which included:

favorite foods - i can specify each actors favorite food and when the item is used on them, they heal extra hp or something

Anyhow I pretty quickly realised a script was not needed to implement this feature. RPG Maker Ace once again proves just how versatile it is because this feature can be implemented in a single input box. In particular this one:

SS0027.png


That formula box alone has tremendous potential. I offered two solutions to the problem.



Code:
if b.id == 1; 500; else; 100; end;



Code:
b.id == 1 ? 500 : 100
Let us first look at exactly what they are doing. They both check a conditional statement, namely:



Code:
b.id == 1
For the formula box in general a refers to the attacker or user and b refers to the defender or recipient, who may or may not be the same.

Any way what we are checking is that the recipient's id is equal to 1. Please note that this example is about an item which should be usable on party members only and therefore b is always going to be an actor (party member in English). So essentially we are checking that the person who the item is being used on is first in the database (Eric by default).

Now let's look at the rest of those lines.



Code:
if b.id == 1; 500; else; 100; end;
If you've done any programming before you will recognise this as a standard if statement. If you're confused about the presence of the semi-colons then let me quickly explain that they are like a way of inserting a new line. Were we not restricted to a single line then we could have written it like this:



Code:
if b.id == 1

500

else

100

end
So what this means is when the character id equals 1, heal 500HP otherwise heal 100HP. The other version achieves the same result but might be a little less clear if you are not overly familiar with ruby.



Code:
b.id == 1 ? 500 : 100
It still has the conditional, it is still checking if the item is being used on Eric but the rest of the code you could say has been condensed. This effectively boils down to:



Code:
condition ? true : false
The last thing two things I want to say before moving on are firstly that for more complicated situations you are probably better going the if statement route and the last thing is that while the formula box is pretty powerful you need to be aware that it is capped at a certain length (let's be grateful that we use a & b and not attacker & defender) and if there is an error in your formula the skill will just miss repeatedly rather than crash the game.

Right so that concludes the explanation section of this tutorial. I personally find having a lot of examples to work from quite helpful so this next section is going to be a problem and solution section and hopefully one of them will be close enough to what you hope to achieve that you can adapt it.

Scenario: Half your party are demi-humans or robots and require different healing items than the rest of your party.

There are some quite creative solutions to this problem but for the sake of this tutorial I want to keep is simple. So the solution I offer just checks the ids again.



Code:
(b.id==1 or b.id==3 or b.id==4) ? 500 : 0
A couple of things I would like to draw your attention to. I have omitted the spaces on both sides of == because when we're space conscious they are wasted characters. The other feature is the ors they act like you'd expect but be careful when using and because it has a bit more to it.

Scenario: One character's favourite food is peanut butter sandwiches but another character is allergic to peanuts.

So the way I'm going to implement it is that Eric gets a bonus 100 health when he eat the sandwich but Natalie is afflicted by poison when she eats one.



Code:
if b.id==1;200;elsif b.id==2;b.add_state(2);0;else;100;end;
So it was a bit more complicated this time so I went the route of the if statement, elsif is a way of checking another condition if the first one wasn't met. So let's just talk it through, if Eric gain 200HP, if Natalie add status number 2 (poison) otherwise restore 100HP.

Scenario: You have a thunder skill which doors more damage when outside, which you control with a switch

This is just a switch example.



Code:
$game_switches[x] ? 500 + a.mat * 4 - b.mdf * 2 :  200 + a.mat * 4 - b.mdf * 2
I've also included in it an example of how to include the default formula so you can see how to incorporate it.

Scenario: I have a skill whose power I want to control through a variable

Easy enough. Here's how to do 10 times the value of the variable.



Code:
$game_variables[x] * 10
Scenario: I have a gamber and I want him to use dice skills e.g. he rolls two six sided dice, the skill does 100 times the combined number of the faces. It does double if he rolls a double and 10000 if he rolls snake eyes.

This one has a lot in it so we'll look at it closely. We'll use the if method and then use the rand(x) method which returns a number from 0 to x-1 meaning we will add 1 to it to simulate dice. We will also need to create some local variables, I will use c & d (DO NOT USE a & 😎 .



Code:
c=1+rand(6);d=1+rand(6);if c==1 and d==1;10000;elsif c==d;c*400;else;(c+d)*100;end;
It's not perfect, I would quite like to know what he rolled rather than just seeing the damage. Don't worry the solution to this new problem will be shown in the next tutorial.

Scenario: We want a skill like White Wind (a final fantasy move which restores someone's current health).

An easier one for once, we want the target's ( 😎 hp



Code:
b.hp
Other parameters you can use:

hp,mp,tp,mhp (Maximum Hit Points), mmp *Maximum Magic Points), atk,def,mat (Magic ATtack power),mdf,agi,luk

Scenario: What about demi?

Demi was a move which took a quarter of your current health.



Code:
b.hp / 4
Scenario: I want a skill which uses someone else's stat e.g. my princess is bossing her guard around.

This is easier than you think, as long as it's a particular character.



Code:
$game_actors[1].atk * 4 - b.def * 2
Eric attack!

Scenario: I want a skill to do x

Leave a comment and I will try my best to direct you.
Many many thanks Fomar.

I think I read somewhere you could use \v[x] instead of $game_variables[x] though.
Many many thanks Fomar.

I think I read somewhere you could use \v[x] instead of $game_variables[x] though.
Thanks, \v[x] is in message boxes it won't work in a damage formula.
Oh right, without the backslash.
Yeah, I'd point that out. The mouseover help box shows this: v[n] can be used

Anyways, there's a lot of neat information there; I was hoping to see a tutorial on formulae. I think I'm gonna toy around with some of the ideas you presented 🙂
Oh right, without the backslash.
Yeah v[n] is fine, see I just learnt something new.

Yeah, I'd point that out. The mouseover help box shows this: v[n] can be used

Anyways, there's a lot of neat information there; I was hoping to see a tutorial on formulae. I think I'm gonna toy around with some of the ideas you presented 🙂
Thanks and good luck!
Okay so i want a skill to work like this. ( no idea how )

Skill: Mark

Applies Mark on Enemy target (a.atk*3-b.def*2)

Then the next time A attacks B The mark explodes, damage is dealt (a.atk*4-b.def*1.25)

Aswell, 10% life drain to A from B from the damage of mark exploding.

the goal of this is to be able to employ strategies where I put the mark on enemy, an ally buffs my atk, and total damage is better.

Sorry if its a bit too much. Maybe just go over how i would make the Next attack simply more powerful after application of the Mark Skill.
like i said in irc bro this tut is mad useful. awesome job.

Okay so i want a skill to work like this. ( no idea how )

Skill: Mark

Applies Mark on Enemy target (a.atk*3-b.def*2)

Then the next time A attacks B The mark explodes, damage is dealt (a.atk*4-b.def*1.25)

Aswell, 10% life drain to A from B from the damage of mark exploding.

the goal of this is to be able to employ strategies where I put the mark on enemy, an ally buffs my atk, and total damage is better.

Sorry if its a bit too much. Maybe just go over how i would make the Next attack simply more powerful after application of the Mark Skill.
might be better off using troop event pages than formulae for this.
How would that simplify it?
This is a great tutorial. thank you very much!
@leaguepro Try this.



Code:
if b.state?(x); y; else; z; end
X being the id of the Mark state, Y the damage dealt if the state is applied, Z if othewise.



Code:
if b.state?(x); b.remove_state(x); y; else; z; end
If you want to remove the Mark state after the skill.
like i said in irc bro this tut is mad useful. awesome job.

might be better off using troop event pages than formulae for this.
Thanks 🙂

Okay so i want a skill to work like this. ( no idea how )

Skill: Mark

Applies Mark on Enemy target (a.atk*3-b.def*2)

Then the next time A attacks B The mark explodes, damage is dealt (a.atk*4-b.def*1.25)

Aswell, 10% life drain to A from B from the damage of mark exploding.

the goal of this is to be able to employ strategies where I put the mark on enemy, an ally buffs my atk, and total damage is better.

Sorry if its a bit too much. Maybe just go over how i would make the Next attack simply more powerful after application of the Mark Skill.
Couldn't the initial attack add a state where the enemy's defence is lowered?
Just discovered you can also use b.exp, b.gold and even b.enemy_id and b.index. Basically anything in Game_Enemy that returns a number.

I guess creating new methods inside that class would work too.
I guess creating new methods inside that class would work too.
Yep, hopefully I'll finish writing that tutorial soon.
any way of making an attack or skill do more damage as the users hp is lower?

-it is official, i am dumb... figured this our after rereading your tutorial...
Is there a way to use this to make an enemy award more experience ? I've tried b.xp *= 1.5 and that didnt work.
I want to include something in every one of my damage formulas that prevents each attack from ever doing less than 1 damage.

I've tried:

1 + (a.atk * 4 - b.def * 2)

and

(a.mat * 2.5 - b.mdf) + 1

But attacks will still sometimes do 0 damage. I think I need to find some way to have the 1 be added after all other damage calculations are completed, but I don't know how to do that yet.

Also, I loved the tutorial. Taught me a lot.
try something like this:

[a.atk * 4 - b.def * 2, 1].max

remember that variance will affect damage, so set it to 0
any way of making an attack or skill do more damage as the users hp is lower?

-it is official, i am dumb... figured this our after rereading your tutorial...
Maybe something like:

(200 - ((100*a.hp)/a.mhp)*(a.atk * 4 - 2 * b.def))/100

That would do normal damage when hp is max and upto twice the normal damage when HP gets low.

Is there a way to use this to make an enemy award more experience ? I've tried b.xp *= 1.5 and that didnt work.
No, not possible with just the formula box.

I want to include something in every one of my damage formulas that prevents each attack from ever doing less than 1 damage.

I've tried:

1 + (a.atk * 4 - b.def * 2)

and

(a.mat * 2.5 - b.mdf) + 1

But attacks will still sometimes do 0 damage. I think I need to find some way to have the 1 be added after all other damage calculations are completed, but I don't know how to do that yet.

Also, I loved the tutorial. Taught me a lot.
Thanks, let's build on dionisiovega's idea.

try something like this:

[a.atk * 4 - b.def * 2, 1].max

remember that variance will affect damage, so set it to 0
Good call it should work but without variance it will always do the same damage. Let's add variance back in.

Thankfully we can just call it:

apply_variance(damage, variance)

So we get:

[a.apply_variance(a.atk * 4 - b.def * 2, 1,20)].max
great idea, i didn't know apply_variance was a legitimate function (learn something new everyday), also thanks for the idea with normal damage at full hp and up to twice damage at low hp, mine was working, yours is better.

Original thread: https://forums.rpgmakerweb.com/threads/how-to-make-the-most-of-custom-formulae-part-1.1143/