AI optimization

Ok, so here you go:

You have to create your own CharacterMovementComponent:
And add to it’s header:

UCLASS()
class YOUR_GAME_NAME_API UCM: public UCharacterMovementComponent
{
	GENERATED_BODY()

protected:
	//Init
	virtual void InitializeComponent() override;


public:

        //Useful to make sure that your characters are using the correct Component.
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = StubCheck)
	bool m_dummyToMakeSure; 

	virtual void TickComponent(float DeltaTime, enum ELevelTick TickType, FActorComponentTickFunction *ThisTickFunction) override;

private:
	float m_DeltaTime;
	int m_nTicksCount;
	int	m_nTickGroup;

	bool m_SkipFloorChecks;
};

In the CPP

void UCM::InitializeComponent()
{
	Super::InitializeComponent();

	m_nTickGroup = FMath::RandRange(3, 4);
	m_DeltaTime = 0.0f;
	m_SkipFloorChecks = FMath::RandBool();
	m_nTicksCount = 0;
}

void UCM::TickComponent(float DeltaTime, enum ELevelTick TickType, FActorComponentTickFunction *ThisTickFunction)
{
   	m_DeltaTime += DeltaTime;
	if (!CharacterOwner->GetMesh()->bRecentlyRendered)
	{
		if (++ m_nTicksCount % m_nTickGroup == 0)
		{
			m_nTicksCount = 0;
			DeltaTime = m_DeltaTime;
		}
		else
		{
			return;
		}
		
	}

	m_DeltaTime = 0.0f;

	//Call Default CharacterMovementComponent
	Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
}

This code snippet will cull of some of the Movement Component overhead for an invisible Characters.
Theoretically it should increase the number of Characters (that are off-screen) twice.

Please refer to this Wiki Page: on how to create a custom CharacterMovementComponent

2 Likes