1

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
2
  • Why do you manually call the destructor another.~Character(); and what does this destructor do? This may cause strange behavior and there is almost no reason to manually call a destructor. Commented Apr 27, 2020 at 16:20
  • I wanted to make sure, that if character's health drops to 0, it will get destroyed Commented Apr 27, 2020 at 16:33

2 Answers 2

1

In the function onClick, the variable current is passed by value not reference. I assume that is supposed to be passed by reference:

void onClick(Vector2f mouse_position, Character &current, Character *target, Position &positionx)

Notice the argument current has been updated to a reference.

Edit:

Looking at the body of the Attack function, if the line printing out the name is met, are you sure the if-conditional statements if(another.defensive > 0) and if(attack > another.defensive) are met?

Sign up to request clarification or add additional context in comments.

3 Comments

Isn't the question about "target"? As far as I see "current" modifies "target", or at least should modify it.
SKCoder is right. I wanted to make " *target " modified, and "current" is object that runs "Attack()" method. I changed current to reference, but its still not working
@Azam Every thing was working fine when i was using Attack outside onClick function.
0

Do you think that Position &positionx might be a problem here? The most important part of onClick() function is target = &positionx.positioning[i][j]. But its passed by reference, so it shouldnt make a problem.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.