Hi, I am coding a 2D space shooter game in C++ with literally about 100 sprites on the screen
The main spaceship is a flipbook and the 26 enemies are also flipbooks
On level 1 the enemies just move left and right and there is no Z-fighting going on
Currently I have it locked @ 120 FPS in 1080p
The thing is the one line that sets the new actor location in the movement class is tanking the FPS down to 90 to 105
With the line commented out, i.e. don’t make the enemies move, then the game is solid @ 120 FPS
Each enemy actor spawns a custom AI controller which executes just one behaviour tree per enemy and the behaviour tree itself just has one sequence node which points to a task that does the movement logic in the tick task
What could be causing the FPS drop?
Here is the task (the very last line is causing the frame drop):
// Class is derived from UBTTask_BlackboardBase
include “EnemyMove.h”
include “AIController.h”
include “BehaviorTree/BehaviorTreeComponent.h”
include “BehaviorTree/BlackboardComponent.h”
include “BasicEnemy.h”
UEnemyMove::UEnemyMove(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer)
{
NodeName = TEXT(“Move Enemy”);
bNotifyTick = true;
}
EBTNodeResult::Type UEnemyMove::ExecuteTask(UBehaviorTreeComponent& OwnerComp, uint8* NodeMemory)
{
Super::ExecuteTask(OwnerComp, NodeMemory);
FinishLatentTask(OwnerComp, EBTNodeResult::InProgress);
return EBTNodeResult::InProgress;
}
void UEnemyMove::TickTask(UBehaviorTreeComponent& OwnerComp, uint8* NodeMemory, float DeltaSeconds)
{
Super::TickTask(OwnerComp, NodeMemory, DeltaSeconds);
ABasicEnemy* basicEnemy = Cast<ABasicEnemy>(OwnerComp.GetAIOwner()->GetPawn());
FVector loc = basicEnemy->GetActorLocation();
UBlackboardComponent* blackboard = OwnerComp.GetBlackboardComponent();
int count = blackboard->GetValueAsInt("Count");
if (count++ < 150)
loc.X += 4.0f;
else if (count < 150 * 3)
loc.X -= 4.0f;
else if (count < 150 * 4)
loc.X += 4.0f;
else
count = 0;
blackboard->SetValueAsInt("Count", count);
basicEnemy->SetActorLocation(loc);
}