Game Dev concept.

I feel stupid, but I need help with one simple concept. I've though of a few ways to do it, but I feel like they all are bad methods. I have two classes, Player, and Enemy both in different .cpp files. I have a private member in my Player class that is the players health. I have a function titled TakeDamaga(int th) which I can use to make the player take damage whenever I call the function. However that means ->I<- have to pass in the value every time, I want it to depend on how it corresponds with the enemy objects attack function. Is there a way I can take the attack function from my Enemy class and have it in my TakeDamage function from my player class?
1
2
3
Player::TakeDamage(const Enemy &enemy){
   TakeDamage(enemy.Attack());
}
So you can put a function in parameters?
It calls "enemy.Attack()" and gives the return value to "TakeDamage(...)"
It's probably better to have the TakeDamage in the player class and then do a top level Battle function. Then you could do this...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void Battle(Player p1, Player p2)
{
       while (!p1.IsDead() && !p2.IsDead())
       {
            p2.TakeDamage(p1.Attack());
            p1.TakeDamage(p2.Attack());
        }

        if (!p1.IsDead() && p2.IsDead())
            cout << "Player 1 is victorious"!;
        else if  (! p2.IsDead() && p1.IsDead())
            cout << "Player 2 is victorious!";
        else 
            cout << "They were both slain!";
}


You should not let them Attack if they are dead though. That would make more sense.
If you haven't already, you probably want to make Attack and AttackRoll that uses a random die generator.
Last edited on
Thank you so much guys, this is what I did. (Note this isn't actually code to a game, I was just trying to get the concept down)

1
2
3
4
5
6
7
8
9
10
11
// Enemy.h
#ifndef ENEMY_H
#define ENEMY_H
class Enemy
{
private:
	int attackPower;
public:
   int attack(int ap);
};
#endif 


1
2
3
4
5
6
7
8
9
// Enemy.cpp
#include "Enemy.h"
#include<iostream>

 int Enemy::attack(int ap)
{
	attackPower = ap;
	return attackPower;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// Player.h
#ifndef PLAYER_H
#define PLAYER_H
#include<iostream>
class Player
{
private:
	int health;
public:
	int TakeDamage(int h);
	void printHealth();
	void setHealth(int a);
};

#endif 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// Player.cpp
#include "Player.h"
#include "Enemy.h"
#include<iostream>

 int Player::TakeDamage(int h)
 {
	 health = health - h;
	 return 0;
 }

 void Player::printHealth()
 {
	 std::cout << health << std::endl;
	 return;
 }

 void Player::setHealth(int a)
 {
	 health = a;
	 return;
 }


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// main.cpp
#include<iostream>
#include"Enemy.h"
#include"Player.h"
int main()
{
	Player po;
	Enemy eo;
	po.setHealth(100);
	po.printHealth();
	po.TakeDamage(eo.attack(50));
	po.printHealth();
	system("PAUSE");
	return 0;
}


I'm pretty sure that I could be passing by reference in some of these but I'm not really proficient with pointers yet. Feel free to point out my errors.
Austin J wrote:
I'm pretty sure that I could be passing by reference in some of these but I'm not really proficient with pointers yet.
Pointers have nothing to do with passing by reference ;)
I thought that there is passing by value and then passing by reference? Passing by value you have something like
1
2
3
4
5
6

    int someFunction(int x)
           {
               // When calling will create a variable copy
           }


Then passing by reference
1
2
3
4
     int anotherFunction(int *x)
        {
             // here you would be directly accessing the variable 
        }           


I'm really confused now :(.
Passing by value:
1
2
3
4
void func(Type t)
{
    //t is copied
}
Passing by reference:
1
2
3
4
void func(Type &t)
{
    //t refers to the object passed in
}
Passing pointer by value:
1
2
3
4
void func(Type *t)
{
    //t is a pointer, the pointer is copied
}
Passing pointer by reference:
1
2
3
4
void func(Type *&t)
{
    //t is a pointer, the pointer refers to the pointer passed in
}
Hmmm, I guess that's what I get for watching thenewboston and not reading more of my books lol.
I've changed it so that the enemy attack power gets loaded in the constructor. Finally, I found a use for it!
Topic archived. No new replies allowed.