ofxImGui and vectors of objects

Hey there folks,

I am trying to create a base class called “Entity” that I later plan to inherit from to make more exotic/specialised classes. These entities or derived classes could then be stored in vectors for some dynamic runtime fun.

I had the idea that each Entity would write it’s own information and controls to a panel of the ofxImGui addon, as I have a little experience with ImGui and I like the way it looks etc. This way I can possibly select one or more entities and see only the appropriate information/controls for the selection. Perhaps this isn’t the best way to do this? (Open to advice!)

So far, I have each Entity in my vector of Entities writing a couple of simple buttons and text to the ImGui panel and they appear as I would expect at this stage. However I can only change the properties of item 0 in the vector. The controls for the other items do not function hover and click do affect colour on the button though.

Below is the draw function for an entity, where currently the ImGui controls are set up.

void Entity::Draw()
{
	std::stringstream ss; 
	ss << "UID : " << uid;
	ImGui::Text(ss.str().c_str());
	if (ImGui::Button("Disable Axes"))
	{
		drawAxes = false;
		std::cout << uid << " : DrawAxes : " << drawAxes << std::endl;
	}
	if (ImGui::Button("Enable Axes"))
	{
		drawAxes = true;
		std::cout << uid << " : DrawAxes : " << drawAxes << std::endl;
	}
	if (drawAxes)
	{
		ofDrawIcoSphere(position, 10.0f);
	}
}

The creation of a vector of Entities:

	for (int i = 0; i < 10; i++)
	{
		ofVec3f pos = { ofRandom(0,1024), ofGetViewportHeight() / 2.0f, 100.0f };
		auto e = std::make_unique<Entity>(i, pos);
		entities.push_back(std::move(e));
	}

And draw in ofApp:

void ofApp::draw(){
	ofClear(0);
	gui.begin();
	for (auto& e : entities)
	{
		e->Draw();
	}
	gui.draw();
	gui.end();
}

Hi,
I haven’t used ImGui recently, but this situation remind me something. Sometimes you have to use PushID. For example:

ImGui::PushID( this ) ;
// Draw ImGui controls here...
ImGui::PopID() ;

I guess that to use your UID instead of “this” may also works, if you’ve got a different UID for each object.

You’re a damn wizard. Worked like a charm. Thanks so much! :slight_smile: