Jump to content

  • Log In with Google      Sign In   
  • Create Account

Damaging Enemy AI


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 Lakhai   Members   -  Reputation: 106

Like
0Likes
Like

Posted 15 September 2012 - 04:58 PM

Hello everyone!

This is my first topic, since I've started reading these forums a while ago.
So I finally felt ready to start a little "game" project in C++; I've been using this language on and off for a bit, and I thought a project would help me understand it and practice it more.

What I want to do is a text-based RPG (focusing more on combat system, like Pokémon for example) and for now I'm just trying to get the basics to work.
You can make a character, name him and the game will randomly generate an amount of HP for you.

So I made this for the enemy:
class Enemy
{
	public:
	int health;
	int curhealth, atk1;
	string name;
	int GetHealth();
	void SetHealth();
	int TakeDamage(int);
};

Everything is going fine, but when I tried damaging one of those enemies with this function:
Spoiler


The first time I damage the enemy the health returns fine( ex. Enemy health is 100, I attack for 20 it returns 80);
now, the second time instead of taking the current health value of 80 and reducing, say another attack of 10, it'll reduce to the total health, returning 90.

I think that's because it's always re-setting curhealth to the value of maxhealth, therefore everytime I damage him it ignores the times before.

This is probably a silly mistake, and I'll try to fix it tonight if I have time, but a little insight wouldn't hurt Posted Image.

Apart from that, if you have any ideas as to what I could put in the game, let me know!
Cheers Posted Image

EDIT: It now works just fine, thanks to Servant of the Lord and Sollum!

Edited by Lakhai, 15 September 2012 - 06:50 PM.


Sponsor:

#2 Sollum   GDNet+   -  Reputation: 761

Like
0Likes
Like

Posted 15 September 2012 - 05:38 PM

I think you're not showing entire code. Took the methods you provided and put mechanics into my code, works fine.
By the way, just a little tip. Avoid stuff like

useSuperMegaAwesomeFunction(4, 6, 1, "crab", 54, 2, 4, 2, 'c', JUST_RANDOM.ENUM, "GO!");

It will confuse you later on. Don't be afraid to write few additional Int's as MIN_DAMAGE = 10 and MAX_DAMAGE = 30

I add working code, tho it's in C#, but logic behind in the same.

[source lang="csharp"]using System;using System.Collections.Generic;using System.Text;namespace ConsoleApplication2{ class Program { static void Main(string[] args) { Enemy Goblin = new Enemy("Goblin", 1); Enemy Link = new Enemy("Link", 15); Link.Attack(Goblin); Link.Attack(Goblin); Link.Attack(Goblin); Link.Attack(Goblin); Link.Attack(Goblin); Console.ReadLine(); } } class Enemy { int health; int curhealth, atk1; string name; public Enemy(string name, int atk1) { health = 100; curhealth = health; this.name = name; this.atk1 = atk1; } int GetHealth() { return curhealth; } void SetHealth() { //null } void TakeDamage(int damage) { curhealth -= damage; } public void Attack(Enemy enemy) { enemy.TakeDamage(atk1); Console.WriteLine(name + " attacked for: " + atk1 + "."); Console.WriteLine("Enemy HP: " + enemy.GetHealth() + "."); Console.WriteLine("===================================="); } }}[/source]

Edited by Sollum, 15 September 2012 - 05:39 PM.


#3 Servant of the Lord   Crossbones+   -  Reputation: 21198

Like
4Likes
Like

Posted 15 September 2012 - 06:37 PM

The problem is that you are passing 'enemy' by value, instead of by reference or pointer.
void Player::Attack(Enemy enemy)

When you pass by value, you create a copy* of the variable just for that function, and you aren't altering the original.

*Technically, it's possible that for optimizations a literal copy might not actually get made. But you can still think of it as a 'copy' and be 100% safe.

Currently you are going like this:

//This is psuedo-code for explanation, not a real C++ example.
Enemy myEnemy = 100 health;
Enemy myCopyOfEnemy = myEnemy;
Attack(myCopyOfEnemy, 20); //Copy's health is now 80. The original's health is still 100.
Enemy myDifferentCopyOfEnemy = myEnemy;
Attack(myDifferentCopyOfEnemy, 10); //A different copy's health is now 90. The original's health is still 100.

Each time you pass by value, a new copy is being created from the original without altering the original.

If you pass by reference, however, the original is altered, since no copy is created. References are just 'aliases' - different 'variables' that point to the same data - though the original variable still owns the data, so you have to make sure it still exists if you keep using the reference. Pointers would also work in this situation, but references are better here (references can't be re-assigned to reference a new variable, so they are safer).

The only thing you need to change, is from this:
void Player::Attack(Enemy enemy)

To this: (Added a '&' ampersand symbol)
void Player::Attack(Enemy &enemy)

Edited by Servant of the Lord, 15 September 2012 - 06:39 PM.

It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal

[Fly with me on Twitter] [Google+] [My broken website]

[Need web hosting? I personally like A Small Orange]


#4 Lakhai   Members   -  Reputation: 106

Like
0Likes
Like

Posted 15 September 2012 - 06:40 PM

Well, here's my entire code:
Main.cpp
Spoiler


Player:
Spoiler


Enemy:
Spoiler


#5 Servant of the Lord   Crossbones+   -  Reputation: 21198

Like
0Likes
Like

Posted 15 September 2012 - 06:55 PM

This is unrelated to your problem (which I answered while you were posting your last post) and isn't a bug, but worth pointing out:
class Enemy
{
		public:
		int health;
		int curhealth, atk1;
		string name;
		int GetHealth();
		void SetHealth();
		int TakeDamage(int);
};

'SetHealth()' implies that the user of the class gets to choose what to set the health to. "ResetHealth()" would be a better name.
'heath' implies it's the current health. 'maxHealth' would be a better name.
'curhealth' is slightly confusing. 'curHealth' or 'cur_health' is better (depending on your preference, but the first is more commonly used by C++ programmers, and the second is more commonly used by the C++ standard library because it was popular when the standard was being created), so you can see the beginning of each word. Even better still is 'currentHealth', I would rather have the extra readabillity, even if need to type four extra letters.

'TakeDamage(int)' should either return void, or return how much health is left. Currently you have it return int, but it doesn't actually return anything, meaning that the return value is gibberish.

 

What's the point of a GetHealth() function if the actual 'curhealth' variable it returns is 'public'? 'curhealth' should probably be private. A class can access it's own private variables and functions, but outside code can't access them.

Something like this:
class Enemy
{
public:
		int GetHealth(); //Returns the monster's current health.
		void SetHealth(int health); //Manually set the monster's current health to 'health'.
		void ResetHealth(); //Resets the monster's health to maximum (maximum is a random amount between 100 and 120).
		void TakeDamage(int amount); //Damages the enemy. Negative 'amount' values can heal the enemy, even beyond maximum.

private:
		int maxHealth, currentHealth; //currentHealth and maxHealth are related, so it's okay to put them on the same line.
		int attack1; //attack1 is only loosely related, so it should be on a different line. (This is my personal opinion!)
		std::string name; //Use the 'std::' when in headers. Don't put "using namespace" in header files (This is not my personal opinion! It can cause problems. Ask if you want to know why)
};

Edited by Servant of the Lord, 15 September 2012 - 06:56 PM.

It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal

[Fly with me on Twitter] [Google+] [My broken website]

[Need web hosting? I personally like A Small Orange]





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS