Announcement

Collapse
No announcement yet.

Behavior Tree not recognizing the player

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

    Behavior Tree not recognizing the player

    The problem I'm having is my AI I constructed will not recognize neither the player nor the Vector of the player.

    Click image for larger version

Name:	superError.jpg
Views:	1
Size:	59.6 KB
ID:	1134160

    the above image is the behavior tree simulating and by the bottom right all the values are zeroed out or set to None . I also have blackboard variables for the behavior tree as well.

    .h
    Code:
    // Copyright 1998-2014 Epic Games, Inc. All Rights Reserved.
    
    #pragma once
    
    #include "GameFramework/AIController.h"
    #include "CorruptAI.generated.h"
    
    /**
     * 
     */
    UCLASS()
    class ACorruptAI : public AAIController
    {
    	GENERATED_UCLASS_BODY()
    
    	UPROPERTY(transient)
    	TSubobjectPtr<class UBlackboardComponent> BlackboardComp;
    
    	UPROPERTY(transient)
    	TSubobjectPtr<class UBehaviorTreeComponent> BehaviorComp;
    
    	virtual void Possess(class APawn* InPawn);
    	virtual void Tick(float DeltaTime);
    
    	void SetEnemy(class APawn* InPawn);
    	class ASideScrollerConceptCharacter *GetEnemy() const;
    
    	UFUNCTION(BlueprintCallable, Category = Behavior)
    	void SearchForEnemy();
    
    protected:
    	int32 EnemyKeyID;
    	int32 EnemyLocationID;
    	
    };

    .cpp
    Code:
    // Copyright 1998-2014 Epic Games, Inc. All Rights Reserved.
    
    #include "SideScrollerConcept.h"
    #include "CorruptAI.h"
    #include "CorruptBot.h"
    #include "SideScrollerConceptCharacter.h"
    
    
    ACorruptAI::ACorruptAI(const class FPostConstructInitializeProperties& PCIP)
    	: Super(PCIP)
    {
    	BlackboardComp = PCIP.CreateDefaultSubobject<UBlackboardComponent>(this, TEXT("BlackBoardComp"));
    
    	BehaviorComp = PCIP.CreateDefaultSubobject<UBehaviorTreeComponent>(this, TEXT("BehaviorComp"));
    }
    
    void ACorruptAI::Possess(class APawn* InPawn)
    {
    	Super::Possess(InPawn);
    	ACorruptBot* Bot = Cast<ACorruptBot>(InPawn);
    	if (Bot && Bot->BotBehavior)
    	{
    		BlackboardComp->InitializeBlackboard(Bot->BotBehavior->BlackboardAsset);
    
    		EnemyKeyID = BlackboardComp->GetKeyID("Enemy");
    		EnemyLocationID = BlackboardComp->GetKeyID("Destination");
    
    		BehaviorComp->StartTree(Bot->BotBehavior);
    	}
    	
    }
    
    void ACorruptAI::Tick(float DeltaTime)
    {
    
    
    }
    
    void ACorruptAI::SearchForEnemy()
    {
    	APawn* MyBot = GetPawn();
    	if (MyBot == NULL)
    	{
    		return;
    	}
    
    	const FVector MyLoc = MyBot->GetActorLocation();
    	float BestDistSq = MAX_FLT;
    	ASideScrollerConceptCharacter* BestPawn = NULL;
    
    	for (FConstPawnIterator It = GetWorld()->GetPawnIterator(); It; ++It)
    	{
    		ASideScrollerConceptCharacter* TestPawn = Cast<ASideScrollerConceptCharacter>(*It);
    		if (TestPawn)
    		{
    			const float DistSq = (TestPawn->GetActorLocation() - MyLoc).SizeSquared();
    			if (DistSq < BestDistSq)
    			{
    				BestDistSq = DistSq;
    				BestPawn = TestPawn;
    			}
    		}
    	}
    	if (BestPawn)
    	{
    		SetEnemy(BestPawn);
    	}
    }
    
    void ACorruptAI::SetEnemy(class APawn* InPawn)
    {
    	BlackboardComp->SetValueAsObject(EnemyKeyID, InPawn);
    	BlackboardComp->SetValueAsVector(EnemyLocationID, InPawn->GetActorLocation());
    	SetFocus(InPawn);
    }
    
    class ASideScrollerConceptCharacter* ACorruptAI::GetEnemy() const
    {
    	if (BlackboardComp)
    	{
    		return Cast<ASideScrollerConceptCharacter>(BlackboardComp->GetValueAsObject(EnemyKeyID));
    	}
    
    	return NULL;
    }
    Most of this code is from the ShooterGame code for testing and template usage.


    Any ideas??
    Portfolio/Tutorials: http://chadreddick.com

    #2
    From your code and behavior tree setup image, it looks like you have the parts from the shooter game example to find the player but still need to use them. Shooter game calls the equivalent of the SearchForEnemy function from a blueprint service. That is why the function is declared as BlueprintCallable. If you look at the shooter game behavior tree you will notice the following service that I outlined in red:



    If you double click on this service you will find that it is a blueprint derived from the class BTService_BlueprintBase with the following in its event graph:



    In the shooter game example, this service is set to tick every 2 seconds. So every 2 seconds the service blueprint will get the Event Receive Tick which will call the controller function to set an enemy. You could create a similar setup that calls your SearchForEnemy from a blueprint service. You could also write this service in code instead by deriving from UBTService and overriding the TickNode function to do the same thing that the shooter game blueprint service does except in code. Finally, you could do all of this entirely in blueprint if you wanted. Your SearchForEnemy function could be implemented in blueprint. You can check out an example of how to set up a blueprint service in a response I wrote for someone who was trying to do this entirely in blueprint: here You will also find a few other points about behavior tree services later in that thread.

    If this does not answer your question just let me know.
    Gameplay Programmer @ Epic Games

    Comment


      #3
      @Stephen

      I did those corrections and it still wont find the vector nor the player itself still

      Click image for larger version

