Skip to content

Commit

Permalink
unique_ptr instead shared_ptr
Browse files Browse the repository at this point in the history
  • Loading branch information
MillhioreBT committed Jun 9, 2023
1 parent 08aa41c commit 9bd192d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
13 changes: 7 additions & 6 deletions src/npc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,6 @@ bool Npc::load()

reset();

if (!scriptInterface) {
scriptInterface.reset(new NpcScriptInterface());
scriptInterface->loadNpcLib("data/npc/lib/npc.lua");
}

loaded = loadFromXml();
return loaded;
}
Expand Down Expand Up @@ -1078,8 +1073,14 @@ int NpcScriptInterface::luaNpcCloseShopWindow(lua_State* L)
}

NpcEventsHandler::NpcEventsHandler(const std::string& file, Npc* npc) :
npc(npc), scriptInterface(npc->getScriptInterface())
npc(npc), scriptInterface(std::make_unique<NpcScriptInterface>())
{
if (!scriptInterface->loadNpcLib("data/npc/lib/npc.lua")) {
std::cout << "[Warning - NpcLib::NpcLib] Can not load lib: " << file << std::endl;
std::cout << scriptInterface->getLastLuaError() << std::endl;
return;
}

loaded = scriptInterface->loadFile("data/npc/scripts/" + file, npc) == 0;
if (!loaded) {
std::cout << "[Warning - NpcScript::NpcScript] Can not load script: " << file << std::endl;
Expand Down
9 changes: 5 additions & 4 deletions src/npc.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class NpcEventsHandler

private:
Npc* npc;
std::shared_ptr<NpcScriptInterface> scriptInterface;
std::unique_ptr<NpcScriptInterface> scriptInterface;

int32_t creatureAppearEvent = -1;
int32_t creatureDisappearEvent = -1;
Expand All @@ -82,6 +82,8 @@ class NpcEventsHandler
int32_t playerEndTradeEvent = -1;
int32_t thinkEvent = -1;
bool loaded = false;

friend class Npc;
};

class Npc final : public Creature
Expand Down Expand Up @@ -147,7 +149,7 @@ class Npc final : public Creature
void turnToCreature(Creature* creature);
void setCreatureFocus(Creature* creature);

auto getScriptInterface() { return scriptInterface; }
auto& getScriptInterface() { return npcEventHandler->scriptInterface; }

static uint32_t npcAutoID;

Expand Down Expand Up @@ -207,9 +209,8 @@ class Npc final : public Creature
bool isIdle;
bool pushable;

std::shared_ptr<NpcScriptInterface> scriptInterface;

friend class Npcs;
friend class NpcEventHandler;
friend class NpcScriptInterface;
};

Expand Down

0 comments on commit 9bd192d

Please sign in to comment.