Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

Reference ACharacter class from APlayerController class?

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"));
 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.

Product Version: Not Selected
more ▼

asked Apr 04 '15 at 01:00 AM in C++ Programming

avatar image

8 2 7

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

1 answer: sort voted first

You can grab your ACharacter derived class by using

 class ADerivedCharacter *Character = Cast< class ADerivedCharacter >( UGameplayStatics::GetPlayerCharacter( this, NULL ) );
more ▼

answered Apr 04 '15 at 01:50 AM

avatar image

131 6 5 18

avatar image darkhorizon Apr 04 '15 at 02:48 AM

Thanks! I also realized (after a bit more research) that I can use the GetPawn() function as well and cast it to my ACharacter derived class.

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question