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"

Custom AIController called twice and GetPawn()==NULL

In my project I have a playerPawn (already in the world) and an enemyPawn which should be possessed by AI when I spawn it into the world. enemyPawn is a c++ class based on Pawn, then i created the related blueprint. The problem appears when i spawn the enemyPawn and from myAiController i try to GetPawn(). GetPawn() returns NULL also if the enemyPawn was already created. The funny thing is that for some reason the beginplay function of myAIController runs for a second time and in that case the GetPawn() is not NULL.

I think everything is set correctly : Pawn section of enemyPawn alt text

This is my CustomAiController derived by AAIController alt text

In my Enemy constructor i also added this two lines

 AIControllerClass = AMyAIController::StaticClass();
 AutoPossessAI = EAutoPossessAI::PlacedInWorldOrSpawned;

but i don't think they are strictly necessary I spawn the enemyPlayer from the gamemode (but it is the same if i spawn it everywhere else) through this instruction

 AEnemy* enemy = GetWorld()->SpawnActor<AEnemy>(EnemyClass, FVector(600.0f, 0.0f, 0.0f), FRotator(.0f, .0f, .0f));


Do you have any idea why i get null the first time? and why beginPlay of MyAIController is called twice?

I also made an experiment in a new project with just a pawn, CustomAIController (that check if getPawn() is null otherwise it prints the name of the pawn) and an actor who spawns 3 pawns and I printed the result. alt text

Can you solve this mistery????

Product Version: UE 4.21
Tags:
question1.png (17.6 kB)
question2.png (36.9 kB)
question3.png (49.4 kB)
more ▼

asked Apr 01 '19 at 07:48 PM in C++ Programming

avatar image

MThaste
3 1 2

avatar image MThaste Apr 03 '19 at 09:25 AM

UP UP UP UP

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

2 answers: sort voted first

Okay, I solved the problem in this way (maybe can help someone):

  • First of all i put the Super::BeginPlay() in in the BeginPlay() of my custom AAIController

  • Second i override the Possess function of AAIController

       void AEnemyController::Possess(APawn * InPawn)
         {
             Super::Possess(InPawn);
             controlledPawn = Cast<AEnemyPawn>(InPawn);
             if (controlledPawn) {
                 //Pawn not null ***theorically you can do something here***
                 UE_LOG(LogTemp, Warning, TEXT("GetPawn() worked. Name: %s"), *controlledPawn->GetName());
             }
             else {
                 UE_LOG(LogTemp, Error, TEXT(" Pawn is null"));
             }
         }
     
    
    

I understood also that Possess is called before of the controlled pawn's beginplay() so pay attention in anycase (I solved initializing things in constructor or in SpawnActorDeferred.

Still i don't know why BeginPlay was called twice but in some way I managed the problems.

more ▼

answered Apr 08 '19 at 01:18 AM

avatar image

MThaste
3 1 2

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

[48h UP] Still have this strange "problem". I can continue to develop but i'm curious about this behavior. Hope someone has some idea!

more ▼

answered Apr 04 '19 at 04:29 PM

avatar image

MThaste
3 1 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