C++ Game Error.

  • (7 Pages)
  • +
  • 1
  • 2
  • 3
  • 4
  • Last »

94 Replies - 3622 Views - Last Post: 12 November 2017 - 01:22 AM Rate Topic: -----

#16 Xrey274  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 134
  • Joined: 10-December 16

Re: C++ Game Error.

Posted 15 October 2017 - 09:08 AM

Well thats what i am asking. Where should i put the variables playerHealth and enemyHealth???
Was This Post Helpful? 0
  • +
  • -

#17 ndc85430  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 588
  • View blog
  • Posts: 2,482
  • Joined: 13-June 14

Re: C++ Game Error.

Posted 15 October 2017 - 09:27 AM

You've declared them correctly in the class. Just assign to them in the constructor rather than declaring a variable with the same name and assigning to that.
Was This Post Helpful? 0
  • +
  • -

#18 Xrey274  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 134
  • Joined: 10-December 16

Re: C++ Game Error.

Posted 15 October 2017 - 10:37 AM

I've got no idea what you are talking about. What i did is i remove the type from the variables in the constructors and then declared them in take_pDamage and take_eDamage respectively. But after i attack the value return to 20. It gets reinitialized.
Was This Post Helpful? 0
  • +
  • -

#19 ndc85430  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 588
  • View blog
  • Posts: 2,482
  • Joined: 13-June 14

Re: C++ Game Error.

Posted 15 October 2017 - 10:43 AM

View PostXrey274, on 15 October 2017 - 06:37 PM, said:

I've got no idea what you are talking about. What i did is i remove the type from the variables in the constructors


The second sentence here implies that you do understand, because again now you're assigning to the members rather than to some local variables that are shadowing them.

I haven't looked at the rest of the code, so can't comment further.
Was This Post Helpful? 0
  • +
  • -

#20 Xrey274  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 134
  • Joined: 10-December 16

Re: C++ Game Error.

Posted 15 October 2017 - 10:48 AM

Well, can you tell me how to prevent the reinitialization of the variables. Excuse me for not being able to understand certain phrases. I am completely self taught.
Was This Post Helpful? 0
  • +
  • -

#21 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2009
  • View blog
  • Posts: 5,388
  • Joined: 27-December 05

Re: C++ Game Error.

Posted 15 October 2017 - 10:56 AM

There's a fundamental concept that you're not understanding. Let me try to explain.

Here's your Enemy class definition:
class Enemy
{
    public:
        Enemy();
        void enemyEncountering(int, bool&);
        void enemyAttack(bool);
        void take_eDamage(int);
        void getHealth();
        void enemyDeath();
    private:
        int enemyHealth;
};


When you declare, as you have on line 11 above, a class member variable such as int enemyHealth, that means that EVERY Enemy object "automatically" has its own int variable called enemyHealth as soon as the Enemy object itself is declared. That variable is immediately accessible by every method (member function) of the Enemy class. So there's no need to re-declare enemyHealth in any of those functions. You just use it, as in, for example, enemyHealth = 20; or enemyHealth--; with no mention of type.

Not only is it unnecessary to re-declare it -- it's dangerous. When you do that you are declaring an entirely new variable with the same name as the class member variable -- a new variable that exists ONLY within the immediate scope {} in which it was declared, and which HIDES the class member variable (making that inaccessible) within that scope.

That's the "shadowing" that ndc85430 was referring to.

As you your "reinitializing" question, it sounds like you may be making the same error in take_pDamage and take_eDamage, but if after reading the above you can't fix it, you should repost the latest version of your code so we can see what's happening.
Was This Post Helpful? 1
  • +
  • -

#22 Xrey274  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 134
  • Joined: 10-December 16

Re: C++ Game Error.

Posted 15 October 2017 - 11:21 AM

Thanks. It's not like i don't get it it's just that............i forgot i declared the variables in Player.h and Enemy.h :D

Here's the code:

Player.cpp
#include "Player.h"
#include "Enemy.h"
#include "Animations.h"
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <windows.h>

using namespace std;

Player::Player()
{
    playerHealth = 20;
}

void Player::redirecting()
{

}

