Bit flags

Blitz3D Forums/Blitz3D Tutorials/Bit flags

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

Here's another use for bit flags: state modes

State modes are always used in programs, for thousands of reasons. For example, you want to know if the game is started, or you want to know if the trace mode is activated, or say you want to know if your game is in the menu or in pause or IN GAME or whatever...

Usually, what you'll find is the following:

Type Game_Info
   Field game_started%
   Field game_paused%
   Field game_over%
   Field game_menu%
   Field debug_mode%
   Field ...
End Type


...Or, you sometimes find stuff like this:
Global game_state%
...
game_state = 1 ; Set the flag to game started
...
game_state = 2 ; the game is in the menu
...

Note here, that the coder actually had to explain what the code is doing, as 1 or 2 are just numeric values that, by themselves, don't mean anything.

Most of the time, you'll be searching for the field, and you'll have to maintain all those fields, which is usually one of the headaches of managing your game, because it seems so sloppy...

-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-

Here's the example I've tought for everyone interested in the topic:

You want to have a game_state that contains all your possible game states. First you'll define your bit constants as follows........
; Game state modes
Const GMSTA_RESET         = %00000000
Const GMSTA_INITIALIZED   = %00000001
Const GMSTA_MENU          = %00000010
Const GMSTA_IN_GAME       = %00000100
Const GMSTA_PAUSE         = %00001000
Const GMSTA_GAME_OVER     = %00010000
Const GMSTA_DEBUG         = %10000000
Const GMSTA_NO_MENU       = %11111101
Const GMSTA_NOT_IN_GAME   = %11111011
Const GMSTA_NOT_PAUSED    = %11110111
Const GMSTA_NOT_GAME_OVER = %11101111
Const GMSTA_NO_DEBUG      = %01111111


Then, in your game type:
; main game information
Type game_info
   Field mode%
...
End Type

Global game.game_info = New game_info


Next, you can do the following:
game\mode = game\mode Or GMSTA_INITIALIZED ; set the game mode as initialized


If you want to know if the game is initialized:
If game\mode And GMSTA_INITIALIZED Then


Let's say you were in the menu, but now you have left that and started a new game, you'll set the mode as following:
game\mode = game\mode And GMSTA_NO_MENU Or GMSTA_IN_GAME


So, in essence, you have a mode, and in that, you can set a number of bits to whatever you wish. Of course it has to be consistent to what it means and how it is used.

Cheers.