Skip to content

Instantly share code, notes, and snippets.

@ariok
Last active February 22, 2024 18:12
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ariok/ba35a69ec6446fa50e720462529c3af1 to your computer and use it in GitHub Desktop.
Save ariok/ba35a69ec6446fa50e720462529c3af1 to your computer and use it in GitHub Desktop.
UE4 Perception AI System: Detect By Affiliation
#include "AIControllerTeam.h"
// A Tutorial for this code is available here:
// https://www.thinkandbuild.it/ue4-ai-perception-system/
AAIControllerTeam::AAIControllerTeam()
{
SetGenericTeamId(FGenericTeamId(5));
}
ETeamAttitude::Type AAIControllerTeam::GetTeamAttitudeTowards(const AActor& Other) const
{
if (const APawn* OtherPawn = Cast<APawn>(&Other)) {
// DEFAULT BEHAVIOR---------------------------------------------------
if (const IGenericTeamAgentInterface* TeamAgent = Cast<IGenericTeamAgentInterface>(OtherPawn->GetController()))
{
return Super::GetTeamAttitudeTowards(*OtherPawn->GetController());
}
//OR CUSTOM BEHAVIOUR--------------------------------------------------
if (const IGenericTeamAgentInterface* TeamAgent = Cast<IGenericTeamAgentInterface>(OtherPawn->GetController()))
{
//Create an alliance with Team with ID 10 and set all the other teams as Hostiles:
FGenericTeamId OtherTeamID = TeamAgent->GetGenericTeamId();
if (OtherTeamID == 10) {
return ETeamAttitude::Neutral;
}
else {
return ETeamAttitude::Hostile;
}
}
}
return ETeamAttitude::Neutral;
}
#include "CoreMinimal.h"
#include "AIController.h"
#include "GenericTeamAgentInterface.h"
#include "AAIControllerTeam.generated.h"
// A Tutorial for this code is available here:
// https://www.thinkandbuild.it/ue4-ai-perception-system/
UCLASS()
class THEMIRRORSEND_API AAIControllerTeam : public AAIController
{
GENERATED_BODY()
ASightAIController();
public:
// Override this function
ETeamAttitude::Type GetTeamAttitudeTowards(const AActor& Other) const override;
};
#include "APlayerControllerTeam.h"
APlayerControllerTeam::APlayerControllerTeam()
{
PrimaryActorTick.bCanEverTick = true;
TeamId = FGenericTeamId(10);
}
FGenericTeamId APlayerControllerTeam::GetGenericTeamId() const
{
return TeamId;
}
#include "CoreMinimal.h"
#include "GameFramework/PlayerController.h"
#include "GenericTeamAgentInterface.h"
#include "APlayerControllerTeam.generated.h"
class APlayerControllerTeam;
UCLASS()
class TUTORIAL_API APlayerControllerTeam : public APlayerController, public IGenericTeamAgentInterface
{
GENERATED_BODY()
public:
APlayerControllerTeam();
private:
// Implement The Generic Team Interface
FGenericTeamId TeamId;
FGenericTeamId GetGenericTeamId() const;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment