Hi! I am working with UE4.23 and in some cases face strong class dependencies (for example, Pawn and Movement classes, Animation and Character classes, etc). I am thinking that if base component class (for example UActorComponent) just implements some Interface as the code below, classes can become more independent and code can become more modular.
UINTERFACE(MinimalAPI)
class UOwnerMulticasterComponentBase : public UInterface {
GENERATED_BODY()
};
class REBELHERO_API IOwnerMulticasterComponentBase {
GENERATED_BODY()
// Add interface functions to this class. This is the class that will be inherited to implement this interface.
public:
virtual UActorComponent* GetSelfAsActorComponent() { return nullptr; };
template<typename T>
T* GetOwnerAs() {
static TMap<IOwnerMulticasterComponentBase*, T*> mapping;
if (!mapping.Contains(this)) {
if (auto component = GetSelfAsActorComponent()) {
mapping.Add(this, Cast<T>(component->GetOwner()));
}
else {
mapping.Add(this, nullptr);
}
}
return mapping[this];
}
};
However, this is not fine. Because in the heap absolute memory addresses change from time to time. So, only if we find the way that Unreal smart pointers update itself we can then try to use it in full with Unreal.
Here is the next iteration of all this and at the moment I am not facing any errors. In prev comment I was wrong. The errors there comes not from global changes of memory addresses, but rather from tricky memory allocation of TMap container. So, when I move to simple c++ std::map class, those strange pointer mistakes go away