Character retuning none when attempting to access component

I am still fairly new to coding and unreal so sorry if this is a bit of a dumb question but iv’e been trying to figure this out for a while and its driving me insane. So I have a pawn that I am having act as the character I play as and I have made a component that I want to handle the player input and movement. All components are created with the player constructor below.

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

	CharacterRootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("Character Root Component"));
	RootComponent = CharacterRootComponent;

	CharacterStaticMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Character Static Mesh Component"));
	CharacterStaticMesh->SetupAttachment(RootComponent);

	CharacterMovementHandler = CreateDefaultSubobject<UCharacterMovementHandler>(TEXT("Character Movement Handler"));
}

When I view the character in the blueprint editor I can see all of the components present, create references to them and call the functions within the component from the editor. These are my functions that are currently in my component.

UCharacterMovementHandler::UCharacterMovementHandler()
{
	CurrentCharacter = GetOwner();
	if (CurrentCharacter != nullptr)
	{
		UE_LOG(LogTemp, Warning, TEXT("Movment handler reporting for %s."), *CurrentCharacter->GetName())
	}
	else
	{
		UE_LOG(LogTemp, Warning, TEXT("Movment handler failed/not assigned."))
		return;
	}
}

void UCharacterMovementHandler::MoveForward(float Axis)
{
	UE_LOG(LogTemp, Warning, TEXT("MoveForward test."));
}

void UCharacterMovementHandler::MoveLeft(float Axis)
{
	UE_LOG(LogTemp, Warning, TEXT("MoveLeft test."));
}

But my understanding is the access none means you are trying to access something that doesn’t exist. However the logging that I added for the constructor for the appears saying that the component is there and reporting on the controlled pawn, however I cannot seem to get any of the functions within that component to work. One thing I read suggested it may be a naming conflict so I tried making several other components with different names and still had no luck.

So I am assuming that there is something small that I must be missing that would be causing this to not work, so after 2 days of testing and research I am trying to reach out and hoping someone can help me solve this. Thank you ahead of time.

No none of them are declared as UPROPERTY.

That didn’t seem to fix it. I still get spammed with:

“Blueprint Runtime Error: Accessed None trying to read property CharMoveHandler from function: ‘ExecuteUbergraph_PlayerCharacter_BP’ from node: Move Forward in graph: EventGraph in object: PlayerCharacter_BP with description: Accessed None trying to read property CharMoveHandler”

But from what you are saying, and my interpretation is that when the game starts and my character gets spawned, it makes my component, but then after its construction is it ending up getting deleted? That’s what I thought also but when I look at the components of my player while spawned in the game its still listed:

251491-active-components.png

I also never heard about the garbage collection thing before now so I looked it up, and it also seems like it only does that when it sees a component is no longer being used but would that be the case due to the MoveForward() function continually being called, or am I just completely misunderstanding that?

There is a chance that your components are actually being garbage collected. When declaring them on your header file did you declare them as UPROPERTY()?

That is why they are being garbage collected. Make sure that when you declare a component it is declared as follows:

UPROPERTY(EditAnywhere, Category = "Components")
UStaticMeshComponent MyStaticMesh;

Not sure where my original reply is (had a screenshot of my character showing in the game, that the component was still attached to my pawn), but regardless this didn’t seem to work. I had never known about this garbage collecting before so I read up a bit on it but it seems to me that it only does that if the engine thinks that component in this case, is no longer relevant but it doesn’t seem like that would be the case if I am trying to constantly call a function from the component. Also to set my mind at ease I added a tick function to the component to add a log saying that its still there, still attached to the pawn and can still report every tick and its doing that, but it just refuses to let me access MoveForward().

As an update I went ahead and removed the creation of my component from my playercharacter cpp and made it blueprint spawnable and added it that way and it worked fine. Would that mean there could be something wrong with how I referenced my CharacterMovementHandler?

Upon further testing it turns out I am just unfamiliar with Unreal. Having the UPROPERTY() of EditAnywhere was the incorrect property for what I was trying to do, Making it BlueprintReadWrite or BlueprintReadOnly, which should have been obvious to me but it was my first time using a c++ component that wasn’t BlueprintSpawnable. Live and learn.

Upon further testing it turns out I am just unfamiliar with Unreal. Having the UPROPERTY() of EditAnywhere was the incorrect property for what I was trying to do, Making it BlueprintReadWrite or BlueprintReadOnly, which should have been obvious to me but it was my first time using a c++ component that wasn’t BlueprintSpawnable. Live and learn.