diff --git a/dDatabase/Tables/CDScriptComponentTable.cpp b/dDatabase/Tables/CDScriptComponentTable.cpp index 1a922199..9ee02a87 100644 --- a/dDatabase/Tables/CDScriptComponentTable.cpp +++ b/dDatabase/Tables/CDScriptComponentTable.cpp @@ -1,7 +1,6 @@ #include "CDScriptComponentTable.h" void CDScriptComponentTable::LoadValuesFromDatabase() { - // First, get the size of the table unsigned int size = 0; auto tableSize = CDClientDatabase::ExecuteQuery("SELECT COUNT(*) FROM ScriptComponent"); @@ -28,12 +27,8 @@ void CDScriptComponentTable::LoadValuesFromDatabase() { tableData.finalize(); } -const CDScriptComponent& CDScriptComponentTable::GetByID(unsigned int id) { +const std::optional CDScriptComponentTable::GetByID(unsigned int id) { std::map::iterator it = this->entries.find(id); - if (it != this->entries.end()) { - return it->second; - } - - return m_ToReturnWhenNoneFound; + return (it != this->entries.end()) ? std::make_optional(it->second) : std::nullopt; } diff --git a/dDatabase/Tables/CDScriptComponentTable.h b/dDatabase/Tables/CDScriptComponentTable.h index d2b7e7ae..48e93387 100644 --- a/dDatabase/Tables/CDScriptComponentTable.h +++ b/dDatabase/Tables/CDScriptComponentTable.h @@ -5,18 +5,16 @@ struct CDScriptComponent { unsigned int id; //!< The component ID - std::string script_name; //!< The script name - std::string client_script_name; //!< The client script name + std::string script_name; //!< The script name + std::string client_script_name; //!< The client script name }; class CDScriptComponentTable : public CDTable { private: std::map entries; - CDScriptComponent m_ToReturnWhenNoneFound; - public: void LoadValuesFromDatabase(); // Gets an entry by scriptID - const CDScriptComponent& GetByID(unsigned int id); + const std::optional GetByID(unsigned int id); }; diff --git a/dGame/Entity.cpp b/dGame/Entity.cpp index 72803f24..d80130c2 100644 --- a/dGame/Entity.cpp +++ b/dGame/Entity.cpp @@ -463,9 +463,11 @@ void Entity::Initialize() { if (scriptComponentID > 0 || m_Character) { std::string clientScriptName; if (!m_Character) { - CDScriptComponent scriptCompData = scriptCompTable->GetByID(scriptComponentID); - scriptName = scriptCompData.script_name; - clientScriptName = scriptCompData.client_script_name; + auto scriptCompData = scriptCompTable->GetByID(scriptComponentID); + if (scriptCompData) { + scriptName = scriptCompData->script_name; + clientScriptName = scriptCompData->client_script_name; + } } else { scriptName = ""; } @@ -510,10 +512,11 @@ void Entity::Initialize() { if (zoneData) { int zoneScriptID = zoneData->scriptID; - CDScriptComponent zoneScriptData = scriptCompTable->GetByID(zoneScriptID); - - ScriptComponent* comp = new ScriptComponent(this, zoneScriptData.script_name, true); - m_Components.insert(std::make_pair(eReplicaComponentType::SCRIPT, comp)); + auto zoneScriptData = scriptCompTable->GetByID(zoneScriptID); + if (zoneScriptData) { + ScriptComponent* comp = new ScriptComponent(this, zoneScriptData->script_name, true); + m_Components.insert(std::make_pair(eReplicaComponentType::SCRIPT, comp)); + } } } diff --git a/dGame/dComponents/InventoryComponent.cpp b/dGame/dComponents/InventoryComponent.cpp index 236e1715..dad47507 100644 --- a/dGame/dComponents/InventoryComponent.cpp +++ b/dGame/dComponents/InventoryComponent.cpp @@ -933,8 +933,9 @@ void InventoryComponent::EquipScripts(Item* equippedItem) { int32_t scriptComponentID = compRegistryTable->GetByIDAndType(equippedItem->GetLot(), eReplicaComponentType::SCRIPT, -1); if (scriptComponentID > -1) { CDScriptComponentTable* scriptCompTable = CDClientManager::Instance().GetTable(); - CDScriptComponent scriptCompData = scriptCompTable->GetByID(scriptComponentID); - auto* itemScript = CppScripts::GetScript(m_Parent, scriptCompData.script_name); + auto scriptCompData = scriptCompTable->GetByID(scriptComponentID); + if (!scriptCompData) return; + auto* itemScript = CppScripts::GetScript(m_Parent, scriptCompData->script_name); if (!itemScript) { Game::logger->Log("InventoryComponent", "null script?"); } @@ -948,8 +949,9 @@ void InventoryComponent::UnequipScripts(Item* unequippedItem) { int32_t scriptComponentID = compRegistryTable->GetByIDAndType(unequippedItem->GetLot(), eReplicaComponentType::SCRIPT, -1); if (scriptComponentID > -1) { CDScriptComponentTable* scriptCompTable = CDClientManager::Instance().GetTable(); - CDScriptComponent scriptCompData = scriptCompTable->GetByID(scriptComponentID); - auto* itemScript = CppScripts::GetScript(m_Parent, scriptCompData.script_name); + auto scriptCompData = scriptCompTable->GetByID(scriptComponentID); + if (!scriptCompData) return; + auto* itemScript = CppScripts::GetScript(m_Parent, scriptCompData->script_name); if (!itemScript) { Game::logger->Log("InventoryComponent", "null script?"); }