Jump to content
Fomar0153

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

Recommended Posts

So I have been talked into following up my last tutorial with another one. This one will be expanding on the previous one by going in to building upon the default scripts. First I want to talk about why you would do this and then go through a few worked examples.

 

One reason you might want to extend the default scripts for a formula could be the case that what you want to achieve doesn’t fit in the custom formula box. This is pretty easy to accomplish. Next up, you want to build in a default formula. I know that you can just use the same formula in every skill but I personally consider that bad practice. What if you discover your formula doesn’t act as you intended later in the game, fancy re-databasing all the skills and items? Or you want to create states that do different things to the formulas. We’ll be covering all that in this tutorial.

 

Let’s start by quickly analysing the formula box itself; as I’m sure you are aware a refers to the the attacker or user and b refers to the defender or recipient, now a and b refer either to an instance of Game_Actor or Game_Enemy. A quick glance at this class diagram shows us that both inherit from Game_Battler (which in turn inherits from Game_BattlerBase).

 

game_battler_inheritance.png

 

Why was that important? Simple because we have to decide where we are going to place our new bits of code. Personally I would recommend one of three places:

  • Game_Battler
  • Game_BattlerBase
  • A new Module

A module might in many ways make the most sense but being as I’m aiming this tutorial at people with limited scripting experience I’d rather avoid modules. Also if the code is added in Game_Battler we can directly access more variables directly (which suits me). So if we go to Materials in the script editor and insert us a new section then we need to add the code to add to the class:

 

class Game_Battler < Game_BattlerBase

end

Right next lets get down to examples. First let’s go through how to use a formula that’s too big for the box. Next let’s add a new method:

 

class Game_Battler < Game_BattlerBase
 def custom_formula_fireball

 end
end

If you don’t consider yourself to be a scripter then the easiest thing for you would probably be to enter the formula exactly how you would have in the custom formula box. Good news, we can do that. What we need to do is pass our new method the a,b from the custom formula. So this is what we’re upto:

 

Custom Formula Box:

 

