Increment variable inside class only increments once

OK. I’m going slightly crazy with this.

I have a variable inside a class that I want to increment on update. I try using

someVar++

But all it does it increment it once and then stays the same value on each update. Read up on C++ increments but still no joy. If I use ++ in the ofApp class it works fine. What’s the deal with ++ and – in class files?

Hope someone can help- if I don’t solve this soon someone is going to get hurt!

Thanks!

can you please post a small example?

sure thing:

class Geoloc {


public:
    
    Geoloc();
    void update();
    void draw();
    int alpha; // the thing I want to decrement
    
    float x;
    float y;
    float z;
    ofColor fillColor;
    vector<ofColor> colorPalette;
    
};

And the implementation

Geoloc::Geoloc() {
    
    x = Utils::snapToGrid(ofRandom(ofGetWidth()), GEOLOCSIZE);
    y = Utils::snapToGrid(ofRandom(ofGetHeight()), GEOLOCSIZE);
    z = Utils::snapToGrid(ofRandom(-1000,300), GEOLOCSIZE);
    colorPalette = Colors::getColorPalette();
    //fillColor = colorPalette.at(ofRandom(colorPalette.size()));
    fillColor = ofColor(100,100,100);
    alpha = 255;
    
}

void Geoloc::update(){
    
    --alpha;
    
    //cout<<alpha<<endl;

    fillColor = ofColor(100,100,100,alpha);
}



void Geoloc::draw(){
    
    ofPushMatrix();
    ofTranslate(-ofGetWidth()/2, -ofGetHeight()/2);
    //ofRotateX(-10);
    ofSetColor(fillColor);
    ofFill();
    ofDrawPlane(x, y,z, GEOLOCSIZE, GEOLOCSIZE);
    ofPopMatrix();
    
}

I’m trying to decrement the alpha value as opposed to increment but the idea is still the same!

Thanks Zach.

can I see how you use Geoloc ?

void ofApp::addGeoloc(){
    
    Geoloc g = Geoloc();
    geolocs.push_back(g);
    
}

I think that looks ok - how are you iterating the geolocs vector in ofApp::update()? Something like this?

for (std::vector<Geoloc>::iterator it = geolocs.begin(); it != geolocs.end(); ++it) {
    it->update();
}

I’m doing this but I’m doing it all in ofApp::draw() (is that the problem?)

 for (int i=0; i < geolocs.size(); i++) {
        
        Geoloc g = geolocs.at(i);
        g.update();
        g.draw();
        
    }

that is making a copy of the original object in the vector and incrementing that so in each frame you are creating a new object instead of incrementing the original

 for (int i=0; i < geolocs.size(); i++) {
        geolocs[i].update();
        geolocs[i].draw();

}

or

 for (int i=0; i < geolocs.size(); i++) {
        Geoloc & g = geolocs[i];
        g.update();
        g.draw();

}

updates the original object. note the & when retrieving the object from the vector in the second version. it’s called a reference in c++ and it’s a way of making a variable that points to the original object. if you do:

Geoloc g = geolocs.at(i);

without the & you are copying the object

Thanks Arturo - worked perfectly! Need to get up to speed with c++

Thanks everyone else for replying too - much appreciated.

just to add on to what arturo wrote, it’s useful to get a handle of “pass by reference” and “pass by copy” – c++ is default using pass by copy, so when you put an object into a function or return it from a function, it’s making a duplicate, which is unlike Java, for example. Getting a sense of those passing semantic can be really helpful.

some useful references (no pun!) :

http://www.cplusplus.com/articles/z6vU7k9E/
http://courses.washington.edu/css342/zander/css332/passby.html

1 Like

Thanks Zach - had no idea about that.