I was following along with the course in the UE4 Online Learning center called Converting Blueprint to C++, and I noticed that while the instructor did a lot of great work that was informative, he would CONSISTENTLY skip over the “hard” stuff and just leave it implemented in Blueprint. This seems to be an extremely common aspect to many UE4 “C++ tutorials” in my experience, and it really infuriates me. This is usually anything with referencing functions in other classes or sometimes converting functions in general that do something more than just simple math or a getter/setter.
The guy even showed how to create a dynamic multicast delegate and bind it to an event in C++, yet he couldn’t show how to get the proper reference to a component and left it in blueprint.
That’s what drove me to make this post.
In the first person character in this tutorial, it has a Grabber component which lets it pick up physics bodies that are in front of the camera via GrabComponentAtLocationWithRotation(). You can see the hierarchy in the screenshot.
My question is this:
How can I get a proper reference to that Grabber component in order to call Grabber->Grab(); from within the FirstPersonCharacter.cpp code? Basically I want the characters input action to call the Grab() in Grabber.cpp.
I am familiar with casting, and in previous projects I’ve done something like:
AMainCharacter* mainChar = Cast<AMainCharacter>(OtherActor);
if (mainChar)
{
mainChar->SomeFunction();
}
But I searched through that code and noticed that every time it relied on either casting from the OtherActor in the case of an overlapped component, or a damage causer, or if it wasn’t that it was something like:
enemyPawn = TryGetPawnOwner();
if (enemyPawn)
{
// casts enemyPawn as a enemyClass type so there's a way to access variables in AEnemy_Base
enemyClass = Cast<AEnemy_Base>(enemyPawn);
}
Where it can access the class through casting an owner.
What if I just want to call a function in a component class that is a part of my character? Or what about getting a totally unrelated class in general? Is there a way to access all the different classes in a project via getting the game mode or game state, something like that? I’ve tried searching the forums and the web, but haven’t found the specific answer I need. I was hoping someone here can either point me to where I can properly gain this knowledge so I don’t keep coming up against this and getting stuck in the future.
Any advice would be greatly appreciated.