How to wait until model is loaded while not blocking the other processes
I have a World plugin in which I need a pointer to a particular model's link. The problem is, that this plugin gets loaded before the model, therefor accessing the model's link results in failure. I tried to wait until the model is loaded as shown below.
physics::ModelPtr model = this->world->GetModel(model_name);
while(model == NULL) {
model = this->world->GetModel(model_name);
}
But this seems to induce a neverending loop. The model never gets loaded. I tried putting c++ usleep()
and roscpp ros::Duration(0.5).sleep()
in attempt to let the other processes load the world and the models in the meanwhile, but that does not help. Gazebo stucks in this loop.
Is there any way how to solve this?
SOLUTION
Ok, so I used the solution suggested by nlamprian, just declaring the event::ConnectionPtr as a class variable. But at the moment when the event callback was called, the robot was still not fully loaded in Gazebo. So I just set a flag inside this callback function and I get the pointer to the model in the next call of the OnUpdate function (which is called on every simulation time step).