Difference between revisions of "MV Formula Library/Conditional Formulas"

From HimeWorks
Jump to: navigation, search
m
(Battler Formulas)
Line 107: Line 107:
 
*<code>ACTOR.isLearnedSkill(SKILL_ID)</code>
 
*<code>ACTOR.isLearnedSkill(SKILL_ID)</code>
 
:- returns true if the actor has learned the skill (excluding skills added by traits)
 
:- returns true if the actor has learned the skill (excluding skills added by traits)
 
+
*<code>ACTOR.skills().contains($dataSkills[SKILL_ID])</code>
 +
:- returns true if the actor has the skill (includes learned skills, and added by traits)
 
*<code>ACTOR.isClass($dataClasses[CLASS_ID])</code>
 
*<code>ACTOR.isClass($dataClasses[CLASS_ID])</code>
 
:- returns true if the actor is the specified class (assuming $dataClasses is the class database)
 
:- returns true if the actor is the specified class (assuming $dataClasses is the class database)

Revision as of 15:06, 29 February 2016

Condition Basics

There are some common syntax that you may need to use to translate your conditions into code.

Logical Operators

Logical operators are used when you would like to combine multiple conditions together in a certain way using the following logic:

  • AND
  • OR
  • NOT

For example, assuming you have condition A and condition B, you can create the following conditions:

  • A && B - returns true only if both condition A and condition B are true.
  • A || B - returns true if any of the conditions are true.
  • !A - returns true if condition A is false.

You can combine any number of conditions together. For example, if you had a third condition C, you can write

  • A && B && C - returns true if A and B and C are all true
  • A || B || C - returns true if any of A, B, or C are true

Operators are read from left to right if they have the same precedence. What does that mean?

Operator Precedence

When multiple operators are used, Javascript evaluates them in a certain order. They are not necessarily evaluated from left to right.

This is important, because the meaning of your condition changes depending on how you write it. The order of precedence for logical operators is

  1. NOT !
  2. AND &&
  3. OR ||

For example:

  • A || B && C - returns true if (A is true) or (B and C are true)
  • (A || B) && C - returns true if (A or B is true) and (C is true)

Notice the differences between the two statements. The first statement is not read from left to right, because && has higher precedence than ||.

Parentheses are used to clarify which operate is evaluated first. For more information, read about Operator Presence on MDN.

Inequalities

Inequalities are used to compare two objects. For example, a common use for inequalities is to compare two numbers. In Javascript, you can compare almost anything with each other.

For example, given two numbers A and B,

  • A > B - returns true if A is *greater than* B
  • A >= B - returns true if A is *greater or equal to * B
  • A < B - returns true if A is *less than* B
  • A <= B - returns true if A is *less than or equal to* B
  • A === B - returns true if A is *equal to* B
  • A !== B - returns true if A is *not equal to* B

Precedence is important here as well.

General Formulas

  • $gameVariables.value(3) === 5
Returns true if value of variable 3 is equal to 5
  • $gameSwitches.value(2) === false
Returns true if switch 2 is OFF</code>

Battler Formulas

In the following formulas, BATTLER represents any Game_Actor or Game_Enemy reference. Basically, these are your actors or enemies in the game.

There are many different ways to obtain a reference to a battler, depending on the plugin you're using and where the formula is written.

For example

  • In damage formulas, they are represented as a or b.
  • $gameParty.leader() is the leader of the party
  • $gameActors.actor(3) is game actor 3
  • $gameTroop.members()[2] is the third enemy in the current troop (zero-indexed)

Certain conditions are only available to actors or enemies by default, for example enemies by default do not have levels unless you have a plugin that adds it.

  • BATTLER.isStateAffected(STATE_ID)
- returns true if the battler is affected by the given state
  • BATTLER.isEnemy()
- returns true if the battler is a Game_Enemy
  • BATTLER.isActor()
- returns true if the battler is a Game_Actor.
  • BATTLER.isAlive()
- returns true if the battler is alive
  • BATTLER.isDead()
- returns true if the battler is dead
  • BATTLER.result().hpDamage > 100
- returns true if the HP damage from the current action is greater than 100
  • BATTLER.result().mpDamage < 50
- returns true if the MP damage from the current actio nis less than 50
  • ACTOR.isWtypeEquipped(WTYPE_ID)
- returns true if the actor is holding a weapon of the given type
  • ACTOR.isLearnedSkill(SKILL_ID)
- returns true if the actor has learned the skill (excluding skills added by traits)
  • ACTOR.skills().contains($dataSkills[SKILL_ID])
- returns true if the actor has the skill (includes learned skills, and added by traits)
  • ACTOR.isClass($dataClasses[CLASS_ID])
- returns true if the actor is the specified class (assuming $dataClasses is the class database)
  • ACTOR.weapons().some( function(eq) { return eq && eq.id === WEAPON_ID})
- returns true if the actor is holding the specified weapon
  • ACTOR.armors().some( function(eq) { return eq && eq.id === WEAPON_ID})
- returns true if the actor is holding the specified armor
  • ACTOR.level === LEVEL
- returns true if the actor's level is equal to the given LEVEL