- 453
- Posts
- 11
- Years
- Seen Apr 22, 2025
This is the script I made that enables you to make custom trainer battles without editing the trainers
PBS file.
How to use
Make a new script in the script editor, name it however you like, say CustomTrainers
and copy and paste this code inside.
To use it in the game, make a new event that looks like this:
The game will enter the conditional branch if you won against the trainer, and that's where we
activate the self switch A on. And here's the second page of the event.
The script code:
You can change the number of pokemon the trainer has, the species, levels, everything. Once you
created a pokemon with createPokemon method, you can edit the pokemon's hp, IVs, EVs, stats,
and anything else.
For example, let's make a trainer that has only one pokemon, a pikachu, on level 13, and let's give
that pikachu some HP IVs, and let's make his hp half of his total hp. We'll set the id of the trainer
to 7, and make her name "Alice". The part of the event that changes is the script:
Hope this comes in handy in your game too. I'm using this system instead of the original one.
No credits needed since half of the code is copied from essentials, but if you want, you can
give me half a credit!
PBS file.
How to use
Make a new script in the script editor, name it however you like, say CustomTrainers
and copy and paste this code inside.
Spoiler:
Code:
# ------------------------------------------------------------------------------
# Written by Stochastic, except for customTrainerBattle method which is a
# modified version of pbTrainerBattle method.
# ------------------------------------------------------------------------------
BR_DRAW = 5
BR_LOSS = 2
BR_WIN = 1
# ------------------------------------------------------------------------------
# species - Name of the species, e.g. "PIKACHU"
# level - Level
# moveset - Optional. Array of moves, e.g. [:MUDSLAP, :THUNDERBOLT, :VINEWHIP]
# If not specified, pokemon will be created with moves learned by leveling.
# The pokemon doesn't need to be able to learn the given moves, they can be
# arbitary.
# ------------------------------------------------------------------------------
def createPokemon(species, level, moveset=nil)
begin
poke = PokeBattle_Pokemon.new(species, level)
poke.moves = convertMoves(moveset) if moveset
poke.shinyflag = false
return poke
rescue
return PokeBattle_Pokemon.new("PIKACHU", 5)
end
end
def convertMoves(moves)
moves.map! {|m| PBMove.new(getMoveID(m))}
return moves
end
# provide move like this; :TACKLE
def getMoveID(move)
return getConst(PBMoves,move)
end
# ------------------------------------------------------------------------------
# Creates a trainer with specified id, name, party, and optionally, items.
# Does not depend on defined trainers, only on trainer types
# ------------------------------------------------------------------------------
def createTrainer(trainerid,trainername,party,items=[])
name = pbGetMessageFromHash(MessageTypes::TrainerNames, trainername)
opponent = PokeBattle_Trainer.new(name, trainerid)
opponent.setForeignID($Trainer) if $Trainer
opponent.party = party
return [opponent,items,party]
end
# ------------------------------------------------------------------------------
# Initiates trainer battle. This is a modified pbTrainerBattle method.
#
# trainer - custom PokeBattle_Trainer provided by the user
# endspeech - what the trainer says in-battle when defeated
# doublebattle - Optional. Set it to true if you want a double battle
# canlose - Optional. Set it to true if you want your party to be healed after battle,
and if you don't want to be sent to a pokemon center if you lose
# ------------------------------------------------------------------------------
def customTrainerBattle(trainer,endspeech,doublebattle=false,canlose=false)
trainerparty=0 # added by SH
if $Trainer.pokemonCount==0
Kernel.pbMessage(_INTL("SKIPPING BATTLE...")) if $DEBUG
return BR_LOSS # changed by SH
end
if !$PokemonTemp.waitingTrainer && $Trainer.ablePokemonCount>1 &&
pbMapInterpreterRunning?
thisEvent=pbMapInterpreter.get_character(0)
triggeredEvents=$game_player.pbTriggeredTrainerEvents([2],false)
otherEvent=[]
for i in triggeredEvents
if i.id!=thisEvent.id && !$game_self_switches[[$game_map.map_id,i.id,"A"]]
otherEvent.push(i)
end
end
if otherEvent.length==1
if trainer[2].length<=3
$PokemonTemp.waitingTrainer=[trainer,thisEvent.id,endspeech,doublebattle]
return BR_LOSS # changed by SH
end
end
end
if $PokemonGlobal.partner && ($PokemonTemp.waitingTrainer || doublebattle)
othertrainer=PokeBattle_Trainer.new(
$PokemonGlobal.partner[1],$PokemonGlobal.partner[0])
othertrainer.id=$PokemonGlobal.partner[2]
othertrainer.party=$PokemonGlobal.partner[3]
playerparty=[]
for i in 0...$Trainer.party.length
playerparty[i]=$Trainer.party[i]
end
for i in 0...othertrainer.party.length
playerparty[6+i]=othertrainer.party[i]
end
fullparty1=true
playertrainer=[$Trainer,othertrainer]
doublebattle=true
else
playerparty=$Trainer.party
playertrainer=$Trainer
fullparty1=false
end
if $PokemonTemp.waitingTrainer
combinedParty=[]
fullparty2=false
if false
if $PokemonTemp.waitingTrainer[0][2].length>3
raise _INTL("Opponent 1's party has more than three Pokémon, which is not allowed")
end
if trainer[2].length>3
raise _INTL("Opponent 2's party has more than three Pokémon, which is not allowed")
end
elsif $PokemonTemp.waitingTrainer[0][2].length>3 || trainer[2].length>3
for i in 0...$PokemonTemp.waitingTrainer[0][2].length
combinedParty[i]=$PokemonTemp.waitingTrainer[0][2][i]
end
for i in 0...trainer[2].length
combinedParty[6+i]=trainer[2][i]
end
fullparty2=true
else
for i in 0...$PokemonTemp.waitingTrainer[0][2].length
combinedParty[i]=$PokemonTemp.waitingTrainer[0][2][i]
end
for i in 0...trainer[2].length
combinedParty[3+i]=trainer[2][i]
end
fullparty2=false
end
scene=pbNewBattleScene
battle=PokeBattle_Battle.new(scene,playerparty,combinedParty,playertrainer,
[$PokemonTemp.waitingTrainer[0][0],trainer[0]])
trainerbgm=pbGetTrainerBattleBGM(
[$PokemonTemp.waitingTrainer[0][0],trainer[0]])
battle.fullparty1=fullparty1
battle.fullparty2=fullparty2
battle.doublebattle=battle.pbDoubleBattleAllowed?()
battle.endspeech=$PokemonTemp.waitingTrainer[2]
battle.endspeech2=endspeech
battle.items=[$PokemonTemp.waitingTrainer[0][1],trainer[1]]
else
scene=pbNewBattleScene
battle=PokeBattle_Battle.new(scene,playerparty,trainer[2],playertrainer,trainer[0])
battle.fullparty1=fullparty1
battle.doublebattle=doublebattle ? battle.pbDoubleBattleAllowed?() : false
battle.endspeech=endspeech
battle.items=trainer[1]
trainerbgm=pbGetTrainerBattleBGM(trainer[0])
end
if Input.press?(Input::CTRL) && $DEBUG
Kernel.pbMessage(_INTL("SKIPPING BATTLE..."))
Kernel.pbMessage(_INTL("AFTER LOSING..."))
Kernel.pbMessage(battle.endspeech)
Kernel.pbMessage(battle.endspeech2) if battle.endspeech2
if $PokemonTemp.waitingTrainer
pbMapInterpreter.pbSetSelfSwitch(
$PokemonTemp.waitingTrainer[1],"A",true
)
$PokemonTemp.waitingTrainer=nil
end
return BR_WIN # changed by SH
end
Events.onStartBattle.trigger(nil,nil)
battle.internalbattle=true
pbPrepareBattle(battle)
restorebgm=true
decision=0
pbBattleAnimation(trainerbgm,trainer[0].trainertype,trainer[0].name) {
pbSceneStandby {
decision=battle.pbStartBattle(canlose)
}
if $PokemonGlobal.partner
pbHealAll
for i in $PokemonGlobal.partner[3]; i.heal; end
end
if decision==2 || decision==5
if canlose
for i in $Trainer.party; i.heal; end
for i in 0...10
Graphics.update
end
else
$game_system.bgm_unpause
$game_system.bgs_unpause
Kernel.pbStartOver
end
else
Events.onEndBattle.trigger(nil,decision)
if decision==1
if $PokemonTemp.waitingTrainer
pbMapInterpreter.pbSetSelfSwitch(
$PokemonTemp.waitingTrainer[1],"A",true
)
end
end
end
}
Input.update
$PokemonTemp.waitingTrainer=nil
return decision # changed by SH
end
To use it in the game, make a new event that looks like this:
Spoiler:
![[PokeCommunity.com] Custom trainer battles [PokeCommunity.com] Custom trainer battles](https://i.imgur.com/BXWOsJJ.png)
The game will enter the conditional branch if you won against the trainer, and that's where we
activate the self switch A on. And here's the second page of the event.
Spoiler:
![[PokeCommunity.com] Custom trainer battles [PokeCommunity.com] Custom trainer battles](https://i.imgur.com/e01hQcg.png)
The script code:
Spoiler:
Code:
p0 = createPokemon("KLANG", 38)
p1 = createPokemon("VOLTORB", 39)
p2 = createPokemon("RAICHU", 39)
party = [p0, p1, p2]
trainer = createTrainer(137, "Rashad", party)
result = customTrainerBattle(trainer, "Oh no, you defeated me!")
pbSet(1, result == BR_WIN ? 0 : 1)
You can change the number of pokemon the trainer has, the species, levels, everything. Once you
created a pokemon with createPokemon method, you can edit the pokemon's hp, IVs, EVs, stats,
and anything else.
For example, let's make a trainer that has only one pokemon, a pikachu, on level 13, and let's give
that pikachu some HP IVs, and let's make his hp half of his total hp. We'll set the id of the trainer
to 7, and make her name "Alice". The part of the event that changes is the script:
Spoiler:
Code:
p0 = createPokemon("PIKACHU", 13)
p0.iv[0] = 27
p0.hp = p0.totalhp / 2
party = [p0]
trainer = createTrainer(7, "Alice", party)
result = customTrainerBattle(trainer, "...")
pbSet(1, result == BR_WIN ? 0 : 1)
Hope this comes in handy in your game too. I'm using this system instead of the original one.
No credits needed since half of the code is copied from essentials, but if you want, you can
give me half a credit!
Last edited: