The PokéCommunity Forums  

Go Back   The PokéCommunity Forums > Creative Discussions > Game Development > Pokémon Essentials
Sign Up Rules/FAQ Live Battle Blogs Mark Forums Read

Notices

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.


Advertise here

Reply
Click here to go to the first staff post in this thread.  
Thread Tools
  #1    
Old September 14th, 2013, 07:08 AM
Badhaas's Avatar
Badhaas
 
Join Date: Jul 2013
Gender: Male

Advertise here
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?
Reply With Quote
  #2    
Old September 14th, 2013, 08:58 AM
Maruno's Avatar
Maruno
Lead Dev of Pokémon Essentials
 
Join Date: Jan 2008
Location: England
$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.
__________________
Reply With Quote
  #3    
Old September 14th, 2013, 10:14 AM
Badhaas's Avatar
Badhaas
 
Join Date: Jul 2013
Gender: Male
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.
Reply With Quote
  #4    
Old September 14th, 2013, 10:38 AM
Darkroman
 
Join Date: Oct 2011
Gender: Male
Nature: Adamant
I'd suggest looking into some binary truth tables, and understand what AND, OR, NOT, and XOR operations do. It'll come in handy.
Reply With Quote
  #5    
Old September 17th, 2013, 06:40 AM
Badhaas's Avatar
Badhaas
 
Join Date: Jul 2013
Gender: Male
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.
Reply With Quote
Reply
Quick Reply

Sponsored Links


Advertise here
Thread Tools

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
HTML code is Off
Minimum Characters Per Post: 25



All times are UTC -8. The time now is 06:11 AM.


Style by Nymphadora, artwork by Sa-Dui.
Like our Facebook Page Follow us on Twitter © 2002 - 2014 The PokéCommunity™, pokecommunity.com.
Pokémon characters and images belong to The Pokémon Company International and Nintendo. This website is in no way affiliated with or endorsed by Nintendo, Creatures, GAMEFREAK, The Pokémon Company or The Pokémon Company International. We just love Pokémon.
All forum styles, their images (unless noted otherwise) and site designs are © 2002 - 2014 The PokéCommunity / PokéCommunity.com.
PokéCommunity™ is a trademark of The PokéCommunity. All rights reserved. Sponsor advertisements do not imply our endorsement of that product or service. User generated content remains the property of its creator.