From c7c5a2c9c091da44e3af6b25a4c9ce0797d6943f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=CE=B6eh=20Matt?= <5415177+ZehMatt@users.noreply.github.com> Date: Fri, 21 Mar 2025 00:04:41 +0200 Subject: [PATCH] Skip the type checking in GetEntity/TryGetEntity with T as BaseEntity --- src/openrct2/entity/EntityRegistry.h | 36 ++++++++++++++++++++++------ 1 file changed, 29 insertions(+), 7 deletions(-) diff --git a/src/openrct2/entity/EntityRegistry.h b/src/openrct2/entity/EntityRegistry.h index 28fb5768d9..69d6aaeeed 100644 --- a/src/openrct2/entity/EntityRegistry.h +++ b/src/openrct2/entity/EntityRegistry.h @@ -29,22 +29,44 @@ namespace OpenRCT2 constexpr uint16_t kMaxEntities = 65535; -EntityBase* GetEntity(EntityId sprite_idx); +EntityBase* GetEntity(EntityId entityId); template -T* GetEntity(EntityId sprite_idx) +T* GetEntity(EntityId entityId) { - auto spr = GetEntity(sprite_idx); - return spr != nullptr ? spr->As() : nullptr; + auto* ent = GetEntity(entityId); + if (ent == nullptr) + { + return nullptr; + } + if constexpr (std::is_same_v) + { + return ent; + } + else + { + return ent->As(); + } } EntityBase* TryGetEntity(EntityId spriteIndex); template -T* TryGetEntity(EntityId sprite_idx) +T* TryGetEntity(EntityId entityId) { - auto spr = TryGetEntity(sprite_idx); - return spr != nullptr ? spr->As() : nullptr; + auto* ent = TryGetEntity(entityId); + if (ent == nullptr) + { + return nullptr; + } + if constexpr (std::is_same_v) + { + return ent; + } + else + { + return ent->As(); + } } EntityBase* CreateEntity(EntityType type);