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.
This is a discussion on Bool within the Game Programming forums, part of the General Programming Boards category; What is it, I don't understand but I think I need it to progress in my game. please could you ...
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.
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...
MagosX.com
Give a man a fish and you feed him for a day.
Teach a man to fish and you feed him for a lifetime.
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:
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:Code:bool CollisionOccurrance; if ( Location ( ObjectA ) == Location ( ObjectB ) ) CollisionOccurrance = true; else CollisionOccurance = false;
Of course, you could also write that same line of code like this:Code:if ( CollisionOccurance == true ) 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.Code:if ( CollisionOccurrance ) ExplodeObjects ( ObjectA, ObjectB );
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:
or this way:Code:if ( CollisionOccurrance == false ) MoveObjects ( ObjectA, ObjectB );
Anyways, thats about all. I hope those code examples help out.Code:if ( !CollisionOccurrance ) MoveObjects ( ObjectA, ObjectB );