Game Development Reference
In-Depth Information
The preceding code has
ActorDrain
and
EnemyDrain
(kill two birds with one stone and all that).
%s
is used as
an indicator within the
sprintf
method. To quote the help file for the
%s
indicator:
Character strings are output.
The
sprintf
method for our first statement uses three parameters (
fmt
is the sentence to be formatted and not a
parameter per se). As you can see, both
ActorDrain
and
EnemyDrain
have three instances of
%s
. The method plugs in
the value of its parameters, from left to right. So, the first
%s
in
ActorDrain
would be filled with
@battler.name
(the
name of the actor or enemy, as appropriate); the second
%s
would house the current name of the HP stat (remember
that you can switch the name of quite a few terms from the self-named tab in the Database); and the third
%s
would
display
@hp_drain
(the amount of HP drained by the skill or item).
ActorDrain
is used when an Actor is affected by a draining attack, whereas
EnemyDrain
is used when an enemy is
hit by the same. Suppose Eric is hit with a draining attack that takes away 37 HP. The
ActorDrain
string would read as
follows:
Eric was drained of HP 37.
Not the clunkiest statement, but we should invert the number and the HP term for sure. The
EnemyDrain
string,
on the other hand, is terrible. Here's a sample line from a Slime drained of 11 HP.
Drained Slime 11 from HP.
There's a reason why I made it a point to figure out how to fix this. Your first instinct may be to just swap
the location of the HP value and the number, as I noted in the previous paragraph. The problem in this initial
if
statement is that, if we do that, we still get the following for the
EnemyDrain
sentence:
Drained Slime HP from 11.
Closer, but still clunky. What we actually have to do is split both possibilities, such that RMVXA fills in the
sentence, depending on whether it is
ActorDrain
or
EnemyDrain
. After all, the
%s
should not be filled out in the same
order for both.
Tweaking the hp_damage_text Method
Without further ado, let's tweak this method. Here is what the code for
if @hp_drain > 0
should look like when
tweaked:
def hp_damage_text
if @hp_drain > 0
fmt = @battler.actor? ? Vocab::ActorDrain : Vocab::EnemyDrain
if fmt = Vocab::ActorDrain
sprintf(fmt, @battler.name, @hp_drain, Vocab::hp)
else
sprintf(fmt, @hp_drain, Vocab::hp, @battler.name)
end
As you can see, we split the single
sprintf
argument in two, based on whether the
Vocab
used is
ActorDrain
or
EnemyDrain
. In the first case, we flip the
@hp_drain
and
Vocab::hp
parameters. In the alternate case, we switch it all
around. The previous example should now read like the following:
Drained 11 HP from Slime.