Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: How to make setters  (Read 2861 times)

0 Members and 1 Guest are viewing this topic.

Mihrusha

  • Newbie
  • *
  • Posts: 3
    • View Profile
How to make setters
« on: September 26, 2021, 09:59:20 am »
How to make setters on sfml parameters?
I transfer functions from the Player class to the Game class, so that most interactions are in this class, I transfer the collision function, for example, a line in the Player class:

this-> pBody.left = j * BoxSize - this-> pBody.width;

where Pbody is FloatRect;
 so I'm trying to do in the Game class
this-> player-> setPBodyLeft (this-> player-> getPBody (). left) = j * BoxSize - this-> player-> getPBody (). width;


writes expression must have mudifiable lvalue error

so here setter and getter and the function itself

void Player :: setPBodyLeft (float x)
{
    this-> pBody.left = x;
}

const sf :: FloatRect Player :: getPBody () const
{
    return this-> pBody;
}


void Game :: PlayerMapCollision (float dir)
{
   
    for (int i = this-> player-> getPBody (). top / BoxSize; i <(this-> player-> getPBody (). top + this-> player-> getPBody (). height) / BoxSize; i ++ )
        for (int j = this-> player-> getPBody (). left / BoxSize; j <(this-> player-> getPBody (). left + this-> player-> getPBody (). width) / BoxSize; j ++ )
        {
            if (TileMap [i] [j] == 'A')
            {
                if (dx> 0 && dir == 0)
                    this-> player-> setPBodyLeft (this-> player-> getPBody (). left) = j * BoxSize -this-> player-> getPBody (). width;
                this-> player-> getPBody (). left = j * BoxSize - this-> player-> getPBody (). width;
                if (dx <0 && dir == 0)
                    this-> player-> getPBody (). left = j * BoxSize + BoxSize;
                if (dy> 0 && dir == 1)
                {
                    this-> player-> getPBody (). top = i * BoxSize - this-> player-> getPBody (). height;
                    dy = 0;
                    OnGround = true;
                }
                if (dy <0 && dir == 1)
                {
                    this-> player-> getPBody (). top = i * BoxSize + BoxSize;
                    dy = 0;
                }
            }

            if (TileMap [i] [j] == 'o')
            {
                TileMap [i] [j] = '';
                this-> health + = 10;
                this-> helthbar.setSize (sf :: Vector2f (health + 10, 20));
            std: cout << health;
            }

        }
 
}

kojack

  • Sr. Member
  • ****
  • Posts: 343
  • C++/C# game dev teacher.
    • View Profile
Re: How to make setters
« Reply #1 on: September 26, 2021, 10:42:55 am »
this-> player-> setPBodyLeft (this-> player-> getPBody (). left) = j * BoxSize - this-> player-> getPBody (). width;
In this case you are trying to give two different values. The this->player>getPBody().left is passing as an argument and is just setting the left to itself. Effectively it's just doing pBody.left = pBody.left.

At the end is an assignment, but you can't assign to a void function.

Give this a try:
this-> player-> setPBodyLeft (j * BoxSize - this-> player-> getPBody (). width);

 

anything