feat: Add GetComponents(Mut) functions to Entity (#1842)

Allows for a really neat way of getting components using structured binding.  Tested that powerups still function

do it again because its neat
This commit is contained in:
David Markowitz 2025-07-01 05:26:05 -07:00 committed by GitHub
parent 9524198044
commit 5e9fe40bec
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 21 additions and 9 deletions

View File

@ -1662,11 +1662,9 @@ void Entity::PickupItem(const LWOOBJID& objectID) const {
auto* const skillsTable = CDClientManager::GetTable<CDObjectSkillsTable>(); auto* const skillsTable = CDClientManager::GetTable<CDObjectSkillsTable>();
const auto skills = skillsTable->Query([&info](CDObjectSkills entry) {return (entry.objectTemplate == info.lot); }); const auto skills = skillsTable->Query([&info](CDObjectSkills entry) {return (entry.objectTemplate == info.lot); });
for (const auto& skill : skills) { for (const auto& skill : skills) {
auto* skillComponent = GetComponent<SkillComponent>(); const auto [skillComponent, missionComponent] = GetComponentsMut<SkillComponent, MissionComponent>();
if (skillComponent) skillComponent->CastSkill(skill.skillID, GetObjectID(), GetObjectID(), skill.castOnType, NiQuaternion(0, 0, 0, 0)); if (skillComponent) skillComponent->CastSkill(skill.skillID, GetObjectID(), GetObjectID(), skill.castOnType, NiQuaternion(0, 0, 0, 0));
auto* missionComponent = GetComponent<MissionComponent>();
if (missionComponent != nullptr) { if (missionComponent != nullptr) {
missionComponent->Progress(eMissionTaskType::POWERUP, skill.skillID); missionComponent->Progress(eMissionTaskType::POWERUP, skill.skillID);
} }

View File

@ -2,6 +2,7 @@
#include <map> #include <map>
#include <functional> #include <functional>
#include <tuple>
#include <typeinfo> #include <typeinfo>
#include <type_traits> #include <type_traits>
#include <unordered_map> #include <unordered_map>
@ -161,6 +162,12 @@ public:
template<typename T> template<typename T>
T* GetComponent() const; T* GetComponent() const;
template<typename... T>
auto GetComponents() const;
template<typename... T>
auto GetComponentsMut() const;
template<typename T> template<typename T>
bool TryGetComponent(eReplicaComponentType componentId, T*& component) const; bool TryGetComponent(eReplicaComponentType componentId, T*& component) const;
@ -579,3 +586,13 @@ inline ComponentType* Entity::AddComponent(VaArgs... args) {
// To allow a static cast here instead of a dynamic one. // To allow a static cast here instead of a dynamic one.
return dynamic_cast<ComponentType*>(componentToReturn); return dynamic_cast<ComponentType*>(componentToReturn);
} }
template<typename... T>
auto Entity::GetComponents() const {
return GetComponentsMut<const T...>();
}
template<typename... T>
auto Entity::GetComponentsMut() const {
return std::tuple{GetComponent<T>()...};
}

View File

@ -6165,12 +6165,9 @@ void GameMessages::HandleRemoveDonationItem(RakNet::BitStream& inStream, Entity*
} }
void GameMessages::HandleConfirmDonationOnPlayer(RakNet::BitStream& inStream, Entity* entity) { void GameMessages::HandleConfirmDonationOnPlayer(RakNet::BitStream& inStream, Entity* entity) {
auto* inventoryComponent = entity->GetComponent<InventoryComponent>(); const auto [inventoryComponent, missionComponent, characterComponent] = entity->GetComponentsMut<InventoryComponent, MissionComponent, CharacterComponent>();
if (!inventoryComponent) return; if (!inventoryComponent || !missionComponent || !characterComponent || !characterComponent->GetCurrentInteracting()) return;
auto* missionComponent = entity->GetComponent<MissionComponent>();
if (!missionComponent) return;
auto* characterComponent = entity->GetComponent<CharacterComponent>();
if (!characterComponent || !characterComponent->GetCurrentInteracting()) return;
auto* donationEntity = Game::entityManager->GetEntity(characterComponent->GetCurrentInteracting()); auto* donationEntity = Game::entityManager->GetEntity(characterComponent->GetCurrentInteracting());
if (!donationEntity) return; if (!donationEntity) return;
auto* donationVendorComponent = donationEntity->GetComponent<DonationVendorComponent>(); auto* donationVendorComponent = donationEntity->GetComponent<DonationVendorComponent>();