0
\$\begingroup\$

I've been over this for a long time, I can't seem to understand what is wrong, is the use of the this keyword right? I want to add the instance of the class that is being created to the vector 'allEntities' in the constructor, or am I not using the iterators right? I'm very new to c++ and programming in general so all help is appreciated.

GameObject.h

class GameObject {

public:
    GameObject(const char* texturesheet, SDL_Renderer* ren, int x, int y, int _maxHp, int _currentHp, int _strength);
    ~GameObject();

    static std::vector<GameObject> allEntities;

    void update();
    void Render();
    void movement(const char* coord, bool positive);
    int getMaxHp();
    void addMaxHp(int change);
    int getCurrentHp();
    void setCurrentHp(int change);
    int getStrength();
    void changeStrength(int change);
    void attack(GameObject &other);
    int getX() { return x; };
    int getY() { return y; };
    void setDirectionFacing(char direction) { directionFacing = direction; };
    char getDirectionFacing() { return directionFacing; };
private:

    int x;
    int y;
    int strength;
    int maxHp;
    int currentHp;
    char directionFacing;


    SDL_Texture* objectTexture;
    SDL_Rect srcRect, destRect;
    SDL_Renderer* renderer;

};

GameObject.cpp

GameObject::GameObject(const char* texturesheet, SDL_Renderer* ren, int xx, int yy, int _maxHp, int _currentHp, int _strength)
{
    allEntities.push_back(*this);
    renderer = ren;
    objectTexture = textureCreator::loadTexture(texturesheet, ren);
    GameObject::maxHp = _maxHp;
    GameObject::currentHp = _currentHp;
    changeStrength(_strength);
    x = xx;
    y = yy;
}
GameObject::~GameObject()
{

}

void GameObject::update()
{



    srcRect.h = 48;
    srcRect.w = 48;
    srcRect.x = 0;
    srcRect.y = 0;


    destRect.x = x;
    destRect.y = y;
    destRect.w = srcRect.w * 1.5;
    destRect.h = srcRect.h * 1.5;
}

void GameObject::movement(const char* coord, bool positive)
{

    if (coord == "x" && positive == true) 
    { 
        y -= 4;
        setDirectionFacing('w');
        std::cout << 'w' << std::endl;
    };
    if (coord == "x" && positive == false) 
    { 
        y += 4; 
        setDirectionFacing('s');
        std::cout << 's' << std::endl;
    };
    if (coord == "y" && positive == true) 
    { 
        x += 4;
        setDirectionFacing('d');
        std::cout << 'd' << std::endl;
    };
    if (coord == "y" && positive == false) 
    { 
        x -= 4; 
        setDirectionFacing('a');
        std::cout << 'a' << std::endl;

    };


}

void GameObject::Render()
{
    SDL_RenderCopy(renderer, objectTexture, &srcRect, &destRect);
}

int GameObject::getCurrentHp() { return currentHp; };

int GameObject::getMaxHp() { return maxHp; };

void GameObject::setCurrentHp(int change) { currentHp += change; };

void GameObject::addMaxHp(int change) { maxHp += change; };

int GameObject::getStrength() { return strength; }

void GameObject::changeStrength(int change) { strength += change; }

void GameObject::attack(GameObject &other) 
{


}

game.cpp

GameObject* player;
player = new GameObject("player.png", renderer, 10 , 10, 10,  10, 2);
    for (std::vector<GameObject>::iterator it = GameObject::allEntities.begin(); it != GameObject::allEntities.end(); it++) {
        int hp = it->getCurrentHp();
        std::cout << hp << std::endl;
    }

I expect to print out the hp value which is 10, but instead it prints 0 instead.

Also I need this vector so when the player attacks, it checks every entity and sees if it is in the area of the attack in a for loop, is there any better way to do this?

\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

These two lines in the constructor do not do what you think they do:

GameObject::maxHp = _maxHp;
GameObject::currentHp = _currentHp;

In the constructor, as in any class method, you access the class variables directly, like so:

maxHp = _maxHp;
currentHp = _currentHp;

Most compilers would probably catch this as a warning, and especially while learning I would recommend turning up your compiler warnings as high as they go, just on the general principle of getting a feeling for what the compiler is doing.

You need one more change because this line creates a shallow copy of the object being constructed and adds that copy to the allEntities vector, and it does that copy before any member variables have been set.

allEntities.push_back(*this);

I think that if you move that line after all the variables have been set then you're done. With a note: what you probably want to do later is make allEntities a vector of pointers to created objects and not a full copy, but since you're very new that can come later.

Also, for problems like this there's a technique called Rubber Duck Debugging that is very helpful https://en.wikipedia.org/wiki/Rubber_duck_debugging

\$\endgroup\$
5
  • \$\begingroup\$ thank you for the reply, but I think there might be another problem, i fixed it and there is still the same problem, could the problem be in the game.cpp? \$\endgroup\$ Feb 14, 2019 at 23:21
  • \$\begingroup\$ @noobProgrammer I have updated the answer with another possible point of failure and notes on what it's doing. \$\endgroup\$ Feb 14, 2019 at 23:59
  • \$\begingroup\$ Oh Thats exactly what i want to do, i want to be able to modify the variables of the actual objects using the vector. is there any article or documentation you can give me for me to learn about pointers? or even give me an example? \$\endgroup\$ Feb 15, 2019 at 0:54
  • \$\begingroup\$ I think i made the vector of pointers instead after some reading static std::vector<GameObject*> allEntities; but I dont know how to pass the argument of This keyword as a pointer in allEntities.push_back(*this); how can i push back the pointer of the object being created? \$\endgroup\$ Feb 15, 2019 at 1:18
  • \$\begingroup\$ You need to slow down, frustrating though it will be, and read up on C++ fundamentals like how the C++11 and C++14 smart pointers work. These low level concepts are at the core of C++ and its class/polymorphism and memory management model and without that foundation everything will be a tangle of unanswered mysteries. \$\endgroup\$ Feb 15, 2019 at 8:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .