The PokéCommunity Forums  

Go Back   The PokéCommunity Forums > Fan Games > Binary ROM Hacking
Reload this Page Other When to Use Vars instead of Flags?

Notices
For all updates, view the main page.

Binary ROM Hacking Need a helping hand or just want to talk about binary ROM hacks? Get comments and answers to any ROM Hacking-related problems, questions or thoughts you have here.

Ad Content
Reply
 
Thread Tools
  #1   Link to this post, but load the entire thread.  
Old August 5th, 2015 (9:17 PM).
Sayuri's Avatar
Sayuri Sayuri is offline
人生忘れられた
 
Join Date: Jun 2015
Location: Boston, MA
Age: 24
Gender: Female
Nature: Lonely
Posts: 276
Can anyone explain to me when to use vars instead of flags and vice versa? I've always had trouble with flags.
Reply With Quote
  #2   Link to this post, but load the entire thread.  
Old August 6th, 2015 (12:45 AM).
Touched's Avatar
Touched Touched is offline
Resident ASMAGICIAN
 
Join Date: Jul 2014
Gender: Male
Posts: 625
Flags are booleans. They're for when you want a simple true/false, yes/no or on/off condition, e.g. "has the player completed this event?", "is this NPC visible?", "do I have this gym badge?", etc. As you can see, these are binary (two choices) questions, so it makes sense to represent them that way in memory to save space. As each flag only uses one bit, you can have 16 flags in the space that a variable uses. Most events are binary, so this turns out to be pretty useful.

Variables are for when you want to store more information, such as a step counter, the non-binary state of some event, the X/Y position of the player, etc. You can perform arithmetic on variables (only add/subtract with default script commands). Variables are in a sense, more powerful than flags - but in most cases they are not needed. Variables are 2 bytes (16 bits) and thus can store values from 0 to 65535 inclusive (2^16-1).

There are some places you absolutely need to use one or the other. Flags can be attached to NPCs which then control their visibility when you toggle them. Variables control script triggers and level scripts.

The question you need to ask when choosing is "Can this be represented as a binary choice?". If the choice is between exactly 2 distinct things, a flag is a good choice. However, if you need 3 or more options or need to perform arithmetic, use a variable. You can also use some combination of the two (one flag and one variable, two flags, etc.) if your needs are more complex.
__________________

A Pokemon that is discriminated!
Support squirtle and make it everyone's favourite.
Reply With Quote
  #3   Link to this post, but load the entire thread.  
Old August 6th, 2015 (9:29 AM).
Sayuri's Avatar
Sayuri Sayuri is offline
人生忘れられた
 
Join Date: Jun 2015
Location: Boston, MA
Age: 24
Gender: Female
Nature: Lonely
Posts: 276
Quote:
Originally Posted by Touched View Post
Flags are booleans. They're for when you want a simple true/false, yes/no or on/off condition, e.g. "has the player completed this event?", "is this NPC visible?", "do I have this gym badge?", etc. As you can see, these are binary (two choices) questions, so it makes sense to represent them that way in memory to save space. As each flag only uses one bit, you can have 16 flags in the space that a variable uses. Most events are binary, so this turns out to be pretty useful.

Variables are for when you want to store more information, such as a step counter, the non-binary state of some event, the X/Y position of the player, etc. You can perform arithmetic on variables (only add/subtract with default script commands). Variables are in a sense, more powerful than flags - but in most cases they are not needed. Variables are 2 bytes (16 bits) and thus can store values from 0 to 65535 inclusive (2^16-1).

There are some places you absolutely need to use one or the other. Flags can be attached to NPCs which then control their visibility when you toggle them. Variables control script triggers and level scripts.

The question you need to ask when choosing is "Can this be represented as a binary choice?". If the choice is between exactly 2 distinct things, a flag is a good choice. However, if you need 3 or more options or need to perform arithmetic, use a variable. You can also use some combination of the two (one flag and one variable, two flags, etc.) if your needs are more complex.
Thank you so much for clearing that up for me.
Reply With Quote
Reply

Quick Reply

Join the conversation!

Create an account to post a reply in this thread, participate in other discussions, and more!

Create a PokéCommunity Account
Ad Content

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

Forum Jump


All times are GMT -8. The time now is 9:09 AM.