Cant change variable from another class

Hey I have a very simple problem yet its very hard for me to figure out why I cant change variable value from another class.

This is the class I want to change the variable value in
"frames"

class Game
{
public:
void SetNewFrames(float newFrame) { frames = newFrame; };

private:
// Updates run at 240 per second.
float frames = 240.0f;
float dt = 1.0f / frames;
};

Then in a different class I simply do

Game overRideFrame;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::O))
{
overRideFrame.SetNewFrames(5.0f);
}

I see no change in frames when I press O and I have even cout "frames" and it just stays on 240

Please help
1
2
3
4
5
6
7

if (sf::Keyboard::isKeyPressed(sf::Keyboard::O))
{
overRideFrame.SetNewFrames(5.0f);

cout << overRideFrame.frames << endl;
}


Temporarily change private to public so you can edit the function as I've posted, then tell me what you see.

Alternatively, instead, if you don't like changing private to public, do this:

 
void SetNewFrames(float newFrame) { frames = newFrame; cout << "SetNewFrames " << frames << endl; };


Now, I know the kind of problem you're having. I, myself, some 38 years ago, would have sworn the computer was actively refusing to do what I wrote. Something that simply MUST work wasn't.

It's never once been true.

I've seen people swear on their life the computer is simply refusing to execute their code.

It has never been the case.

I suspect overRideFrame is not the object you're checking after you change it here.
Last edited on
I see no change in frames when I press O and I have even cout "frames" and it just stays on 240
I would guess that you have two instances of the class Game. When you change the temporary variable overRideFrame it will not change the value of that other instance.
When you get frames to change, consider whether SetNewFrames() should dO
1
2
frames = newFrame;
dt = 1.0/frames;

After all, if dt was related to frames on construction, then shouldn't it change when the frame rate changes?
Topic archived. No new replies allowed.