Why Is my Actor Component Function Called Twice

Good Morning,

I have an odd question, which I am sure is something simple I am missing. None the less I can not figure it out.

I have created an actor component. In that component it has one function call TestFunction(). All Test function does is print a message to the log.

I have been using CreateDefaultSubObject to attach it to my Character.

If I call the function from the character Testclass->TestFunction() the message is getting printed twice.

What am I doing wrong?

Masoric

For the life of me I can not figure out what is happening here. I am just going to have to remove them and figure out a different method.

Hello, could you post screen shots of your code?

Sure…

This is the code in the test actor function

void UTestActorComponent::TestFunction()
{
	UE_LOG(LogTemp, Warning, TEXT("Did This call Twice?"));
}

This is the code in the Char Controller.

#include "NPC/NPCBase.h"
#include "NPC/UtilityAIBrain.h"
#include "NPC/Actions/WanderAction.h"
#include "AIController.h"
#include "NPC/TestActorComponent.h"
#include "NPC/BaseObject.h"


// Sets default values
ANPCBase::ANPCBase()
{
 	// Set this character to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;

	NPCAIController = Cast<AAIController>(GetController());
	
	NPCBrain = CreateDefaultSubobject<UUtilityAIBrain>(TEXT("The Brain"));  
	TestComp = CreateDefaultSubobject<UTestActorComponent>(TEXT("TestComponent"));
	BaseObject1 = CreateDefaultSubobject<UBaseObject>(TEXT("BaseObject"));
	
}

// Called when the game starts or when spawned
void ANPCBase::BeginPlay()
{
	Super::BeginPlay();

	BaseObject1->Testing();
	TestComp->TestFunction();
}

}

I have been testing with a few different components and parent classes, so lots more includes then nessisary

Hello, I think a good place to start debugging would be to get some more info from your log message. I would update the UE_LOG code to:


void UTestActorComponent::TestFunction()
{
	UE_LOG(LogTemp, Warning, TEXT("Did This call Twice? Owning Actor: %s | Component Name: %s")   
            *GetOwner()->GetName(),*GetName());
}
  

This will help us figure out if the function is actually being called twice or if something else is happening.

That throws a no operands error.

If I switch it to

	UE_LOG(LogTemp, Warning, TEXT("Did This call Twice? Owning Actor: %s | Component Name: %s") ) *GetOwner()->GetName(),*GetName());

I get a :

|Error|C4834|discarding return value of function with ‘nodiscard’ attribute

Sorry about the syntax errors. Not at my computer to test but I think it’s missing a comma.

UE_LOG(LogTemp, Warning, TEXT("Did This call Twice? Owning Actor: %s | Component Name: %s"), *GetOwner()->GetName(),*GetName());

Here is the logging wiki to help trouble shoot: Logging | Unreal Engine Community Wiki

Please do not apologize you are helping me. I thought it was missing one but these days I just assume everyone knows more then me. But SOB that did the job.

There was a second copy of the char blueprint hiding outside the visible area. Hence it was showing as called twice. FACEPALM

Thank you

1 Like

great, glad that worked!