I think I'm Misunderstanding Something Simple...?

Hi guys, me again! Really quick question…

I have my controls set up in my MyCharacter.cpp within the SetupPlayerInputComponent, so therefore all of my keyboard input needs to go through the MyCharacter class at the moment (quite messy, but is fine for now). So far when I have wanted to manipulate things when I press a button I have been using


for (TActorIterator<ATimeObject> ObjectIter(GetWorld()); ObjectIter; ++ObjectIter)
ATimeObject* CurrentTimeObject = *ObjectIter;

which has been serving me well. However, I’m working on AI at the moment and the main thing I seem to have missed is calling classes that don’t need to be created into objects. For example, I don’t want to (and can’t, I think) make an object of AIController within MyCharacter.cpp to allow me to execute the AIController functions.

Is there a way I can set this up so that when I press a button, my MyCharacter class can speak to the AIController class and execute one of it’s functions? I have a feeling it maybe ‘Casting’ which still confuses me.

Thanks a lot!

Hi, I think your objective is to have an AIController take control of your character? In that case you do need to create an instance (object) of type AIController and have it posses your character. You can go to your game mode settings and set the default controller class to AIController. Then you can, from MyCharacter.cpp, get your Controller and cast it to AIController. From the top of my head:
AIController* AICtlr = Cast<AIController>(Controller);
But you have to be sure that your controller is indeed of type AIController.

If you don’t want AIController to be the default controller class, you will have to create your own AIController object and posess your character with it.

Sorry, but I think you’ve misunderstood. For context, what I am trying to do is; when a button is pressed, then stop all behaviour on the AI Controller (and therefore the NPCs the controller is in charge of). So when a button is pressed, all of the NPCs in the level need to stop what they are doing and stay still, and when the button is pressed again the behaviour tree is allowed to begin again. To do this, I wanted to be able to call a function inside the AIController class (from the MyCharacter class) that would change a boolean to false. This would then be checked at the top of my behaviour tree (if false break out of behaviour tree, if true continue as normal).

I hope that makes sense. I find it really hard to get my words across sometimes!

Thanks.

Sorry I misread :smiley: you’ll need to cast the controller using what I mentioned once you captured the input event in C++. As for how to do that, maybe someone else can help.

Okay so I’ve used


ATimeAIController* AICtrlr = Cast<ATimeAIController>(Controller);
AICtrlr->SetTimeRewind();

within my MyCharacter.cpp function that is called when I press spacebar. There is a function in my TimeAIController class that does this


void ATimeAIController::SetTimeRewind()
{
	GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Yellow, TEXT("TimeAIController::SetTimeRewind"));
	TimeState = ETimeState::TimeRewind;
	BlackboardComp->SetValueAsVector(EnemyGuardLocationKey, FVector::ZeroVector);
}

If I call the SetTimeRewind function within my TimeAIController class then it works fine and the Blackboard key values are changed as I expect, but when I try and call the SetTimeRewind function from MyCharacter.cpp using the AICtrlr then the editor crashes on the TimeState and BlackboardComp lines. (if those lines were not in there then the AddOnScreenDebugMessage line works perfectly).

Super confused, anyone got any tips?