MV Formula Library/Conditional Formulas

From HimeWorks
< MV Formula Library
Revision as of 11:46, 1 March 2016 by Hime (Talk | contribs) (Input-Based Formulas)

(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.

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>

Input-Based Formulas

All Input methods take a "button", which is a virtual keycode assigned to the game. Multiple keys on your keyboard, gamepad, touch device, and so on can be assigned to the same "button".

The default key assignments for the keyboard, for example, are

  • ok - Z key, Enter key
  • escape - X key, Esc key, Insert key, numpad 0
  • pageup - Q key, pageup key
  • pagedown - W key, pagedown key
  • control - left or right control, alt
  • shift - left or right shift
  • up - Up arrow, Numpad 8
  • down - Down arrow, Numpad 2
  • left - Left arrow, Numpad 4
  • right - Right arrow, Numpad 6
  • tab - tab key
  • debug - F9

With plugins, you can assign additional inputs, or define your own buttons.

  • Input.isPressed(BUTTON)
Returns true if the input assigned to BUTTON is held down</code>
  • Input.isTriggered(BUTTON)
Returns true if the input assigned to BUTTON is pressed once (repeat ignored)</code>
  • Input.isRepeated(BUTTON)
Returns true if the input assigned to BUTTON is pressed repeatedly (6 frame between repeat)
  • Input.isLongPressed(BUTTON)
Returns true if the input assigned to BUTTON has been held down for at least 24 frames

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
  • PARTY.allMembers().contains(ACTOR)
- returns true if the Game_Actor is in the party.
  • PARTY.battleMembers.contains(ACTOR)
- returns true if the Game_Actor is in the battle party
  • PARTY.allMembers.contains(ACTOR) && !PARTY.battleMembers().contains(ACTOR)
- returns true if the Game_Actor is in the reserve party (ie: in party, but not in battle)