The PokéCommunity Forums Secret ID

 Pokémon Essentials All questions and discussion about Pokémon Essentials, the Pokémon game kit for RPG Maker XP, go in here. Also contains links to the latest downloads and the Essentials Wiki.

#1
September 14th, 2013 (7:08 AM).
 Badhaas Join Date: Jul 2013 Gender: Male Posts: 118
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?

#2
September 14th, 2013 (8:58 AM).
 Maruno Lead Dev of Pokémon Essentials Join Date: Jan 2008 Location: England Posts: 5,133
\$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.
__________________
#3
September 14th, 2013 (10:14 AM).
 Badhaas Join Date: Jul 2013 Gender: Male Posts: 118
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.
#4
September 14th, 2013 (10:38 AM).
 Darkroman Join Date: Oct 2011 Gender: Male Nature: Adamant Posts: 181
I'd suggest looking into some binary truth tables, and understand what AND, OR, NOT, and XOR operations do. It'll come in handy.
#5
September 17th, 2013 (6:40 AM).
 Badhaas Join Date: Jul 2013 Gender: Male Posts: 118
Thanks for the tip. I've looked into it and it actually seems quite simple, though still very useful to know.
__________________

View my profile page for scripts/tutorials made by me, even ones which aren't posted yet.
Currently working on a demo for Tales from the Pokémon World (based on the World of Pokémon project)

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On [icon] codes HTML code is Off Forum Rules
 Forum Jump Dashboard Private Messages Subscriptions Who's Online Search Forums Forums Home Community Square     Main Street: Meet & Greet!         Celebrations!     City Hall: Feedback & Support     PokéCommunity Daily         Published Piece Archive     The Bulletin Board PokéCommunity Get-Together 14     The Event Plaza Main Series Pokémon Gaming     Pokémon Sun & Pokémon Moon     Omega Ruby & Alpha Sapphire     Pokémon X & Pokémon Y     Fifth Generation     Fourth Generation     Third Generation     Second Generation     First Generation Pokémon Gaming     Pokémon GO         Pokémon Field Journals     Pokémon Gaming Central     Pokkén Tournament     Challenges     Trade Corner         Trade Shops         Quick Trades         Trader Reviews     Battling & Team Building         The PokéCommunity League         Events & Groups Approval The Wide World of Pokémon     Pokémon General     Pokémon Anime         Episode Discussion     Pokémon Trivia     Pokémon Trading Card Game         TCG Deck Reviewing         Custom Cards ROM Hacking     ROM Hacking Hub         Beginner’s Lounge     Hacks Studio         Progressing Hacks         Sideshow Showcase     Research & Development     Tools, Tutorials & Resources Create & Discover     Art & Design         A&D School of the Arts     Creative Media     Roleplay Theatre         Roleplay Casting         Roleplay Stage     Fan Fiction and Writing         Writer's Lounge         Fanfiction Archive     Game Development         Games Showcase         Progressing Games         Resources         Scripts & Tutorials         Pokémon Essentials Entertainment & Hobbies     General Entertainment     Anime & Manga     Video Games     Tabletop Gaming Off-Topic Discussions     The Mall     The Treehouse     The Lab     The Round Table     The Playground     The Underground

All times are GMT -8. The time now is 4:31 PM.