Trying to Replicate the "Input Setup" on C++

Hello!, So i just watched this video and since i did not feel comfortable enough having the
Input setup on the blueprint i challenged myself to try and do the same in C++.
i remember that in last section we used a Blueprint “Grabber” and attached to him a c++ where we mapped the action for the grab, so i thought to myself “How hard can it be if we already did something like it?”

Anyway after like 4 hours of playing around i was able to do it and i would like to share with you what i did and maybe also get a tip or 2 of what i could have done better.

So first of all i attached a new class to the blueprint. i called it TankMovement.
then i looked at what we did on the input setup of this video and also at the Grabber that we did last session
so i made a new pointer to UInputComponent on TankMovement, on the beginplay method i used the GetOwner()->FindComponentByClass(); to make it work like we did on the other Section.

then after a little google search i found that in order to get the event of the axis i need to use the method BindAxis on the UInputComponent. like this for example:
InputComponent->BindAxis(“AimAzimuth”, this, &TankMovement::AimAzimuth);
and
InputComponent->BindAxis(“AimY”, this, &TankMovement::AimElevation);

So far so good i made a UE_LOG to test that i was indeed catching the movement
i also noticed that the method i binded to the input was being called every frame.

Anyway now came the challenging part for me.
i wanted to get the Gimbal and SpringArm that were attached to the BP tank in order to set their rotation in the methods
So i managed to get the SpringArm the same way we got the input by:
GetOwner()->FindComponentByClass();
(And including the #include “GameFramework/SpringArmComponent.h” on the top of my file)

But i didnt had much luck finding the Gimbal.
i looked at it on the Blueprint and it said it was from type “Scene” so i assumed it would be something like
GetOwner()->FindComponentByClass();
But i found out that it all the components seem to have that class so it just returned me the first one which was the Tank.

After trying a lot of things and researching on google the only way i could do to accomplish this was to
Iterate through all the components in the owner like this
TArray<USceneComponent*> Components;
GetOwner()->GetComponents(Components);
for (int32 i = 0; i<Components.Num(); i++) //Count is zero
{
}
And check the name with Components[i]->GetName();

That way i was able to find both the Gimbal and the SpringArm to set their Rotations on each method
example:
void UTstActor::AimAzimuth(float val) {
Gimbal->AddLocalRotation(FRotator(0.f, val, 0.f));
}

Any of you has tried to do this after watching this video?
if so, how did you manage to find the Gimbal and SpringArm?
Do you think my way is the correct way to do in the future?

Thanks!!

Privacy & Terms