View Full Version : Bool
Kirdra
09-12-2002, 02:19 PM
What is it, I don't understand but I think I need it to progress in my game. please could you give me a brief explaination. (possibly an example or a link) :)
Thanks.
Magos
09-12-2002, 02:29 PM
Bool, or boolean, is a variable which can only have the values True and False.
Though a boolean variable only uses one bit (1=True, 0=False), a bool takes up one byte (8 bits), so it's not such an optimized datatype, but can be practical in several situations.
If you need a lot of booleans, I suggest you rather use an int or char, and use bitwise operators on them. You can store 8 booleans in one char, which takes up as much memory as a normal bool.
Hope this helps a little...
DavidP
09-12-2002, 03:40 PM
Magos provided a very good explanation of the bool, but I thought I would show you an example in C++ of how a bool can be used.
You said you were making a game. Well, most games have collision detection, or in other words, a way to tell when one object in the game hits another object in the game.
I will show you a very simple example of this:
bool CollisionOccurrance;
if ( Location ( ObjectA ) == Location ( ObjectB ) )
CollisionOccurrance = true;
else CollisionOccurance = false;
you are then able to use this variable CollisionOccurance in other parts of the code. For example, maybe you want the two objects to explode if they hit each other:
if ( CollisionOccurance == true ) ExplodeObjects ( ObjectA, ObjectB );
Of course, you could also write that same line of code like this:
if ( CollisionOccurrance ) ExplodeObjects ( ObjectA, ObjectB );
but since you are just learning how to use bools, use the first way which I showed you, unless you consider yourself smart enough to realize how the second way is done and how it works.
You might also want to do something if they did not collide. Maybe if they dont collide, you want to move each object to a different position on the screen. This can also be done at least two different ways:
if ( CollisionOccurrance == false ) MoveObjects ( ObjectA, ObjectB );
or this way:
if ( !CollisionOccurrance ) MoveObjects ( ObjectA, ObjectB );
Anyways, thats about all. I hope those code examples help out.
vBulletin® v3.7.0, Copyright ©2000-2009, Jelsoft Enterprises Ltd.