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

From HimeWorks
Jump to: navigation, search
(Battler Formulas)
Line 120: Line 120:
 
*<code>ACTOR.level === LEVEL</code>
 
*<code>ACTOR.level === LEVEL</code>
 
:- returns true if the actor's level is equal to the given LEVEL
 
:- returns true if the actor's level is equal to the given LEVEL
 +
 +
==Party Formulas==
 +
 +
These formulas are used for anything related to Game_Party objects.
 +
 +
The word <code>PARTY</code> refers to a Game_Party reference. For example, you can substitute it with <code>$gameParty</code> if you want to reference the current party. If you have plugins such as [http://himeworks.com/2016/02/party-manager-mv/ HimeWorks Party Manager], you may have more parties to work with. Please refer to plugin instructions to see how to reference those parties.
 +
 +
The word <code>ITEM</code> refers to a weapon, armor, or usable item.
 +
 +
*<code>PARTY.hasItem(ITEM)</code>
 +
:- returns true if the party has item 2 in the inventory
 +
 +
*<code>PARTY.numItems(ITEM) === 10 </code>
 +
:- returns true if the party has 10 of the specified item in the inventory
 +
 +
*<code>PARTY.size() === 1</code>
 +
:- returns true if the party has only 1 member
 +
 +
*<code>PARTY.gold() > 1000</code>
 +
:- returns true if the party has more than 1000 gold
 +
 +
*<code>PARTY.steps() < 100</code>
 +
:- returns true if the party has taken less than 100 steps
 +
 +
*<code>PARTY.hasMaxItems(ITEM)</code>
 +
:- returns true if the party has reached the max amount it can hold for wea 5
 +
 +
*<code>PARTY.isAnyMemberEquipped(ITEM)</code>
 +
:- returns true if any party members has the item equipped
 +
 +
*<

Revision as of 08:54, 1 March 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

Party Formulas

These formulas are used for anything related to Game_Party objects.

The word PARTY refers to a Game_Party reference. For example, you can substitute it with $gameParty if you want to reference the current party. If you have plugins such as HimeWorks Party Manager, you may have more parties to work with. Please refer to plugin instructions to see how to reference those parties.

The word ITEM refers to a weapon, armor, or usable item.

  • PARTY.hasItem(ITEM)
- returns true if the party has item 2 in the inventory
  • PARTY.numItems(ITEM) === 10
- returns true if the party has 10 of the specified item in the inventory
  • PARTY.size() === 1
- returns true if the party has only 1 member
  • PARTY.gold() > 1000
- returns true if the party has more than 1000 gold
  • PARTY.steps() < 100
- returns true if the party has taken less than 100 steps
  • PARTY.hasMaxItems(ITEM)
- returns true if the party has reached the max amount it can hold for wea 5
  • PARTY.isAnyMemberEquipped(ITEM)
- returns true if any party members has the item equipped
  • <