Skip to content

Commit

Permalink
Core: Add entity_cast
Browse files Browse the repository at this point in the history
  • Loading branch information
zach2good committed Dec 20, 2024
1 parent 32ee8de commit 44ddba1
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/map/entities/baseentity.h
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ struct EntityID_t
};

class CAIContainer;
class CBaseEntity;
class CBattlefield;
class CInstance;
class CZone;
Expand All @@ -250,6 +251,23 @@ struct location_t
}
};

template <typename TBase, typename TRequested>
inline auto entity_cast(TBase* basePtr) -> TRequested*
{
if (const auto* entityPtr = dynamic_cast<TRequested*>(basePtr))
{
return entityPtr;
}

const auto baseName = typeid(TBase).name();
const auto requestedName = typeid(TRequested).name();
const auto errString = fmt::format("entity_cast: Failed to cast entity from base type '{}' to requested type '{}'.", baseName, requestedName);

ShowCritical(errString);

return nullptr;
}

/************************************************************************
* *
* Basic class for all entities in the game *
Expand Down

0 comments on commit 44ddba1

Please sign in to comment.