Add find function to actor list

This commit is contained in:
nsubiron 2018-12-16 00:05:15 +01:00
parent 91b79687ef
commit 4661c24899
6 changed files with 15 additions and 15 deletions

View File

@ -78,6 +78,7 @@
## `carla.ActorList`
- `find(id)`
- `filter(wildcard_pattern)`
- `__getitem__(pos)`
- `__len__()`

View File

@ -20,12 +20,9 @@ namespace client {
: _episode(std::move(episode)),
_actors(std::make_move_iterator(actors.begin()), std::make_move_iterator(actors.end())) {}
SharedPtr<Actor> ActorList::GetActor(actor_id_type const actor_id) const
{
for (auto &actor: _actors)
{
if (actor_id == actor.GetId())
{
SharedPtr<Actor> ActorList::Find(actor_id_type const actor_id) const {
for (auto &actor : _actors) {
if (actor_id == actor.GetId()) {
return actor.Get(_episode, shared_from_this());
}
}

View File

@ -27,6 +27,9 @@ namespace client {
public:
/// Find an actor by id.
SharedPtr<Actor> Find(actor_id_type actor_id) const;
/// Filters a list of Actor with type id matching @a wildcard_pattern.
ActorList Filter(const std::string &wildcard_pattern) const;
@ -54,8 +57,6 @@ namespace client {
return _actors.size();
}
SharedPtr<Actor> GetActor(actor_id_type const actor_id) const;
private:
friend class World;

View File

@ -16,10 +16,10 @@ namespace detail {
void ActorVariant::MakeActor(EpisodeProxy episode, SharedPtr<const client::ActorList> actor_list) const {
auto const parent_id = GetParentId();
SharedPtr<client::Actor> parent = nullptr;
if ( (actor_list != nullptr) && (parent_id != 0) )
{
// in case we have an actor list as context, we are able to actually create the parent actor
parent = actor_list->GetActor(parent_id);
if ((actor_list != nullptr) && (parent_id != 0)) {
// In case we have an actor list as context, we are able to actually
// create the parent actor.
parent = actor_list->Find(parent_id);
}
_value = detail::ActorFactory::MakeActor(
episode,

View File

@ -148,8 +148,8 @@ void export_blueprint() {
class_<cc::BlueprintLibrary, boost::noncopyable, boost::shared_ptr<cc::BlueprintLibrary>>("BlueprintLibrary", no_init)
.def("find", +[](const cc::BlueprintLibrary &self, const std::string &key) -> cc::ActorBlueprint {
return self.at(key);
})
.def("filter", &cc::BlueprintLibrary::Filter)
}, (arg("id")))
.def("filter", &cc::BlueprintLibrary::Filter, (arg("wildcard_pattern")))
.def("__getitem__", +[](const cc::BlueprintLibrary &self, size_t pos) -> cc::ActorBlueprint {
return self.at(pos);
})

View File

@ -64,7 +64,8 @@ void export_world() {
;
class_<cc::ActorList, boost::shared_ptr<cc::ActorList>>("ActorList", no_init)
.def("filter", &cc::ActorList::Filter)
.def("find", &cc::ActorList::Find, (arg("id")))
.def("filter", &cc::ActorList::Filter, (arg("wildcard_pattern")))
.def("__getitem__", &cc::ActorList::at)
.def("__len__", &cc::ActorList::size)
.def("__iter__", range(&cc::ActorList::begin, &cc::ActorList::end))