Will
Will

Reputation: 420

Class variable resets after function completes

I'm new to C++ from a java/python/etc background, and am trying to teach myself OO programming before I have to take a class on it next semester.

I'm trying to make an animation system using SFML, but am having some trouble with one of my class variables; it keeps resetting to 0 after I increment it. I'll start with the code and follow with a log output I'm using to help figure out what's going on.

SOLUTION: Being new to C++, I was an idiot and returned a new instance of my class in my getter functions; using [class]& func()... instead of [class] func() solved this, but now I have some refactoring to do.

Code (header):

...

typedef std::vector<Frame> frameVect; // (Frame defined above)
typedef std::vector<double> dubVect;

...

class limbAnim
{
private:
    int limbNum;
    int numFrames;
    int curFrame;
    frameVect frames;

public:
    limbAnim(int limb, int nFrames, frameVect F);
    <getters/setters>
    void incCurFrame();

    dubVect incrementAnimation(dubVect curPos, double curRot);
}

Code (cpp):

... (include vector, ofstream, etc)

std::ofstream AnimLog("log.log")

typedef std::vector<Frame> frameVect; // (Frame defined above)
typedef std::vector<double> dubVect;

...

limbAnim::limbAnim(int limb, int nFrames, frameVect F)
{
    limbNum = limb;
    curFrame = 0;
    numFrames = nFrames;
    frames = F;
}

void limbAnim::incCurFrame()
{
    curFrame=curFrame+1;
    if (curFrame >= numFrames)
    {
        curFrame = 0;
        AnimLog << "Greater than." << std::endl;
    }
}

dubVect limbAnim::incrementAnimation(dubVect curPos, double curRot)
{
    AnimLog << limbNum << ", " << numFrames << std::endl;

    if (numFrames > 0)
    {
        AnimLog << curFrame << std::endl;
        dubVect curStepP = frames[curFrame].getStepPos();
        double curStepR = frames[curFrame].getStepRot();

        curPos[0] = curPos[0] + curStepP[0];
        curPos[1] = curPos[1] + curStepP[1];

        curRot = curRot + curStepR;

        incCurFrame();
        AnimLog << "Incremented: " << curFrame << std::endl;
    }

    dubVect retV = curPos;
    retV.push_back(curRot);
    return retV;
}

So, my log output looks good since I'm testing with 2 frames on limbs 6 & 8, except those limbs' curFrame seems to reset to 0 after incrementing:

...
5, 0
6, 2
0
Incremented: 1
7, 0
8, 2
0
Incremented: 1
9, 0
...
5, 0
6, 2
0
Incremented: 1
7, 0
8, 2
0
Incremented: 1
9, 0
...(ad nauseam)

Edit: Code calling the increment function. (main.cpp)

// (Outside main loop.)
Animation walk_anim(12, "assets/anim/walk.dat");

// (Inside main loop.)
for (int i=0; i<12; i++)
{
    dubVect animDat = walk_anim.getLimbFrame(i).incrementAnimation(limbPos[i], curDegs[i]);
    dubVect newPos = getDVect(animDat[0], animDat[1]);
    double newRot = animDat[2];
    curDegs[i] = newRot;
    if (curDegs[i] >= 360)
        curDegs[i] -=360;
    limbPos[i] = newPos;
}

getLimbFrame

Animation::Animation(int lNum, string fName)
{
    numLimbs = lNum;
    fileName = fName;

    // Fill up limbVect with correct # of empty frames.
    for (int i=0; i<numLimbs; i++)
    {
        frameVect emptyFVect;
        limbAnim LA(i, 0, emptyFVect);
        limbFrames.push_back(LA);
    }

    // Boring .dat parsing, populates the 'frames' var of each limbAnim.
    loadAnim();
}

limbAnim Animation::getLimbFrame(int index)
{
    if (index < numLimbs)
    {
        return limbFrames[index];
    }
}

Upvotes: 1

Views: 4859

Answers (2)

Raziel Ravenheart
Raziel Ravenheart

Reputation: 44

I think you need to declare that member variable, with the static keyword, then you can say it is a class variable, where it will be the shared for every instance of your class. Like this:

static int curFrame;

Then you need to initialize it from outside de class. Have in mind that declaration, is a lot different to initialization.

You can read about it here and here

Upvotes: 1

Balog Pal
Balog Pal

Reputation: 17163

Hopefully you're aware that your functions take arguments by value, so they work on a copy of something.

You carefully avoided to show the really interesting code parts where you call incrementAnimation, most likely it follows the same bad pattern as the other functions.

I suggest reading up on how to pass objects by reference and const reference -- and how function arguments work in C++.

Upvotes: 4

Related Questions