creating ofxMSAinteractiveObject in runtime

Hi

I’m trying to implement ofxMSAInteractiveOBject to have some sort of draggable rectangle. So I extended it similar to the example. To add a new one of these fields i have a function newField in an external file.

  
  
bool newField(int x, int y){  
	movable m;  
	m.setSize(100, 200);  
	cout << "new field created" << endl;  
	return true;  
}  
  

This doesn’t work, no matter if i call it in setup or update. This however works:

  
  
class testApp : public ofBaseApp{  
	  
public:  
	void setup();  
	...  
	  
	movable m;  
};  
  

and then in setup of testApp:

  
  
m.setSize(200, 200);  
  

It looks like the events are registered but never called. This makes sense for setup , but not for update and draw events. Is this how ofEvents work? Do they only fire if not registered while runtime?

thx for your help
ph

if you create an object in between a block of {} it will only exist inside that block (unless you use memory allocation).
In your case your object is being created inside the function newField() but is also being destroyed once the function ends.
If you just want one of them you could simply declare them in your testApp.h file

But if you want to create a new object everytime the function is called you could use a list for example a std::vector to store your created objects

something like this

declare the list in you testApp.h

  
  
vector<movable> movableList;  
  

in your function you can do

  
  
bool newField(int x, int y){  
   movable m;  
   m.setSize(100, 200);  
   movableList.push_back(m); // this will make a copy of your object and store it in the list  
   cout << "new field created" << endl;  
   return true;  
}  
  

and now you can access the objects in the list, for example to draw them…

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

hope it helps

Rui

ah yes, of course, that does the trick. COuld have thought of this one.

thank you!