Name:	errorerror.jpg
Views:	1
Size:	150.2 KB
ID:	1050202

      Click image for larger version

Name:	superdupererror.jpg
Views:	1
Size:	61.2 KB
ID:	1050203

      Click image for larger version

Name:	navmesherror.jpg
Views:	1
Size:	70.7 KB
ID:	1050204

      there is also a nav mesh as well for the AI too.

      [EDIT] The AI is the tall block thing
      Portfolio/Tutorials: http://chadreddick.com

      Comment


        #4
        I even tried it in blueprint.. still wont get the player's position nor the player object itself
        Portfolio/Tutorials: http://chadreddick.com

        Comment


          #5
          Is it hitting a breakpoint at the SetEnemy(BestPawn); function?

          What have you tried to debug this problem?

          Comment


            #6
            Ill try to make a tutorial tomorrow of my investigations into the behaviour tree system. I managed to make a monster spider AI that runs around, targets the player when it sees or hears it, shoots ba lls of goo and you, and from time to time, it burrows on the floor, spawning in a random place after a few secs alongside lots of small spiders that go and bite you.
            Once you get how the system works, is tremendously easy to create new behaviours and stuff. But i truly miss some kind of event firing in the behavior tree.
            UDK and UE4 programmer and Unreal engine 4 betatester. Currently working on commercial VR games for PSVR.
            Deep knowlegde of C++ and blueprints. Open to freelance work.
            Games released, Deathwave(Steam), VRMultigames(Steam), DWVR(Steam,Oculus,PSVR):
            http://store.steampowered.com/app/463870
            http://store.steampowered.com/app/500360
            http://store.steampowered.com/app/520750

            Comment


              #7
              @mikepurvis: I just tried putting a breakpoint on my "Search for Enemy" function in my blueprint. It does not snap to that breakpoint on starting the game. :/

              Click image for larger version

Name:	notworking.jpg
Views:	1
Size:	123.3 KB
ID:	1050206

              @vblanco: That would be fantastic! monster spiders = awesome ^_^!



              [EDIT]
              Soooo i just had a realization while I was screenshoting my blueprint.... for some reason it's a custom event tick?!!? so i switched it to be Event Tick.... and now it identify's the player... bleeeh little mistakes make a big difference to fix
              Last edited by CHADALAK1; 04-22-2014, 04:35 PM. Reason: Epiphany
              Portfolio/Tutorials: http://chadreddick.com

              Comment


                #8
                Well, put a breakpoint up in the previous function where it was supposed to be set. Test whether SearchForEnemy() is being called then, is it?

                You can also put a breakpoint on the Tree to make sure the service is getting called.

                Comment


                  #9
                  Originally posted by mikepurvis View Post
                  Well, put a breakpoint up in the previous function where it was supposed to be set. Test whether SearchForEnemy() is being called then, is it?

                  You can also put a breakpoint on the Tree to make sure the service is getting called.
                  It now shows the vector and the player itself now .... only problem now is it will not move .

                  here's the nav mesh layout test

                  Click image for larger version

Name:	nav_mesh.jpg
Views:	1
Size:	103.6 KB
ID:	1050207

                  and here is the behavior tree

                  Click image for larger version

Name:	working.jpg
Views:	1
Size:	66.8 KB
ID:	1050208

                  everything is working now... just not the movement :/
                  Portfolio/Tutorials: http://chadreddick.com

                  Comment


                    #10
                    I think there are some things missing, like event based trees (think, i launch an event of Burrow to the spider, and when it detects it it overrides all to do that.Im doing that by setting a bool value in the blackboard, but a better way would be fine.
                    I also miss some kind of "switch on enum" selector node to make it be like a state machine, maiby i create that one myself.
                    Also, a selector node wich gets a random task and executes it, maiby i also make that one myself.
                    Last edited by vblanco; 04-22-2014, 04:55 PM.
                    UDK and UE4 programmer and Unreal engine 4 betatester. Currently working on commercial VR games for PSVR.
                    Deep knowlegde of C++ and blueprints. Open to freelance work.
                    Games released, Deathwave(Steam), VRMultigames(Steam), DWVR(Steam,Oculus,PSVR):
                    http://store.steampowered.com/app/463870
                    http://store.steampowered.com/app/500360
                    http://store.steampowered.com/app/520750

                    Comment


                      #11
                      Good news!! I have it working!! honestly, I just reloaded the editor and it worked with the properties i saved. I have the AI moving to the player and it's all good. Thank you guys!
                      Portfolio/Tutorials: http://chadreddick.com

                      Comment


                        #12
                        Originally posted by vblanco View Post
                        I think there are some things missing, like event based trees (think, i launch an event of Burrow to the spider, and when it detects it it overrides all to do that.Im doing that by setting a bool value in the blackboard, but a better way would be fine.
                        That feature is included already, but the documentation is non-existent. There is a thread I started on BehaviorTrees in the tutorials section and some of the Epic Devs responded back, they go over that feature in one of their responses. It's called Observer Aborts. https://forums.unrealengine.com/show...-Tree-Tutorial

                        The random feature you want would be best as decorator, it might want to try random childs until one succeeds then return success so it keeps integraty of the BehaviorTree model.

                        Comment

                        Working...
                        X