script comp

This commit is contained in:
David Markowitz 2023-07-25 21:39:20 -07:00
parent 771eb65b92
commit eebf484c4e
4 changed files with 21 additions and 23 deletions

View File

@ -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<CDScriptComponent> CDScriptComponentTable::GetByID(unsigned int id) {
std::map<unsigned int, CDScriptComponent>::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<CDScriptComponent>(it->second) : std::nullopt;
}

View File

@ -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<CDScriptComponentTable> {
private:
std::map<unsigned int, CDScriptComponent> entries;
CDScriptComponent m_ToReturnWhenNoneFound;
public:
void LoadValuesFromDatabase();
// Gets an entry by scriptID
const CDScriptComponent& GetByID(unsigned int id);
const std::optional<CDScriptComponent> GetByID(unsigned int id);
};

View File

@ -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));
}
}
}

View File

@ -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<CDScriptComponentTable>();
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<CDScriptComponentTable>();
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?");
}