Access/ modify ofPath command?

Hi everyone,

I am struggling to change a set path command to my mouse position in “void ofApp::update()”. I don’t get any errors while compiling, however, the path doesn’t move.

I am assigning the path commands in:

void ofApp::setup(){

void ofApp::setup()
    
    ofBackground(ofColor::black);

    myPath.lineTo(20,20);
    myPath.lineTo(ofGetWindowWidth() - 20, 20);
    myPath.lineTo(ofGetWindowWidth() - 20, ofGetWindowHeight()-20);
    myPath.lineTo(20, ofGetWindowHeight()-20);
    myPath.close();
}

I am trying to update the first command in:

void ofApp::update(){

void ofApp::update()
    
    vector <ofPath::Command> controls = myPath.getCommands();

    for(int i = 0; i < controls.size(); i++) {
        controls[0].to, (ofGetMouseX(), ofGetMouseY());
    }
}

And finally drawing the path in:

void ofApp::draw()

void ofApp::draw(){
    
    myPath.setFilled(false);
    myPath.setStrokeWidth(5);
    myPath.setStrokeColor(ofColor::white);
    myPath.draw();
}

Please let me know if you have any advice. Thank you!

Hi the problem you have is in the update.

you have this

void ofApp::update()
    
    vector <ofPath::Command> controls = myPath.getCommands();

    for(int i = 0; i < controls.size(); i++) {
        controls[0].to, (ofGetMouseX(), ofGetMouseY());
    }
}

in it controls is a copy of the paths commands, so when you modify these you don’t modify myPath at all.
What you need to do is to make it a reference, as it follows.

void ofApp::update()
    
    vector <ofPath::Command>& controls = myPath.getCommands();

    for(int i = 0; i < controls.size(); i++) {
        controls[0].to, (ofGetMouseX(), ofGetMouseY());
    }
}

The “big” difference is that there is an & before controls which makes it a reference instead of a copy.

let me know if this worked.

Hey Roy,

Sorry for the late reply, this solution worked!

Thank you!