🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Polymorphism and ID systems C++

Started by
2 comments, last by ddlox 3 years, 8 months ago

I have an old game project, which doesn't use polymorphism. There are enemies of different types, however, they use the same simple_AI class. The code goes IF someAiType THEN do action. I want to use OOP more effectively, but, I part of my problem is going through a list of objects from the same parent, and calling their own functions. I know the virtual keyword means when you call that function, whatever the subparents version is called. However, I do not know how to loop through these for example
enemyai List → Perform subclasses version of virtual function → Repeat through List

I know how to make a subclass, but repeating through children derived from a parent via array confuses me. I know doing if(ID) → do action is bad practise.

Advertisement

It think you're making this more difficult than it needs to be. First I'd like to know whether you differentiate between an enemy and an AI. If you do, there could be a challenge in terms of how many enemy types an AI should be able to operate with. I consider enemy a "shell" for an AI to interact with.

I'm not sure what you're referring to wrt. repeating through children derived from a parent. Iterating with range-based for-loops is very easy, and you can iterate on pointers and interfaces contained in a collection, which is likely what you'd want to do.

One very simple example on how you could accomplish multiple AI types with a single enemy type - let's say your enemies are tanks.
These tanks could expose an interface for sensors and actuators, and the AI that would control the tank would be given this interface/tank reference for it to control. You'd be able to pass a tank to several AI types if you want.

For interfaces and abstracting from subclasses, I can recommend playing around with pure virtuals and modern pointers.

Does this help? Otherwise perhaps you could draw up a diagram of your problem.

// pseudo 
class node 
{
public:
  virtual void do_AI(); 				// you decide if you want this as  = 0 or not
  
  void add/remove/whatever(node* n); 	// manage m_children
  
  void set_parent(node* p);				// manage parent
  
protected:
 node* m_parent;						// use this parent as a weak ptr 
 std::vector<node*> m_children  	
};

class enemy: public node
 {
 public:
    virtual void do_AI() override
    {
      // do this enemy's AI here
      ...
      
      // then 
      if (children.empty())
        return;
        
      for (auto child : m_children)
        child->do_AI();
    };
 };

Here is a little pseudo snippet that should help u a bit to get going, but this code is hugely unpolished. Please clean it up and i'll leave it as an exercise for you to improve on. I have simplified for clarity and it is off the top of my head (before lunch -lol-).

Remember to delete all members cleanly. And find out why I said to use “weak ptr”.

Have fun.

That's it … all the best ?

This topic is closed to new replies.

Advertisement