I’ve been looking at the documentation for a very long time and I can’t figure it out. I haven’t been able to find a website, video, or person that can explain how to read Unreal’s documentation.
Can someone explain how I can use the Cast template in this way I described in the title?
Thanks in advance!
YourClass* CastedVariable = Cast(YourObject);
If your object can’t be casted to the specified class because it doesn’t inherit from that class, Cast<>() will return a nullptr. The if(…) is preventing NullPtrExceptions when calling the member function / method.
You could of course also write
YourClass* CastedVariable = Cast<YourClass>(YourObject);
depending on your situation
What does YourObject need to be? I’m trying to use a function that is declared and defined in an actor component inside of a different actor component.
It’s the instance of the actor component you want to cast to.
In your case it would be
YourActorClass* Actor = Cast<YourActorClass>(GetOwner());
YourActorComponentClass* ActorComponent = Cast<YourActorComponentClass>(Actor->TheActorComponentYouWantToInteractWith);
I’m still confused about these three lines. I’m using an “Interact” actor component on my PlayerCharacter to, when f is pressed, ray cast for an actor, check if that actor has one of the three interaction components (Quest, Shop, or Ambience), then return the interaction type. Then, inside Interact.cpp, it executes the corresponding function.
Well, if the function has the same signature in all the interaction components (for example bool Interact()), than it would be best to use an Interface. Because only then you can perform a cast based on the interface class for all three cases instead of casting to a class:
InterfaceClass* Interface = Cast<InterfaceClass>(ComponentOfTheRaycastedActor);
Unless all three component classes have the same parent where the interaction function is declared though. But it’s really hard for me to explain this here if I don’t know exactly how your code looks like.