a.custom_formula_fireball(a, 

Note: b.custom_formula_fireball(a, B) would be equally valid.

 

Script Editor:

 

class Game_Battler < Game_BattlerBase
 def custom_formula_fireball(a, 

 end
end

So all that leaves us to do it put the formula in the method. I’m going to use the gambler example from the previous tutorial:

 

class Game_Battler < Game_BattlerBase
 def custom_formula_fireball(a, 
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;
 end
end

So that’s how to bypass the length limit on the custom formulae. If that’s all you’re interested in then you can stop here. I’ll be carrying onto covering adding a default formula and manipulating it.

 

I’m not going to cover anything to do with developing your formula I’m just going to show you how to implement it, here’s the formula I was using in my last game:

 

class Game_Battler < Game_BattlerBase
 def basef(a,b, magic = false)
if magic
  return ([a.mat / 5, a.mat-b.mdf].max * (a.mat ** 0.5)).to_i
else
  return ([a.atk / 5, a.atk-b.def].max * (a.atk ** 0.5)).to_i
end
 end
end

First thing I’m going to point out is that I also pass a boolean (true or false) to the method to define whether it’s a magic attack or a physical one. You could do the same if you wanted. Just an example on the custom formula box:

 

a.basef(a,b,true)

Anyway the main reason for doing this is so that if you find your formula doesn’t work well later in the game you’ll only have to change it once. You can also simplify formulas if you wanted by making more methods. So for example in my last ill-fated game I had a character who could heal but I didn’t want him to be a dedicated healer so I made all his heals inflict statuses (with the turns they lasted reflecting the power of the heals) and if a character was affected by one of these statuses they only received half-healing. So the method I made looked like this:

 

def sorcery_healing?
 return state?(51) || state?(52) || state?(53)
end

My healing skill custom formulae looked a little like this

 

b.sorcery_healing? ? 100 : 200

Any how that just about concludes everything I wanted to cover in this tutorial, I hope it helped you.

  • Like 3

Share this post


Link to post
Share on other sites

So, for clarification, a.custom_fireball, would be if the actor used it right? What do the parameters mean?

Share this post


Link to post
Share on other sites

So, for clarification, a.custom_fireball, would be if the actor used it right? What do the parameters mean?

No custom_formula_fireball exists in both Game_Actor and Game_Enemy whether or not an enemy can use the skill that calls it.

a & b are the same as in the custom formula box.

Share this post


Link to post
Share on other sites

So, for clarification, a.custom_fireball, would be if the actor used it right? What do the parameters mean?

 

if you define something in Game_Battler, then both _Enemy and _Actor can do it, since they both got it from the same parent class. :)

Share this post


Link to post
Share on other sites

I can't seem to get this to work for me. If I do something like:

class Game_Battler < Game_BattlerBase
  def damage(a, 
	100
  end
end

and then put, "a.damage" in the skills formula box, it does no damage to the enemy I use it on. What am I doing wrong?

 

POST EDIT:

 

I removed the (a, b,) and it works in this particular case. But then if I make a skill that uses battler stats ( like a.atk or b.def) it doesn't seem to work unless I put the (a, b,) back into the def.

 

So I guess I'm confused about what the (a, b,) is there for. (had to re-edit out smiley faces... which now I know why OP might've used capital B's).

Edited by StrayBalloon

Share this post


Link to post
Share on other sites

Let’s start by quickly analysing the formula box itself; as I’m sure you are aware a refers to the the attacker or user and b refers to the defender or recipient, now a and b refer either to an instance of Game_Actor or Game_Enemy. A quick glance at this class diagram shows us that both inherit from Game_Battler (which in turn inherits from Game_BattlerBase).

 

game_battler_inheritance.png

 

 

Um... not to be nitpicky but, the arrows in this diagram seems backwards to me. :P

 

Anyway, if you are running the method on a, why bother passing a to the method? Just uses self instead:

class Game_Battler < Game_BattlerBase
  def basef(b, magic = false)
    if magic
     return ([self.mat / 5, self.mat-b.mdf].max * (self.mat ** 0.5)).to_i
    else
     return ([self.atk / 5, self.atk-b.def].max * (self.atk ** 0.5)).to_i
    end
  end
end

If anyone is curious about using modules, there is a trick to them. You have to use self before the method name like this:

module DamageFormulas
  def self.basef(a,b, magic = false)
    if magic
     return ([a.mat / 5, a.mat-b.mdf].max * (a.mat ** 0.5)).to_i
    else
     return ([a.atk / 5, a.atk-b.def].max * (a.atk ** 0.5)).to_i
    end
  end
end

then you can just call the method from the module object like

DamageFormulas.basef(a,b,true)

also:

 

I can't seem to get this to work for me. If I do something like:

class Game_Battler < Game_BattlerBase
  def damage(a, 
	100
  end
end

and then put, "a.damage" in the skills formula box, it does no damage to the enemy I use it on. What am I doing wrong?

 

POST EDIT:

 

I removed the (a, b,) and it works in this particular case. But then if I make a skill that uses battler stats ( like a.atk or b.def) it doesn't seem to work unless I put the (a, b,) back into the def.

 

So I guess I'm confused about what the (a, b,) is there for. (had to re-edit out smiley faces... which now I know why OP might've used capital B's).

 

The (a, b ) means the method takes arguments.  Because a and b are just local variables, and you have to tell your little formula method what a and b actually mean. so if you have them after the name, when you call it it will expect you to also put an a and b when you call the method, and consistently if you don't tell the method to use a and b it will not expect you to call, it with anything.

 

I guess all that got deeper into scripting then people really cared about huh? But this basically is scripting at this point.

Edited by KilloZapit

Share this post


Link to post
Share on other sites

a is the user and b is self

 

no, a is the user and b is the target. :P

Share this post


Link to post
Share on other sites

value = item.damage.eval(user, self, $game_variables)

 

def eval(a, b, v)

 

:/

Share this post


Link to post
Share on other sites

But "self" has a particular meaning though, it means the current object. Once you pass it to a method on another object, it's not self anymore. That's why it's better to call it the target. Though if you want, you can call b.whatever(a) and use self to refer to b in your formula. It's arbitrary at that point.

 

Edit: Also in the RPG Maker VX Ace Help file:

 

 


eval(a, b, v)
Evaluates the formula. The action-side battler, target-side battler, and in-game variable array ($game_variables) are specified by a, b, and v, respectively.

Returns a negative value if recovery.

Edited by KilloZapit
  • Like 1

Share this post


Link to post
Share on other sites

The (a, b ) means the method takes arguments.  Because a and b are just local variables, and you have to tell your little formula method what a and b actually mean. so if you have them after the name, when you call it it will expect you to also put an a and b when you call the method, and consistently if you don't tell the method to use a and b it will not expect you to call, it with anything.

 

I guess all that got deeper into scripting then people really cared about huh? But this basically is scripting at this point.

 

 

 

I figured it had something to do with variables. So basically if the value is determined using things like a.atk or b.tp, then it needs the (a,  b ).

 

I'm gonna need to toy around with this more to better understand how it works.

Share this post


Link to post
Share on other sites

Yeah exactly! Hope you can figure it out! *sprinkles fairy dust on you*

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

×
Top ArrowTop Arrow Highlighted