You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
for (uint8_t i = 0; i < ObjectManager::getMaxObjects(ObjectType::cargo); i++)
{
auto cargo = ObjectManager::get<CargoObject>(i);
if (cargo == nullptr)
continue;
// ...
}
Where cargo is just any particular type. We should make a helper method to handle this such as:
// Calls function on every loaded object of TObject type// parameters of Visitor are TObject&, LoadedObjectIdtemplate<typename TObject, typename Visitor>
voidvisitLoadedObjects(Visitor&& func)
{
static_assert(getMaxObjects(T::kObjectType) != 1);
for (LoadedObjectId i = 0U; i < getMaxObjects(TObject::kObjectType); ++i)
{
auto* obj = get<TObject>(i);
if (obj != nullptr)
{
func(*obj, i);
}
}
}
visitLoadedObjects<CargoObject>([](const CargoObject& cargo, LoadedObjectId i){
// ...
});
Now this works perfectly fine and would help but should we maybe make an iterator version so we could do:
One thing with the iterator approach i'm not sure how you could get the index. So maybe it doesn't work in our situation. You can't easily go from object -> id.
The text was updated successfully, but these errors were encountered:
This is pattern that shows up a lot, so it's a neat idea indeed. Agreed the version that passes the ID would be better, for the same reason you've stated.
I would vote for the forEach visitor pattern, we can also return the object id with iterators, just return std::pair instead of the object only, then you can do for(auto&& [id, object] : getLoadedObjects<T>()), there is the downside that when we don't need the id we can't mark it as maybe_unused.
We use the following in a lot of places:
Where cargo is just any particular type. We should make a helper method to handle this such as:
Now this works perfectly fine and would help but should we maybe make an iterator version so we could do:
One thing with the iterator approach i'm not sure how you could get the index. So maybe it doesn't work in our situation. You can't easily go from object -> id.
The text was updated successfully, but these errors were encountered: