Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Register a sight sense missing tidbit. #1

Open
bormanst opened this issue May 9, 2021 · 0 comments
Open

Register a sight sense missing tidbit. #1

bormanst opened this issue May 9, 2021 · 0 comments

Comments

@bormanst
Copy link

bormanst commented May 9, 2021

Thanks for the posting. It all worked except for the indicated note (got it working with the BP extension, as well, but not pure C++). Did a little digging into engine to see what the bAutoRegisterAllPawnsAsSources flag was controlling.

ShouldAutoRegisterAllPawnsAsSources() returns bAutoRegisterAllPawnsAsSources

In AIPerceptionSystem.cpp: ShouldAutoRegisterAllPawnsAsSources() triggers RegisterSource(SenseID, Pawn).

So I just added the equivalent method call to BeginPlay() which registers the player pawn as a new source to monitor. AIDebugger now shows player being tracked while other pawns ignored.

PlayerCharacter.h >>
...
virtual void BeginPlay() override;
...

PlayerCharacter.cpp >>
...
void APlayerCharacter::BeginPlay()
{
Super::BeginPlay();

// RegisterSource:  Should probably validate GetWorld and PerceptionSystem???
UAIPerceptionSystem::GetCurrent(GetWorld())->RegisterSource(*this);

}
...

I found in some cases the AIPerceptionSystem was not fully initialized on first spawn of the player. Not sure how to properly address that one but there is a simple workaround using a timer.

void APlayerCharacter::BeginPlay()
{
Super::BeginPlay();

// Set RegisterSourceCheck.
GetWorldTimerManager().SetTimer(RegisterSourceTimerHandle, this, &APlayerCharacter::RegisterCheck, 1.0f, true);

}

void APlayerCharacter::RegisterSourceCheck()
{
// Check IsSenseInstantiated.
if (UAIPerceptionSystem::GetCurrent(GetWorld())->IsSenseInstantiated(UAISense::GetSenseID<UAISense_Sight>()))
{
// RegisterSource.
UAIPerceptionSystem::GetCurrent(GetWorld())->RegisterSource(*this);

	// ClearTimer.
	GetWorldTimerManager().ClearTimer(RegisterSourceTimerHandle);
}

}

The timer runs a few ticks before the IsSenseInstantiated becomes true. The bAutoRegisterAllPawnsAsSources=true switch probably allows the engine to fully instantiate UAIPerceptionSystem prior to any spawning any pawns because it knows what's coming. Whereas, setting bAutoRegisterAllPawnsAsSources=false and then manually adding Senses/Sources with classes may cause the AIPerceptionSystem to finalize after the designated classes are loaded???

FWIW

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant