• Just a reminder that providing specifics on, sharing links to, or naming websites where ROMs can be accessed is against the rules. If your post has any of this information it will be removed.
  • Ever thought it'd be cool to have your art, writing, or challenge runs featured on PokéCommunity? Click here for info - we'd love to spotlight your work!
  • Our weekly protagonist poll is now up! Vote for your favorite Conquest protagonist in the poll by clicking here.
  • Welcome to PokéCommunity! Register now and join one of the best fan communities on the 'net to talk Pokémon and more! We are not affiliated with The Pokémon Company or Nintendo.

[ASM & Hex] How exactly is the trainer/secret id stored on a pokemon?

  • 232
    Posts
    13
    Years
    • Seen Sep 10, 2019
    So I haven't a clue how the trainer/secret id is stored on a pokemon, and my goal here is to be able to use a custom trainer/secret id for the pokemon I select in my party. If celebi's trainer id is 31121 and secret id is 00000, how is it stored?
     
    Last edited:
    You should use the pokemon_getattr/setattr functions already built into the game as the OTID (as mentioned on Bulbapedia) is part of the XOR encryption key and changing it will result in a bad egg.

    To modify the TID with these functions you should use the attribute ID 1 (REQUEST_TID) which allow you to read and modify the original trainer ID without creating a bad egg (these functions handle encrypting and decrypting the Pokemon structure).

    Example code

    Code:
    .thumb
    .align 2
    
    main:
    push {lr}
    @ place trainer ID on the stack to get a pointer to it
    sub sp, #4
    ldr r0, =(31121 | (00000 << 16))
    str r0, [sp]
    ldr r3, =0x0804037C|1 @ setattr
    mov r2, sp @ pointer to ID
    mov r1, #1 @ set trainer ID
    ldr r0, =0x02024284 @ first pokemon in your party
    bl call_via_r3 
    add sp, #4
    pop {pc}
    
    call_via_r3: bx r3
     
    Last edited:
    You should use the pokemon_getattr/setattr functions already built into the game as the OTID (as mentioned on Bulbapedia) is part of the XOR encryption key and changing it will result in a bad egg.

    To modify the TID with these functions you should use the attribute ID 1 (REQUEST_TID) which allow you to read and modify the original trainer ID without creating a bad egg (these functions handle encrypting and decrypting the Pokemon structure).

    Example code

    Code:
    .thumb
    .align 2
    
    main:
    push {lr}
    @ place trainer ID on the stack to get a pointer to it
    sub sp, #4
    ldr r0, =(31121 | (00000 << 16))
    str r0, [sp]
    ldr r3, =0x0804037C|1 @ setattr
    mov r2, sp @ pointer to ID
    mov r1, #1 @ set trainer ID
    ldr r0, =0x02024284 @ first pokemon in your party
    bl call_via_r3 
    add sp, #4
    pop {pc}
    
    call_via_r3: bx r3

    I tried your example code but I got a bad egg. Don't I need to alter the checksum too? How would I recalculate the checksum?
    edit: found pokemon_calc_checksum: .word 0x0803E3E8+1. I'll try modifying your example code by including this.
    edit2: I have no idea how to include it... I tried adding
    Code:
    ldr r3, =0x0803E3E8|1
    bl call_via_r3
    do I also need .encrypt: .word 0x0803F8F9?
     
    Last edited:
    I tried your example code but I got a bad egg. Don't I need to alter the checksum too? How would I recalculate the checksum?
    edit: found pokemon_calc_checksum: .word 0x0803E3E8+1. I'll try modifying your example code by including this.
    edit2: I have no idea how to include it... I tried adding
    Code:
    ldr r3, =0x0803E3E8|1
    bl call_via_r3
    do I also need .encrypt: .word 0x0803F8F9?

    The TID is not contained within the checksum, as the TID field itself is not encrypted. The checksum is made up of only the sum of the unencrypted half words in the encrypted block.

    pokemon_setattr automatically calls pokemon_calc_checksum and pokemon_encrypt for the attributes that require it, and it also decrypts and re-encrypts the Pokemon, so if you're getting a bad egg then something else went wrong. Maybe try changing the "ldr r0, =(31121 | (00000 << 16))" line to something else?
     
    Back
    Top