So I’m creating a top-down style game. I looked at the Top Down template, but I want to implement my own.
One thing I want to do is allow the player to zoom in and out with the mouse wheel. My ACharacter class contains a UCameraComponent that is attached to a USpringArmComponent so it’s just a matter of changing the TargetArmLength in order to “zoom”.
I want to handle all the player input in my APlayerController class. For me to zoom, I need to reference the USpringArmComponent from my ACharacter class. This is where I’m stuck.
I’m rather fluent in C++, but I’m still very new to the engine. I thought I could grab a pointer to the USpringArmComponent in my GameMode constructor, but that proved…difficult and resulted in weird casts that didn’t even work (this is in my GameMode constructor):
// Find & assign MainCharacter
static ConstructorHelpers::FClassFinder<APawn> bpMainCharacter(TEXT("Pawn'/Game/Incantare/Blueprints/Character/BP_MainCharacter.BP_MainCharacter_C'"));
if (!bpMainCharacter.Succeeded())
{
UE_LOG(LogTemp, Fatal, TEXT("Unable to locate player pawn blueprint class in DefaultGameMode ctor"));
return;
}
DefaultPawnClass = bpMainCharacter.Class;
// Setup DefaultPlayerController
ADefaultPlayerController* defaultPlayerController = dynamic_cast<ADefaultPlayerController*>(ADefaultPlayerController::StaticClass());
defaultPlayerController->SpringArmComponent = dynamic_cast<AMainCharacter*>(bpMainCharacter.Class.GetDefaultObject())->SpringArmComponent;
PlayerControllerClass = defaultPlayerController; // don't know what to cast to here
I wasn’t sure what cast to use ( static_cast
, dynamic_cast
, or the engine’s Cast
function). That being said, I don’t think the StaticClass()
and bpMainCharacter.Class
are returning what I think they are returning (and/or I’m using them incorrectly). I have a feeling they aren’t actual instances of the class…
If that is the case, how can I solve my problem of my APlayerController referencing the USpringArmComponent of my ACharacter class?
Or is there another more “elegant” way of implementing a zoom that won’t require the player controller depending on the character class (probably not the best idea in the long run).
Thanks for your help.