x

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"

Calling a function from another class

I have a function in my EnemyCharacter cpp file called GetIsFrozen and I am trying to call it in my FindPlayer cpp file.

   // Fill out your copyright notice in the Description page of Project Settings.
     
     #include "BTService_FindPlayer.h"
     #include "EnemyAIController.h"
     #include "EnemyCharacter.h"
     #include "BehaviorTree/Blackboard/BlackboardKeyType_Object.h"
     
     UBTService_FindPlayer::UBTService_FindPlayer()
     {
         bCreateNodeInstance = true;
     }
     void UBTService_FindPlayer::TickNode(UBehaviorTreeComponent& OwnerComp, uint8* NodeMemory, float DeltaSeconds)
     {
         Super::TickNode(OwnerComp, NodeMemory, DeltaSeconds);
         auto EnemyAIController = Cast<AEnemyAIController>(OwnerComp.GetAIOwner());
     
         AEnemyCharacter* FrozenChar = Cast<AEnemyCharacter>(WHAT GOES IN HERE?);
         
     
         if (EnemyAIController && FrozenChar)
         {
             auto PlayerPawn = GetWorld()->GetFirstPlayerController()->GetPawn();
             OwnerComp.GetBlackboardComponent()->SetValue<UBlackboardKeyType_Object>(EnemyAIController->TargetKeyID, PlayerPawn);
             UE_LOG(LogTemp, Warning, TEXT("Target has been set!"));
         }
     
     
     }


Product Version: UE 4.21
Tags:
more ▼

asked Aug 11 '19 at 03:54 AM in C++ Programming

avatar image

Trunkzgamer
1

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

1 answer: sort voted first

To call a function in your EnemyCharacter.cpp, you need a pointer to that object and the function you want to call GetIsFrozen() needs to be declared "public" in EnemyCharacter.h.

I'm assuming by the filename that EnemyCharacter is of class Character and is the same entity as PlayerPawn (ACharacter is a subclass of APawn) you get with the call:

 auto PlayerPawn = GetWorld()->GetFirstPlayerController()->GetPawn();

Im also assuming that GetIsFrozen() returns a bool indicating whether the character is frozen or not.

So I think if you moved your PlayerPawn declaration/initialization line before your AEnemyCharacter* line then your EnemyCharacter line can be like the second line below:

 auto PlayerPawn = GetWorld()->GetFirstPlayerController()->GetPawn();
 AEnemyCharacter* FrozenChar = Cast<AEnemyCharacter>(PlayerPawn);
 

(By the way, I would have thought you would have APawn* PlayerPawn... rather than using auto but maybe there is something you are doing that I cannot see and therefore don't understand).

The remaining TODOs are to call the function to obtain the bool result and use that in an IF statement (assuming you only want the blackboard targeting to occur if IsFrozen is true):

 if (EnemyAIController && FrozenChar)
          {
              if(FrozenChar->GetIsFrozen())
                 {
                      OwnerComp.GetBlackboardComponent()->SetValue<UBlackboardKeyType_Object>(EnemyAIController->TargetKeyID, PlayerPawn);
                       UE_LOG(LogTemp, Warning, TEXT("Target has been set!"));
                  }
          }

Hopefully I've made the right assumptions and this is helpful. I'm pretty sure this works but haven't tested the code myself.

more ▼

answered Aug 11 '19 at 03:02 PM

avatar image

MCX292
128 2 2 2

(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