diff --git a/examples/example_plugin/Plugin.cpp b/examples/example_plugin/Plugin.cpp index 79973db2..d60fded3 100644 --- a/examples/example_plugin/Plugin.cpp +++ b/examples/example_plugin/Plugin.cpp @@ -285,6 +285,17 @@ class ExamplePlugin : public uevr::Plugin { API::get()->log_info("Engine name: %s", uengine_name_narrow.c_str()); + // Test if we can dcast to UObject. + { + const auto engine_as_object = engine->dcast(); + + if (engine != nullptr) { + API::get()->log_info("Engine successfully dcast to UObject"); + } else { + API::get()->log_error("Failed to dcast Engine to UObject"); + } + } + // Go through all of engine's fields and log their names. const auto engine_class_ours = (API::UStruct*)engine->get_class(); for (auto super = engine_class_ours; super != nullptr; super = super->get_super()) { diff --git a/include/uevr/API.hpp b/include/uevr/API.hpp index a138a27e..2bb32d70 100644 --- a/include/uevr/API.hpp +++ b/include/uevr/API.hpp @@ -137,6 +137,20 @@ class API { template struct TArray; + // dynamic_cast variant + template + static T* dcast(UObject* obj) { + if (obj == nullptr) { + return nullptr; + } + + if (obj->is_a(T::static_class())) { + return static_cast(obj); + } + + return nullptr; + } + template T* find_uobject(std::wstring_view name) { static const auto fn = sdk()->uobject_array->find_uobject; @@ -335,6 +349,16 @@ class API { return c->get_fname()->to_string() + L' ' + obj_name; } + // dynamic_cast variant + template + T* dcast() { + if (this->is_a(T::static_class())) { + return static_cast(this); + } + + return nullptr; + } + private: static inline const UEVR_UObjectFunctions* s_functions{nullptr}; inline static const UEVR_UObjectFunctions* initialize() {