Updated Implementing Catch EXP (markdown)

DavidJCobb 2023-11-16 02:32:17 +01:00
parent f8f18dfe2c
commit 2adfa9e777
1 changed files with 5 additions and 5 deletions

@ -132,9 +132,9 @@ We need to understand this stuff because `getexp`... doesn't work like that.
### How `getexp` works
The `getexp` command has to do a *lot* of calculations, and Game Freak didn't want to lag the game, so they decided to split those calculations into six batches. The `getexp` command relies on a counter in order to know what batch it needs to run next. The counter is called `gBattleScripting.getexpState` in C, and `sGIVEEXP_STATE` in scripts: that's our `setbyte sGIVEEXP_STATE, 0` command.
The `getexp` command has to do a *lot* of calculations, and Game Freak didn't want to lag the game, so they decided to split those calculations into six batches. The `getexp` command relies on a counter in order to know what batch it needs to run next. The counter is called `gBattleScripting.getexpState` in C, and `sGIVEEXP_STATE` in scripts: that's why we always have to run a `setbyte sGIVEEXP_STATE, 0` script command before we run `getexp`.
Here's the clever devilry: `getexp` doesn't increase `gBattlescriptCurrInstr` when it finishes, *unless* it's finished the sixth batch. So what ends up happening?
Here's the clever trick: `getexp` doesn't increase `gBattlescriptCurrInstr` when it finishes, unless it's finished the sixth batch. So what ends up happening before that batch?
1. The script sets `gBattleScripting.getexpState` to 0.
1. The script runs `getexp`, so `gBattlescriptCurrInstr` points to the `getexp` instruction in the script.
@ -200,11 +200,11 @@ BattleScript_PrintCaughtMonInfo::
That's everything that happens before we try to `getexp`, and hm, nope, there's nothing in there that we can check for. Okay, but how do we actually *get* to the `BattleScript_SuccessBallThrow` label? If we try to Ctrl + F the battle script files, we won't find any other uses of the label name.
Well, here's the trick. Remember back in the earlier explanation of the catch script, when I said that the lines that aren't indented, and that end with two colons, are *labels* that define *named locations* for code, and we can reference them from C code, and we can also "jump" to them if certain conditions are met?
Well, here's the trick. Remember when I showed you the catch script, and I said that the lines that aren't indented and that end with two colons are *labels* that define *named locations* for code, and we can reference them from C code, and we can also "jump" to them if certain conditions are met?
> The lines that aren't indented, and that end with two colons, are *labels*. That is, they define *named locations* for code. We can reference them from C code (for example, the hardcoded game engine can decide to run the script code starting at `BattleScript_SuccessBallThrow`), and we can also "jump" to them if certain conditions are met.
You might be able to see where this is going: when the game decides that you've successfully caught a Pokémon, it sets the `gBattlescriptCurrInstr` "arrow" to point to `BattleScript_SuccessBallThrow`. Everything in battles is scripted except when it isn't, so let's Ctrl + F inside of [`src/battle_script_commands.c`](https://github.com/pret/pokeemerald/blob/master/src/battle_script_commands.c) for `BattleScript_SuccessBallThrow`.
Here's where that comes into play. When the game decides that you've successfully caught a Pokémon, it sets the `gBattlescriptCurrInstr` "arrow" to point to `BattleScript_SuccessBallThrow`. Everything in battles is scripted except when it isn't, so let's Ctrl + F inside of [`src/battle_script_commands.c`](https://github.com/pret/pokeemerald/blob/master/src/battle_script_commands.c) for `BattleScript_SuccessBallThrow`.
That takes us to line 9938, which is in the middle of `Cmd_handleballthrow`. There are actually two different places where we use C to have the script jump to `BattleScript_SuccessBallThrow` -- and an important thing to understand is that from the script's perspective, the jump is instant, but from C's perspective, we only jump when it's time to run the next script instruction; the rest of `Cmd_handleballthrow` still runs. So what does it do?
@ -218,7 +218,7 @@ We *set* that information. We can *get* that information.
## Back to the music code!
You can look up the `GetMonData` and `SetMonData` functions in `include/pokemon.h` to see how they work, but I'll save you the trouble for now. Here's how we want to change `getexp`:
You can look up the `GetMonData` and `SetMonData` functions in [`include/pokemon.h`](https://github.com/pret/pokeemerald/blob/master/include/pokemon.h) to see how they work, but I'll save you the trouble for now. Here's how we want to change `getexp`:
```diff
// music change in wild battle after fainting a poke