Search code examples
c++parameter-passingmember-variables

Change value of member variable within function - C++


I'm doing some collision detection and handling for a GBA game I'm writing and I'm running into problems with putting the handling part into a function. I'm trying to change the value of a member variable (in this case Player.global_x), however passing the object into a function doesn't allow this to work. The relevant code is as follows:

main.cpp

bool CheckCollision(sprite obj1, sprite obj2){
    int obj1_top = obj1.global_y;
    int obj1_bottom = obj1.global_y + (obj1.height-1);
    int obj1_left = obj1.global_x;
    int obj1_right = obj1.global_x + (obj1.width-1);
    int obj2_top = obj2.global_y;
    int obj2_bottom = obj2.global_y + (obj2.height-1);
    int obj2_left = obj2.global_x;
    int obj2_right = obj2.global_x + (obj2.width-1);

    if(obj1_right < obj2_left){     // if obj1 is left of obj2
        return false;
    }
    if(obj1_left > obj2_right){     // if obj1 is right of obj2
        return false;
    }
    if(obj1_bottom < obj2_top){     // if obj1 is above obj2
        return false;
    }
    if(obj1_top > obj2_bottom){     // if obj1 is below obj2
        return false;
    }

    return true;
}

void HandleCollision(sprite obj1, sprite obj2){
    if(obj1.direction == RIGHT){
        Player.global_x -= (obj1.global_x + obj1.width) - obj2.global_x;    // This works but is not modular
        obj1.global_x -= (obj1.global_x + obj1.width) - obj2.global_x;      // This does not work
    }
}

void CheckAllPossibleCollisions(){
    bool collision = false;
    for(int i=0; i<(WallsVector.size()); i++){
        collision = CheckCollision(Player, WallsVector.at(i));
        if(collision==true){
            // This piece of code works, but would incur repetition of code for each different type of game object
            /*if(Player.direction == RIGHT){
                Player.global_x -= ((Player.global_x+Player.width) - WallsVector.at(i).global_x);
            }*/
            HandleCollision(Player, WallsVector.at(i));
        }
    }
}

The CheckCollision() function works fine when passing the objects in, but it doesn't work in HandleCollision() when changing a variable.

Any comments you can make to help with this would be appreciated, thanks!


Solution

  • You are passing the parameters by value. That means the functions get their own copies. It looks like you need to pass references:

    void HandleCollision(sprite& obj1, const sprite& obj2)
    

    Note that here, I have marked the 2nd parameter as const, because you do not modify it in the function. Whether you should pass a value or a reference in this case is only a matter of optimization. Only the first parameter needs to be a (non-const) reference.