Reputation: 1
i have 2 classes Player and Bullet.
I need to read one variable in Bullet class from Player class. Problem is that when i copy variable it shows only value that was initiated when Player was created and not updating when Player is moving. How can i get access to live variable?
When i cout getPosition from Player class it shows live position, when i cout from Bullet class it shows only value that was initiated when Player was created.
Player:
Vector2f Player::PlayerPosition()
{
return shape.getPosition();
}
Bullet:
#include "Bullet.h"
#include "Player.h"
Player a;
Vector2f Bullet::getPlayerPosition()
{
return a.PlayerPosition();
}
Upvotes: 0
Views: 55
Reputation: 71
You're declaring a global Player instance a before the definition of the function Bullet::getPlayerPosition, which means that within the scope of that function, the name a is referring to that global instance, which may not be the one you're changing elsewhere. In general, if you'd like some variable to be in a function's scope, the best way is usually to pass the variable as a parameter to the function.
So, for example, I think what you want could look something like:
#include "Bullet.h"
#include "Player.h"
Vector2f Bullet::getPlayerPosition(const Player& a)
{
return a.PlayerPosition();
}
Upvotes: 2