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.
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 end end
Examples
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.