1
\$\begingroup\$

I am taking the step of moving on from Java over to c++, I have been working on my own custom game engine in java for a while so have a good understanding of polymophism, inheritance etc.

Now to the issue:

Currently I am working on a basic level format trying to get entities to render to the screen, this all goes well if i change the code to what i don't want it to be below is the code:

#include <SFML/Graphics.hpp>
#include "../Entities/Player.h"

#include "Layer.h"

class TileMap: public sf::Drawable, public sf::Transformable {
private:
    std::vector<Player> entities;
public:

    Player* addEntity(Player player) {
        entities.push_back(player);
        return &entities[entities.size() - 1];
    }

    void update() {
        for (unsigned int i = 0; i < entities.size(); i++) {
            entities[i].update();
        }
    }


    virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const {
        for (unsigned int i = 0; i < entities.size(); i++) {
            target.draw(entities[i]);
        }
    }

};

The player that is returned in the function is then stored in a DrawableEntiity* player; variable. & this works fine everything updates

As I want a generalized list of entities and not a list for each entity i add to the game i want to be able to change this from

std::vector<Player> entities;

and change it too (obviously including the include for it too)

std::vector<DrawableEntity> entities;

The issue is everytime i store it this way the draw and update functions of the lower level Entity class isn't called and instead the default function inside of the DrawableEntity is called.

This is probably a easy mistake on something I've overlooked but i can't find anything online anywhere that fixes the issue. a full code listing of the Entity classes can be found at http://pastebin.com/3rRuzZHs

Thanks for taking your time to read this Jordan

P.S: If you need any more information let me know.

\$\endgroup\$

closed as off-topic by Vaillancourt, Josh Jul 7 '16 at 15:39

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Programming questions that aren't specific to game development are off-topic here, but can be asked on Stack Overflow. A good rule of thumb is to ask yourself "would a professional game developer give me a better/different/more specific answer to this question than other programmers?"" – Vaillancourt, Josh
If this question can be reworded to fit the rules in the help center, please edit the question.

0
\$\begingroup\$

When storing items of a child class in a vector, the vector will call the father's constructor (constructor of the type the vector was appointed to.)

To avoid that, you'll want a vector of pointers instead:

 std::vector<DrawableEntity *> entites;

For this approach you will likely have to dynamically allocate your entities manually using, say, the new keyword of course.

\$\endgroup\$
  • 1
    \$\begingroup\$ While it is not wrong to use raw pointers I would recommend using smart pointers which make handling of memory easier. Especially if one comes from Java. Raw pointers should only be used when they can't be avoided at all costs. \$\endgroup\$ – Skalli Jun 30 '16 at 15:51

Not the answer you're looking for? Browse other questions tagged or ask your own question.