Hello, I don’t know if I should post here, but I’ll write it down for now. I don’t know if I should go to UI or which category I should go to. I want AI to move when I press the game start button on the main screen now. But I don’t know how to write the code.
void AOLAIController::StartAI()
{
UBlackboardComponent* BlackboardComp = Blackboard.Get(); // 블랙보드 가져오기
// 사용할 블랙보드 지정하기(블랙보드를 사용할 거라면 꼭 해야 함)
if (UseBlackboard(BBAI, BlackboardComp))
{
FName CurrentPositionName = TEXT("CurrentPos"); // 현재 위치 이름
GetBlackboardComponent()->SetValueAsVector(CurrentPositionName, GetPawn()->GetActorLocation()); // 현재 위치로 블랙보드 값(CurrentPos) 변경
RunBehaviorTree(BTAI); // 행동 트리 실행
UE_LOG(LogTemp, Display, TEXT("RunAI"));
}
}
I want to run the code above. So I made the blue print when I pressed the button in UI as below. But it doesn’t move..
You are not getting anyone’s controller.
You need a reference to the AI pawn (or controller) somehow.
Is there only one such AI in the level ? If yes, you can use “Get Actor From Class”.
If there are multiple, how do you choose which one to start ? If you wanna start all of them, use Get All Actors From Class.
Hello, thank you so much for your response!
I’ve been thinking about Delegate… but I don’t have a clue what to do…
The blueprint only works for Player, and the AI part doesn’t work at all.
The AI moved when I wrote it with the picture in the blueprint. However, if more than two people are placed, only one AI moves.
I brought the Get All Actors From Class with the picture below, but I don’t know what to do here..
Set Input Mode UI Only enabled the mouse to be active on UI only.
And I tried writing about the function when I click the button in UI. I changed it to not work when it comes to UI using Set Input Mode Game Only. (Since I don’t use this, I had to click the UI button and click the screen again to make the character move normally. But even if I don’t use it, Player seems to be moving.)
And I disabled the mouse cursor in the GameStart function. I think this is how it works.
I don’t write down the bShowMouseCursor part, so the character didn’t move normally.
Have you seeted the ENUM EAutoPossessAI to PlacedInWorldOrSpawned.
Example:
I have created mutiples AI Controllers, one for Melee and other for Ranged, they come from a custom AIController, there I init my behavior tree, so in my Enemy Class Default settings I have defined it.
The instructions above may solve your issue of just one AI moving.
In order to solver your condition of your AI only moves when you press the play button, simple add a new node in your Behavior Tree that check if the game has started. For instance, from your UI, after click play, set a varible bGameHasStarted (custom variable) to true in your GameMode, which your IA can check if it true, so it can move.
I came into Forum yesterday during the day and not at night. So I didn’t see the comments speczynk wrote me so carefully..! (sorry)
Anyway, I worked it out. Of course, it might be a little different from how SpecZynk told me.
The AI Controller Class was set up normally.
Auto Possess AI has been changed to Placed in World or Spawned.
If this happens next time, I’ll refer to it.
First of all, my solution is that I imported AI’s controller from GameMode. I used it because the AI controller had a StartAI function.
Let me show you some of the codes first.
void AOLGameMode::GameStart()
{
TArray<AActor*> AllNPC; // 월드에 있는 모든 NPC를 담을 배열
UGameplayStatics::GetAllActorsOfClass(GetWorld(), AOLNPCBasic::StaticClass(), AllNPC);
for (AActor* Actor : AllNPC)
{
if (AOLNPCBasic* NPC = Cast<AOLNPCBasic>(Actor))
{
AController* NPCController = NPC->GetController();
IOLAIControllerInterface* AIControllerInterface = Cast<IOLAIControllerInterface>(NPCController);
AIControllerInterface->StartAI();
}
}
}
I called the function in the AIcontroller through the interface. If you don’t use the interface, I think you can call the function in the AI controller after changing it to public.
I took the actors from that class in the world and casted the AOLNPCBasic, which is the NPC class I’m using, and called the function.
What’s important here is that I did UFUNCTION(BlueprintCallable) for the GameStart function. As shown in the picture, I took the game mode and called the GameStart function (which I made) in the game mode.