Bool

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 ...

  1. #1
    Registered User Kirdra's Avatar
    Join Date
    Aug 2002
    Posts
    105

    Bool

    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.

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    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.

  3. #3
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,738
    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:

    Code:
    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:

    Code:
    if ( CollisionOccurance == true ) ExplodeObjects ( ObjectA, ObjectB );
    Of course, you could also write that same line of code like this:

    Code:
    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:

    Code:
    if ( CollisionOccurrance == false ) MoveObjects ( ObjectA, ObjectB );
    or this way:

    Code:
    if ( !CollisionOccurrance ) MoveObjects ( ObjectA, ObjectB );
    Anyways, thats about all. I hope those code examples help out.
    My Website

    "Circular logic is good because it is."

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. passing params between managed c++ and unmanaged c++
    By cechen in forum C++ Programming
    Replies: 11
    Last Post: 02-03-2009, 07:46 AM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  3. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  4. Headers that use each other
    By nickname_changed in forum C++ Programming
    Replies: 7
    Last Post: 10-03-2003, 04:25 AM
  5. Need Help With an RPG Style Game
    By JayDog in forum Game Programming
    Replies: 6
    Last Post: 03-30-2003, 07:43 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21