Skip to main content

Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
TagsGame Engines

RPG Maker MZ/MV: A collection of useful JavaScript conditions for Conditional Branches

A topic by TomazellaGames created 10 days ago Views: 111 Replies: 2
Viewing posts 1 to 2
(+2)

I kept reusing the same handful of script conditions across projects, so I figured I'd share them here. These go in Conditional Branch → Script (the last tab), and most work in both MV and MZ unless noted. I'm a RPG Maker MZ dev/user, so I'm more used to it, but a lot of JavaScript stuff is the same on MZ and MV.

All script conditions goes into the command "Conditional Branch...", page 4, script, as the image bellow.

CDN media

Party & actors

  • Check if a specific actor is in the current party:
$gameParty.members().some(a => a.actorId() === 3)
  • Check if a specific actor is alive:
$gameActors.actor(3).isAlive()
  • Check if the whole party is above half HP (useful before a boss gate):
$gameParty.members().every(a => a.hp >= a.mhp / 2)
  • Check the current leader (party member 0):
$gameParty.leader().actorId() === 1
  • Check if a specific party member is alive (0 is the leader):
$gameParty.members()[1].isAlive()

Equipment & items

  • Check if any party member has a specific weapon equipped:
$gameParty.members().some(a => a.weapons().some(w => w && w.id === 5))
  • Check how many of an item the party is carrying:
$gameParty.numItems($dataItems[7]) >= 3
  • Check if the party has at least one Key Item (handy for "you're not ready yet" gates):
$gameParty.items().some(item => item.itypeId === 2)

Switches, variables & time

  • Check if ALL switches in a range are ON (useful for "collect all X" quests):
[10, 11, 12, 13].every(id => $gameSwitches.value(id))
  • Check if ANY switches in a range is ON:
[10, 11, 12, 13].some(id => $gameSwitches.value(id))
  • Check if EXACTLY ONE switch in a range is ON:
[10, 11, 12, 13].filter(id => $gameSwitches.value(id)).length === 1
  • Check if ALL switches in a range are OFF:
!([10, 11, 12, 13].some(id => $gameSwitches.value(id)))
  • Check if a variable is within a range instead of an exact match:
$gameVariables.value(5) >= 10 && $gameVariables.value(5) <= 20
  • Check total playtime in seconds (MV/MZ):
$gameSystem.playtime() >= 3600 // 1 hour+

Map & position

  • Check if the player is facing a specific direction:
$gamePlayer.direction() === 8 // 2=down, 4=left, 6=right, 8=up
  • Check distance between player and an event (in tiles):
$gameMap.distance($gamePlayer.x, $gamePlayer.y, $gameMap.event(5).x, $gameMap.event(5).y) <= 2
  • Check if the player is currently on a specific region ID (MZ):
$gamePlayer.regionId() === 4

Misc that saved me a few times

  • Check if it's currently night by variable (paired with a day/night plugin):
$gameVariables.value(1) >= 18 || $gameVariables.value(1) < 6 // Var 1 should be the hour, and check if in between 6pm and 6am
  • Check gold with a comparison instead of the default Gold condition (lets you use >= inside a bigger expression):
$gameParty.gold() >= 500 && $gameSwitches.value(20)

And...

If you find any // things after two slashes, they are comments and are not considered in JavaScript conditions or anywhere with javascript.

You don't need to be attached to my specific examples. You can mix then, like to know if the player has more gold than the play time (in seconds), you can check:

$gameParty.gold() > $gameSystem.playtime() // or any "useless" check you want

Hope these save someone some time. If you've got your own go-to conditions, drop them below - always happy to steal good ideas.

(+2)

Useful list. One thing worth adding for anyone copying the party member line: $gameParty.members()[1].isAlive() throws a TypeError when the party has only one member, and a script error inside a conditional branch stops the game. A guard keeps it safe:

$gameParty.members()[1] && $gameParty.members()[1].isAlive()

Same idea for $gameMap.event(5). It returns undefined on any map without an event 5, so a common event that runs on several maps can crash on it.

A few we use a lot, all fine in MV and MZ:

Another event's self switch (event 5, switch A, this map):

$gameSelfSwitches.value([$gameMap.mapId(), 5, 'A'])

Anyone in the party knows skill 10:

$gameParty.members().some(a => a.hasSkill(10))

Actor 1 has state 4:

$gameActors.actor(1).isStateAffected(4)

Player is in any vehicle:

$gamePlayer.isInVehicle()

(+1)

Awesome! Thank you for the follow up and the new additions. I didn't get into the only one party member alive error yet, didn't try this case scenario. Thanks a lot for pointing that!