Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!


1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


#ActualKaptein

Posted 14 September 2013 - 04:50 PM

yes, it is a problem

you want to use classes better

the power and ease they give you is unsurpassed in programming:

 

1. create a class with member fields that make sense for your NPCs

class NPC
{
    bool enabled;      // 
    bool renderable; // can we see this unit in the camera?
    bool dead;          // draw a dead unit if true

public:
    inline bool isEnabled() const { return enabled; }
   /// etc ...
// so far so good, right? let's make better use of the class
    
    // returns true if the unit is enabled, and at the same time is dead
    inline bool isDead() const { return enabled && dead; }
};
 

this is one approach, where the unit exists in a list somewhere, but can be deactivated / disabled

another is to remove units that arent alive anymore

 

Typically you'd want your units inside a std::vector<>, which you could just call units

And even better, a manager class for units, not just all your units, but any amount of units for any purpose

You could have one vector for dead units one for alive units and any other combination

 

What you should choose to do is entirely up to you

There are books written, tutorials made, but you are making your own game in your own time

Experiment and learn from it smile.png

 

Here is a Units class example:

class UnitList
{
    std::vector<Unit> units;

public:
    void add(const Unit& unit);
    void remove(const Unit& unit);
    
    // returns the number of units in this list
    inline int count() const  { return units.size(); }
    // returns unit no# i
    inline Unit& operator [] (int i)
    {
         if (i < 0 || i >= units.size()) throw "tantrum";
         return units[i];
    }
};

Now that you have a list of units, any and all interactions with units must go through this list, and nowhere else!

That means you have only 2 places now where changes have to be made to accomodate a change.

Of course, if the game logic changes, such as in your example (adding dead units,) that still means you have to go over your outside code and make some changes


#1Kaptein

Posted 14 September 2013 - 04:48 PM

yes, it is a problem

you want to use classes better

the power and ease they give you is unsurpassed in programming:

1. create a class with member fields that make sense for your NPCs

class NPC

{

    bool enabled;      // 

    bool renderable; // can we see this unit in the camera?

    bool dead;          // draw a dead unit if true

 

public:

    inline bool isEnabled() const { return enabled; }

   /// etc ...

// so far so good, right? let's make better use of the class

    

    // returns true if the unit is enabled, and at the same time is dead

    inline bool isDead() const { return enabled && dead; }

};

 

this is one approach, where the unit exists in a list somewhere, but can be deactivated / disabled

another is to remove units that arent alive anymore

 

Typically you'd want your units inside a std::vector<>, which you could just call units

And even better, a manager class for units, not just all your units, but any amount of units for any purpose

You could have one vector for dead units one for alive units and any other combination

 

What you should choose to do is entirely up to you

There are books written, tutorials made, but you are making your own game in your own time

Experiment and learn from it :)

 

Here is a Units class example:

 

class UnitList

{

    std::vector<Unit> units;

 

public:

    void add(const Unit& unit);

    void remove(const Unit& unit);

    

    // returns the number of units in this list

    inline int count() const  { return units.size(); }

    // returns unit no# i

    inline Unit& operator [] (int i)

    {

         if (i < 0 || i >= units.size()) throw "tantrum";

         return units[i];

    }

};


PARTNERS