Удалил UType::object_ptr и заменил его на комбинацию из std::shared_ptr и std::weak_ptr из-за ужасной реализации.

This commit is contained in:
2026-05-31 20:38:30 +07:00
parent fa3e987e93
commit 6389e6b25f
14 changed files with 67 additions and 311 deletions
+7 -7
View File
@@ -2,7 +2,7 @@
#include "Game/SaveMap/SaveMap.hpp"
World::World(GameInstance& game_instance) : game_instance(game_instance)
World::World(GameInstance& game_instance) : game_instance_(game_instance)
{
}
@@ -34,7 +34,7 @@ std::shared_ptr<SaveMap> World::save()
void World::load(std::shared_ptr<SaveMap> save)
{
std::vector<std::string> actors_IDs = save->GetVectorString("ActorsIDs");
std::list<UType::object_ptr<Actor>> act = std::move(reinterpret_cast<std::list<UType::object_ptr<Actor>>&>(save->GetListObject("Actors")));
std::list<std::shared_ptr<Actor>> act = std::move(reinterpret_cast<std::list<std::shared_ptr<Actor>>&>(save->GetListObject("Actors")));
size_t i_id = 0;
for (auto& i : act)
@@ -46,13 +46,13 @@ void World::load(std::shared_ptr<SaveMap> save)
}
}
void World::DestroyActor(UType::object_ptr<Actor> ptr)
void World::DestroyActor(std::weak_ptr<Actor> ptr)
{
if (ptr.get() == nullptr)
std::shared_ptr<Actor> actor = ptr.lock();
if (actor.get() == nullptr)
return;
std::string name = ptr->GetName();
std::string name = actor->GetName();
actor->OnDestroy();
actors_map.erase(name);
ptr->OnDestroy();
ptr.destroy();
}
+11 -12
View File
@@ -7,16 +7,15 @@
#include "RTTI.h"
#include "Game/Actors/Actor.hpp"
#include "Types/object_ptr.hpp"
class GameInstance;
GENERATE_META(World);
class World : public ISave
{
GameInstance& game_instance;
GameInstance& game_instance_;
std::unordered_map<std::string, UType::object_ptr<Actor>> actors_map;
std::unordered_map<std::string, std::shared_ptr<Actor>> actors_map;
public:
World(GameInstance& game_instance);
@@ -25,7 +24,7 @@ public:
virtual void BeginPlay();
virtual void Tick(double delta_time);
GameInstance& GetGameInstance() const { return game_instance; }
GameInstance& GetGameInstance() const { return game_instance_; }
#pragma region ISave
std::shared_ptr<SaveMap> save() override;
@@ -33,21 +32,21 @@ public:
#pragma endregion
template<class T>
std::vector<UType::object_ptr<T>> GetActorsByClass()
std::vector<std::weak_ptr<T>> GetActorsByClass()
{
std::vector<UType::object_ptr<T>> list;
std::vector<std::weak_ptr<T>> list;
for (auto i = actors_map.cbegin(); i != actors_map.cend(); ++i)
{
if (T* cast_object = RTTI::dyn_cast<T, Actor>(i->second.get()))
list.push_back(UType::object_ptr<T>(cast_object));
if (RTTI::dyn_cast<T, Actor>(i->second.get()))
list.push_back(std::static_pointer_cast<T>(i->second));
}
return list;
}
template<class T>
UType::object_ptr<T> SpawnActorFormClass(std::string name, const Vector3D& loc = { 0, 0, 0 }, const Vector3D& rot = { 0, 0, 0 })
std::weak_ptr<T> SpawnActorFormClass(std::string name, const Vector3D& loc = { 0, 0, 0 }, const Vector3D& rot = { 0, 0, 0 })
{
UType::object_ptr<T> object(new T());
std::shared_ptr<T> object = std::make_shared<T>();
object->SetActorLocate(loc);
object->SetActorRotate(rot);
static_cast<Actor*>(object)->world_ = this;
@@ -55,7 +54,7 @@ public:
return object;
}
void DestroyActor(UType::object_ptr<Actor> ptr);
void DestroyActor(std::weak_ptr<Actor> ptr);
template<class T, class Container = std::vector<T*>>
Container GetActorsByTag(std::string tag)
@@ -76,7 +75,7 @@ public:
}
template<class T>
UType::object_ptr<T> GetActorByID(const std::string& id)
std::weak_ptr<T> GetActorByID(const std::string& id)
{
return actors_map[id];
}