Using Conditional Branch Event Commands for Script Input

This tutorial describes how conditional branch commands are used as script input. This command is a very convenient way to ask users to specify input, especially if you're working with conditions. This may be easier to use



Instead of this



This tutorial shows how to use these commands in your scripts, and how you can achieve complex logic using only these commands.

trans.gif
Recall...

If you are not familiar with the conditional branch command, the image below shows all of the built-in conditions that are available for users.



It also comes with a script box that has a character limit of 10000, which is enough for almost anything anyone would need to do with a script call.

Ideally, if you're working with any event (map event, troop event, common event), and your script uses conditions, you want to use a conditional branch command.

Script Parsing

The conditional branch command is command 111 in the game interpreter. You can look at it in your script editor to see how it is evaluated. Typically, I would just copy this code into my own method and adapt it to suit my own needs.

To actually get the command, you will need to parse the event list. Depending on what type of event you're working with, accessing the list would be a bit different, but once you have access to the list, you would simply iterate over each command as follows to find the command you want:

list.each do |cmd| if cmd.code == 111 # do something with the conditional branch command endendExamples
Here are a couple scripts that use conditional branch commands for input.

Logical Expressions
The conditional branch command allows you to ask questions like

  • Is Actor 3 a Soldier class?
  • Does the party have a Hand Ax in their inventory
Which can be evaluated easily. However, what happens when you want to ask more complex questions such as
  • Is Actor 3 a Soldier AND is Actor 3 wearing a Hat?
  • Is switch 1 on OR Switch 2 on?
These are separate conditions joined one or more of the following logical operators:
  • AND
  • OR
  • NOT
This is very easy to accomplish using script calls, but remember the point is to make it easy for users who may not be proficient in ruby. Some users may not even have any idea how programming works.The following rules are proposed for logical expressions using conditional branch commands. Here are some examples to illustrate it:

1. One command represents one condition.

2. Every pair of commands is joined by a logical operator.

3. Two commands with the same indent are joined using OR

 

4. Two commands with a nested indent are joined using AND

 

5. All conditions with non-decreasing indents are grouped together.

  

Condition 0 = indent 0

Condition 1 = indent 1, therefore they are grouped together

Condition 2 = indent 0, so this is the start of a new group

Condition 3 = indent 1

6. All conditions are parsed recursively. Parentheses are based on groups



The last image basically summarizes all of the rules. It may take some time to understand it, but if you think of it as a regular boolean expression, it should become clearer:

(a AND (b OR c))We are simply applying each rule one by one as you go down the conditions, grouping them based on the non-decreasing indentation rule.Note that negation is not shown here. This is because the default conditional branches do not support negation.

Ending

With this in mind, you should be able to provide your users with a much easier way to specify conditions if your script needs it.

Originally posted at Hime Works.
A nice wholesome walkthrough. Well done.
Already knew this, but this is a GREAT tutorial for beginners.  This should help them to understand how to do some very complex forks.
I haven't seen any scripts implement this kind of command parsing though. Still have to get around to doing it myself. These are just the specs for the most part lol
Actually now that I look at what I wrote...this stuff is "basic" eventing logic.


I was only concerned with finding a way to standardize conditional branch parsing for scripts and was completely oblivious to the fact that this is probably how people normally make events, and it's the simplest way to make complex conditional logic.


I can see where djDarkX is coming from now.


That's great because then people don't have to change the way they might already be doing things.
Sorry to necro this thread, but because the image links no longer work, I can't see the examples.


I want to know how to set precedence for multiple operators. Basically, how do I make the equivalent of 


(a AND b) or (c AND d)


Is it correct to do it like this:


Conditional Branch: Script: ($game_variables[0003] == 4 && $game_switches[0036] == ON) || ($game_variables[0003] == 5 && $game_switches[0034] == ON)




I ask because I can't seem to find any tutorials addressing the syntax for setting the precedence of multiple operators, like, anywhere. Which makes me concerned that there's something wrong with this method and/or that it shouldn't be done. And are regular parentheses the way to do it? I could just use two different if statements here, but that's messy and seems unnecessary.

Original thread: https://forums.rpgmakerweb.com/threads/using-conditional-branch-event-commands-for-script-input.21223/