Download

[SOLVED] Chose random endpoint for AI Move to

Hello,

I’m trying to spawn AI characters that chose one of a few different level exits and move toward that.

My plan currently is to have the level exits represented by a blueprint class, so the designer can move them as they please in each level in the editor…the position and number of exits is undetermined currently and probably varies level to level.

I’ not sure how to get a random array element and have a reference to it’s pawn, so I can “get actor location” on it.

Any help Appreciated!

Here is where I’m at currently…

// get array of PossibleDestination actors, choose one at random, set ChosenDestination
	
	TArray<AActor*> PossibleDestinations;
	UGameplayStatics::GetAllActorsOfClass(GetWorld(), ASSPossibleDestinations::StaticClass(), *&PossibleDestinations); 

    ***HELP::	ChosenDestination =		[FMath::FRand()*PossibleDestinations.Num()]

	// need to get index from PossibleDesintation array , and set ChosenDestination

	// Set Destination value as ChosenDestination->GetActorLocation
	GetBlackboardComponent()->SetValueAsVector(TEXT("Destination"), );
// get the number of destinations
int32 NumDestinations = PossibleDestinations.Num();

// randomly select a number between 0 and the max value
// (-1 because arrays are indexed from 0)
int32 SelectedDestinationIdx = FMath::RandRange(0, NumDestinations - 1);

// get the selected destination from the array
AActor* SelectedDestination = PossibleDestinations[SelectedDestinationIdx];

This is working great.

Thanks so much!

1 Like