• Our software update is now concluded. You will need to reset your password to log in. In order to do this, you will have to click "Log in" in the top right corner and then "Forgot your password?".
  • Forum moderator applications are now open! Click here for details.
  • 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.

Secret ID

119
Posts
10
Years
  • Seen Sep 1, 2023
I know there is a function in essentials to return the player´s public Trainer ID ($Trainer.publicID). Though there isn't a function which returns the player's secret ID; the remainder of the full trainer ID. I've looked at the function (publicID) to try and write the function I want myself, yet I don't understand how publicID works.

Could anyone explain me how PokeBattle_Trainer.publicID works so I can write a function which returns the secret ID?
 

Maruno

Lead Dev of Pokémon Essentials
5,285
Posts
16
Years
$Trainer.id is the player's full 32-bit trainer ID. The upper 16 bits of this number are the secret ID, and the lower 16 bits are the public ID. Both numbers on their own go between 0 and 65535. The two numbers are stuck together in binary form, like so:

Secret ID: 1010 1010 1010 1010
Public ID: 1100 1100 1100 1100
$Trainer.id: 1010 1010 1010 1010 1100 1100 1100 1100

To get either the secret ID or public ID back from this, we're going to have to fiddle with the number's bits. There are several different ways you can do this.

Code:
def publicID(id=nil)
  return id ? id&0xFFFF : (@id&0xFFFF)
end
This is how you get the public ID. What it does is perform a bitwise AND operation on the ID number with the number 0xFFFF (where & is the symbol used for this operator, like how = is the symbol meaning "set to equal" or == means "check if equals"). The number 0xFFFF is a hexadecimal number (you know it's hex because it has 0x at the beginning - 0xFFFF is the number 65535), which in binary is the number 1111 1111 1111 1111. A bitwise AND operation looks at each bit in both numbers in turn, and in the result, makes that bit 1 if the first number's bit is 1 AND the second number's bit is 1 (or 0 otherwise).

$Trainer.id: 1010 1010 1010 1010 1100 1100 1100 1100
0xFFFF: 0000 0000 0000 0000 1111 1111 1111 1111
Result: 0000 0000 0000 0000 1100 1100 1100 1100

There's a bunch of zeroes at the beginning of the result, but just like the number 00042, they can be ignored. The result is the public ID (in blue).



The same approach would work if you want to get the secret ID (the red number). Remember that you can stick zeroes in front of a number, so 0xFFFF is the same as 0x0000FFFF. If that got just the second half of the $Trainer.id, and we now want just the first half, how about using 0xFFFF0000 instead?

$Trainer.id: 1010 1010 1010 1010 1100 1100 1100 1100
0xFFFF0000: 1111 1111 1111 1111 0000 0000 0000 0000
Result: 1010 1010 1010 1010 0000 0000 0000 0000

Success! Of sorts. We've gotten rid of the blue number, but we're still left with a bunch of zeroes at the end. 300 is a different number to 3, so we can't just ignore them. How do we get 3 from 300, then? Divide by 100! In this case, we want to divide by the binary number 1 0000 0000 0000 0000 to get rid of those 16 zeroes. In other words, divide by 65536.

Putting all this together, we get this code:

Code:
def secretID
  return (@id&0xFFFF0000) / 65536
end
This isn't the most elegant solution, but it'll do what you want. The most elegant solution involves bitwise shifting, but you've already got something that works so there's no point trying to explain something new just for the sake of having some slightly prettier code.

My explanation of binary/hexadecimal numbers and the bitwise AND operator is quite poor, I know. There's a nice page on Bulbapedia which explains these things too, and even though it's about calculating a Pokémon's gender/nature/ability/shininess from its personal ID, the maths and explanation is still good.
 
119
Posts
10
Years
  • Seen Sep 1, 2023
Thanks for the detailed reply. I think I get how it works now.

I'm still fairly inexperienced at coding and try to learn to do what I want by looking at other code. I already knew what binary and hexadecimal numbers where, yet wasn't familiar with the AND operator. I also haven't worked with binary and hexadecimal values before.
 
180
Posts
12
Years
  • Age 36
  • Seen Aug 12, 2023
I'd suggest looking into some binary truth tables, and understand what AND, OR, NOT, and XOR operations do. It'll come in handy.
 
119
Posts
10
Years
  • Seen Sep 1, 2023
Thanks for the tip. I've looked into it and it actually seems quite simple, though still very useful to know.
 
Back
Top