Save Unit From Battle Documentation

  • 802C1EC (Save Unit From Battle)

This function runs twice after the battle to gather and overwrite unit data. This is what makes battles work by altering HP, increasing Weapon Rank, increasing experience and decreasing weapon durability.

This is a very long function, so I would take the time to examine and learn as much as you can from it before altering it.

First Proc:
r0 = Attacking Unit
r1 = Attacking Unit Battle Position(Left/Right)

Second Proc:
r0 = Defending Unit
r1 = Defending Unit Battle Position(Left/Right)

To check for whether or not the unit is a player or not, check the Attacking/Defending Unit + 0xB < 0x40

If the returned number is higher than 3F, they are either an NPC or enemy unit.

Once you have checked that the unit is a player, you can do specific functions for a player.

If you want to do specific functions for NPC or enemy units, you check if its between 40 and 7F for NPC and 80 or higher for enemy units.

image

For a step by step explanation:

  1. First it pushes. (If you do not understand what this means, please refer to the ASM Tutorial here)

  2. Then it moves the Unit Data Pointers to r4 and r5.

  3. It then proceeds to update the unit data from r5, which is the post battle data, to r4 which is the unit data struct, in the order: Level - EXP - HP. It does this by ldrb(Loading a single byte) and strb(Storing a single byte).

  4. After this, it goes through the process of changing other battle data, like weapon rank, weapon durability and others.

3 Likes
1 Like

What is this exactly? If you don’t mind me asking. It’s not written in asm. Is it for the non-gba fire emblem engine?

This is the routine written in the C# language if I am reading this right.

1 Like
1 Like

Decompiled code from the FE8 ROM. GBAFE was written in C, therefore since we have the compiler that was used to compile the original source we can decompile the FE8 binary and it compiles back into matching binary. Therefore it’s your function, in C, 1:1 with the ROM.

1 Like

I see. That’s pretty cool. Fsr I thought that the entire thing was written in ASM to begin with.