Trainer events have a name of "Trainer(X)", where X is the maximum range of vision of the Trainer. Trainers normally consist of two event pages. The first has an Event Touch trigger and has the following structure:
Code:
Script: Kernel.pbNoticePlayer(
: : get_character(0))
Text: Battle me now!
Conditional Branch: Script: pbTrainerBattle(PBTrainers::CAMPER,"Andrew",_I("A very good battle, indeed."))
-- here you can add event commands to be run when the player wins, but this is not necessary
Control Self Switch: A =ON
Branch End
Notice the parameters to pbTrainerBattle. Here is a list of the parameters:
* Parameter 1: Trainer type.
* Parameter 2: Trainer's name. The Trainer type and Trainer name must be found in the Trainer data file or an exception will be raised.
* Parameter 3: Dialogue that the Trainer says when the player wins. This parameter should be wrapped in _I() (with a capital i), to support localization.
* Parameter 4: Optional. Whether the battle is a double battle. The default is false. If the battle is a double battle, an exception will be raised if the player has only one Pokemon, so this case must be checked before the battle begins. For an example, see the two people to the left of the Pokemon Center's PC.
* Parameter 5: Optional. A number to distinguish Trainers with the same Trainer type and Trainer name.
You can play music when a Trainer wants to battle by adding a "Play ME" event command as the first on the list of commands. Also, the Trainer event need not be named "Trainer(X)" if it only needs to be talked to in order to start a battle, as is the case for Gym Leaders and other special Trainers.
The second event page has a condition of "Self Switch A is ON", and usually contains text that the Trainer says after the battle is over. The trigger must be other than Event Touch.
The game system will automatically handle the case when two Trainers come into contact with the player at the same time; in that case, a multi battle will begin if the player has at least two Pokemon.
For Trainer battles that span two events (as is the case with many double battles), the other event's self switch should be turned on when the player wins. You can use the pbSetSelfSwitch function for this.