so basically, I'm trying to pass an object to my function Attack():
void Character::Attack(Character &another, short int range)
{
if (EnemyInRange(range) && another.health > 0)
{
if (allowed_to_attack)
{
attacked = true;
cout << name << " attacked " << another.name << " taking " << attack << " damage" << endl;
if (another.defensive > 0) // less important things down there
{
another.defensive--;
if (attack > another.defensive)
{
another.health -= (attack - another.defensive);
}
}
else if (another.defensive == 0)
{
another.health -= attack;
}
if (another.defensive <= 0)
another.defensive = 0;
if (another.health <= 0)
{
another.health = 0;
another.draw = false;
another.~Character();
}
}
else
{
attacked = false;
cout << "Blocked" << endl;
}
}
else
cout << name << " wanted to attack " << another.name << " ,but he's out of the range" << endl;
As you see, I'm using reference to pass an object. BUT, when I call this function:
void onClick(Vector2f mouse_position, Character current, Character *& target, Position &positionx)
{
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 5; j++)
{
if ((mouse_position.x >= 245 + i * 164 && mouse_position.x <= 370 + i * 164) &&
(mouse_position.y >= 56 + j * 201 && mouse_position.y <= 221 + j * 201))
{ // there's a 2d map with cards on those positions
target = &positionx.positioning[i][j];
current.Attack(*target);
}
}
}
}
positioning is an array filled with characters
It's behaving like I passed a copy of an object target (it writes out name, but it doesn't change its health)
in main() it looks like this:
Character *current = new Character();
Character *target = NULL;
if ((event.type == Event::MouseButtonPressed) && (event.mouseButton.button == Mouse::Left))//SFML lib
{
Vector2i pos = Mouse::getPosition(window);
Vector2f position = (Vector2f)pos;
onClick(position, *current, target, positionx);
} // target is selected after clicking on it
another.~Character();and what does this destructor do? This may cause strange behavior and there is almost no reason to manually call a destructor.