void Player::playerMovement(int& Tile, bool& enemyEncountered)
{
    cout<<"                      How many tiles do you want to move?"<<endl;
    cout<<"\n\n                                    Tile:"<<Tile<<"\n"<<endl;
    cout<<"\n--------------------------------------------------------------------------------"<<endl;
    cout<<"\n\n                    Hint:You can only move 5 tile at a time."<<endl;

    if(enemyEncountered == false)
    {
        cin>>movementInput;

        Enemy en;

        int x = 6 - movementInput;

        for(int i = x; i <= 5; i++)
        {
            Tile++;
            en.enemyEncountering(1, enemyEncountered);
        }
    }

    else if(enemyEncountered == true)
    {
        cout<<"\n                  Sorry, but you can't move while in a fight!"<<endl;

        cin.ignore();
        cin.get();
    }

}

void Player::playerAttack(string player_name, bool& enemyEncountered)
{
    cout<<"                              Please choose an attack!"<<endl;

    cout<<"\n\n--------------------------------------------------------------------------------"<<endl;

    cout<<" 1.Hadouken\n"<<endl;
    cout<<" 2.Recover\n"<<endl;
    cout<<" 3.Might Guy\n"<<endl;
    cout<<" 4.Toxic Spikes"<<endl;
    Enemy en;
    en.getHealth();

    cout<<"\n\n--------------------------------------------------------------------------------"<<endl;

    cout<<" "<<player_name<<"'s Health"<<endl;

    cin>>attackInput;

    Animations an;
    switch(attackInput)
    {
        case 1:
            en.take_eDamage(5);
            an.hadouken_Animation();
            break;

    }
}

void Player::take_pDamage(int pDamage)
{
    
    

    int x = 6 - pDamage;

    for(int y = x; y <= 5; y++)
    {
        playerHealth = playerHealth - 1;

    }
}

void Player::enemyHealthLeft()
{
    Enemy en;

    cout<<"\n Enemy's Health: "; en.getHealth();

    cin.ignore();
    cin.get();
    system("cls");
}




Player.h
#ifndef PLAYER_H
#define PLAYER_H
#include <string>

using namespace std;

class Player
{
    public:
        Player();
        void redirecting();
        void playerMovement(int&, bool&);
        void playerAttack(string, bool&);
        void take_pDamage(int);
        void enemyHealthLeft();
    private:
        int movementInput;
        int attackInput;
        int playerHealth;
};

#endif // PLAYER_H




Enemy.cpp
#include "Player.h"
#include "Enemy.h"
#include "Animations.h"
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <windows.h>

using namespace std;

Enemy::Enemy()
{
    enemyHealth = 20;
}

void Enemy::enemyEncountering(int tilesMoved, bool& enemyEncountered)
{
    int encounter;

    srand(time(NULL));

    for(int x = 0; x <= 5; x++)
    {
        if(tilesMoved = 1)
        {
            encounter = ( rand()%5 + rand()%4 ) - rand()%3 - tilesMoved;
        }
        if(encounter == 1)
        {
            x = 6;

            system("cls");
            cout<<"                              Enemy encountered!"<<endl;
            enemyEncountered = true;

            cin.ignore();
            if(cin.get())
            {
                system("cls");
            }
        }
    }
}

void Enemy::enemyAttack(bool enemyEncountered)
{
    if(enemyEncountered == true)
    {
        srand(time(NULL));

        int attackChance;

        for(int y; y <= 5; y++)
        {
            attackChance = rand()%3;
        }

        Player pl;
        switch(attackChance)
        {
            case 1:
                pl.take_pDamage(1);
                break;
        }
    }
}

void Enemy::take_eDamage(int eDamage)
{
    int x = 6 - eDamage;

    for(int y = x; y <= 5; y++)
    {
        enemyHealth = enemyHealth - 1;

        getHealth();
    }

    cin.ignore();
    cin.get();
}

void Enemy::getHealth()
{
    cout<<enemyHealth<<endl;
}

void Enemy::enemyDeath()
{

}




Enemy.h
#ifndef ENEMY_H
#define ENEMY_H


class Enemy
{
    public:
        Enemy();
        void enemyEncountering(int, bool&);
        void enemyAttack(bool);
        void take_eDamage(int);
        void getHealth();
        void enemyDeath();
    private:
        int enemyHealth;
};

#endif // ENEMY_H



Was This Post Helpful? 0
  • +
  • -

#23 ndc85430  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 588
  • View blog
  • Posts: 2,482
  • Joined: 13-June 14

Re: C++ Game Error.

Posted 15 October 2017 - 11:40 AM

View PostXrey274, on 15 October 2017 - 06:48 PM, said:

Excuse me for not being able to understand certain phrases. I am completely self taught.


You can understand them. The onus is sometimes on the person explaining to try and explain a different way. Hopefully r.stiltskin's explanation about shadowing helps. Many of us here (myself included) are self taught, too. How have you been learning the language?
Was This Post Helpful? 0
  • +
  • -

#24 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2009
  • View blog
  • Posts: 5,388
  • Joined: 27-December 05

Re: C++ Game Error.

Posted 15 October 2017 - 01:15 PM

Sorry for the delay -- I'm trying to do too many things at once. I can't run your program (working in Linux), but here's my 2 cents worth just from reading your code:

You're making similar errors in almost every function, where you create a new local Player or Enemy object and do something with it, and then at the close of that function the object is destroyed. The next function creates a completely new object, and so on.

For example in playerMovement (under the first if statement) you create an Enemy, call its enemyEncountering function, and then the function ends, destroying that Enemy object. You never see that particular Enemy again. In playerAttack, you create an Enemy, check its health, maybe give it some damage, and then the function ends, destroying that Enemy object. You never see that particular Enemy again. In enemyHealth, you create yet another new Enemy object, check its health, and then the function ends, destroying that Enemy object. You never see that particular Enemy again. And in enemyAttack, you create a Player, maybe give it some damage, and then the function ends, destroying that Player object. You never see that particular Player again.

You have to realize that an object exists only as long as it is "in scope" -- or loosely, as long as the function that created it continues to run.

I'm not a game person so I don't know the precise jargon that they would use, but you need a "game driver" function someplace that creates objects that persist (at least until they are killed) and are passed around from function to function so that you're not working on an entirely new object every time you call another function.
Was This Post Helpful? 0
  • +
  • -

#25 Xrey274  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 134
  • Joined: 10-December 16

Re: C++ Game Error.

Posted 15 October 2017 - 01:40 PM

No idea how to do that :(
Was This Post Helpful? 0
  • +
  • -

#26 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2009
  • View blog
  • Posts: 5,388
  • Joined: 27-December 05

Re: C++ Game Error.

Posted 15 October 2017 - 01:55 PM

Are you working with a tutorial?
Was This Post Helpful? 0
  • +
  • -

#27 Xrey274  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 134
  • Joined: 10-December 16

Re: C++ Game Error.

Posted 15 October 2017 - 02:52 PM

No? I made this project on my own.(and with help from forums)
Was This Post Helpful? 0
  • +
  • -

#28 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2009
  • View blog
  • Posts: 5,388
  • Joined: 27-December 05

Re: C++ Game Error.

Posted 15 October 2017 - 06:56 PM

Well, you may be getting a little ahead of yourself. You definitely need to read about passing by reference in C++. And you will probably also have to know about dynamic memory (on the assumption that you will be creating and destroying Enemy objects during runtime).

Your while loop (in main.cpp) seems to be the engine that keeps the game running. The Player is created there, so that looks OK. I don't see where enemyAttack is ever called -- I guess you just didn't get to that yet. But the thing is, you can't create a Player object in that function and expect that to have any effect on the Player you started out with in main. enemyAttack needs as one of its parameters a reference to main's Player.

As for the Enemy object(s) -- I'm guessing that there will be more than one of them and that they will be spawned and destroyed at various times during the game. If so you'll have to figure out where, when and under what circumstances those events occur, but regardless of where (in which function) that occurs, you'll have to pass each Enemy object around from function to function if you want to have persistence of each Enemy's attributes (health). So again you'll have to understand passing by reference. And dynamic memory allocation and deallocation to handle creation and destruction of enemies during the game.
Was This Post Helpful? 0
  • +
  • -

#29 Xrey274  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 134
  • Joined: 10-December 16

Re: C++ Game Error.

Posted 16 October 2017 - 02:42 AM

So basically what you're saying I won't be finishing the game any time soon?
Was This Post Helpful? 0
  • +
  • -

#30 ndc85430  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 588
  • View blog
  • Posts: 2,482
  • Joined: 13-June 14

Re: C++ Game Error.

Posted 16 October 2017 - 04:28 AM

It might be an idea to put it on hold for a while and go back to learning about some of the basics, like passing by reference, dynamic memory management and scope as r.stiltskin suggests. It looks like you've at least learnt about references somewhere, given you're sometimes passing them to your methods, e.g. Player::playerAttack takes enemyEncountered by reference (though you never use it in there).

How are you going about learning the language? Do you have a decent book, or are going to a class, or ...?

This post has been edited by ndc85430: 16 October 2017 - 04:28 AM

Was This Post Helpful? 1
  • +
  • -

  • (7 Pages)
  • +
  • 1
  • 2
  • 3
  • 4
  • Last »