Hello everyone,
I am experiencing an issue where two different Actors are checking a bool contained in the Game Mode (Single player game, so it should be local) and getting two different results.
I am trying to set an alarm - one actor checks every tick to see if the alarm in the game mode is true, so that it can implement different behavior. The other actor sets the game mode alarm variable to true if it sees the player and the alarm is currently false. The actor waiting to see if the alarm is true always sees it as false. The actor that is trying to set the game mode alarm bool to true always sees it as true. This is happening simultaneously. Note that I have debugged the code and it is not failing at any cast or pointer check - it gets all the way to the point where it checks the bool values.
In my Game mode.cpp constructor I have declared a public bool:
bIsBotAlarmed = false;
This is the AI checking every tick to see if the alarm has been set. When the game starts, this AI sees the correct result of bIsBotAlarmed (which is false). Note that the bIsAlarmSet bool variable is set to false in this AI’s constructor.
void ARobotAICharacter::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
//check if this bot's alarm is set, if it's not, check if it should be set to true
if (!bIsAlarmSet)
{
AAITest1GameMode * CurrentGameMode;
CurrentGameMode = (AAITest1GameMode*)GetWorld()->GetAuthGameMode();
if (CurrentGameMode)
{
//check if the game mode alarm variable is true. If it is, set the blackboard alarm key value to true as well
// this AI is seeing this variable as false (the correct value)
if (CurrentGameMode->bIsBotAlarmed)
{
bIsAlarmSet = true;
ARobotAIController* RobotAIController = Cast<ARobotAIController>(GetController());
if (RobotAIController)
{
UBehaviorTreeComponent* OwnerComp;
OwnerComp = RobotAIController->GetBehaviorTreeComp();
if (OwnerComp)
{
uint8 BlackboardKeyID_RobotAlarmSet = OwnerComp->GetBlackboardComponent()->GetKeyID("bisAlarmOn");
OwnerComp->GetBlackboardComponent()->SetValue<UBlackboardKeyType_Bool>(BlackboardKeyID_RobotAlarmSet, true);
}
}
}
}
}
}
This is the AI that is trying to set the alarm - when it sees the player it checks to see if the game mode bool bIsBotAlarmed is false - if so, it executes code to set it to true. However, it always sees it as true (simultaneously to the other AI seeing it as false).
void AAlarmRobotAICharacter::OnSeePlayer(APawn* SensedPawn)
{
Super::OnSeePlayer(SensedPawn);
AAITest1GameMode * GameModeRef;
GameModeRef = (AAITest1GameMode*)GetWorld()->GetAuthGameMode();
if (GameModeRef)
{
// This AI is seeing this variable as true (the incorrect value)
if (GameModeRef->bIsBotAlarmed)
{
AAITest1Character * CharacterPawn = Cast<AAITest1Character>(SensedPawn);
if (CharacterPawn)
{
SetAlarm();
}
}
}
}
Sorry for the messy code, I have torn this apart and rewritten it many times now with the same result each time. I’m sure that I am just missing something really simple. I really appreciate your help!
-Alex