MV Formula Library/Conditional Formulas

From HimeWorks
< MV Formula Library
Revision as of 13:29, 29 February 2016 by Hime (Talk | contribs) (Created page with "==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...")

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

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.

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. 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 an enemy.
  • 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.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 === 3