Mutator Not working

I'm trying to set a bool in one class to false through another class. But no matter what I do the bool stays true. Why????

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
//this class sets the bool in iter->second->getAnim().setRunning(!mIsPaused)
//mIsPaused is changed from false to true if the space bar is hit.
 void UnitManager::draw()
{
	if (!(mUnitMap.empty()))
	{
		std::map<int, Unit*>::iterator iter;

		//go through all entries in map and delete
		for (iter = mUnitMap.begin(); iter != mUnitMap.end(); ++iter)
		{
			iter->second->getAnim().setRunning(!mIsPaused);
			iter->second->draw();
		}
	}
}

//this function will run depending on that bool

void Animation::update()
{
	//mIsRunning = false;
	if (mIsRunning)
	{
		if (mTimer.getElapsedTime() >= 1600 / mFps)
		{
			mCurrentFrame++;
			//reset the timer
			mTimer.start();
		}

		if (mCurrentFrame >= mNumOfFrames)
			mCurrentFrame = 0;
	}
}
Show the source to void setRunning(bool)
Last edited on
1
2
3
4
5
public:
	void setRunning(bool running);

private:
	bool mIsRunning;
1
2
3
4
void Animation::setRunning(bool running)
{
	mIsRunning = running;
}
That's the declaration and a variable. It is not the source (the code for the body) of the function.

The return type from getAnim() would also be relevant.
and one more

1
2
3
4
Animation Unit::getAnim()
{
	return mAnim;
}
It really can't get any simpler. I must be missing something little. I've just been staring at it for too long, and it's evading me. I need a new set of eyes to look at it. Any help is greatly appreciated.
Last edited on
There's your problem. Unit::getAnim() returns a copy of an Animation. You're making changes to that copy and not the original.
but mAnim is a member variable of the Unit class. It's returning that unit's mAnim. If not, how would I return the actual mAmin?
Ah got it. Thanks cire. I added the & in the function definition and source.
Topic archived. No new replies allowed.