I have a scene component that uses addforce() to create a gravity-esque acceleration that points in the owner’s z axis. When added to an asset that is not based off of a C++ or Blueprint class, the component works as it should, but when attached to an asset that is based off of a C++ or Blueprint class the component returns a nullptr exception. I think that the component is unable to find the owner when attached to class based asset but I am unsure as to why. (I am not completely confident in this hypothesis as I am still inexperienced with Unreal.) Here is my c++ code:
#pragma once
#include "CoreMinimal.h"
#include "Components/SceneComponent.h"
#include "SceneGravity.generated.h"
UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
class TESTS_API USceneGravity : public USceneComponent
{
GENERATED_BODY()
public:
// Sets default values for this component's properties
USceneGravity();
protected:
// Called when the game starts
virtual void BeginPlay() override;
public:
// Called every frame
virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;
UStaticMeshComponent* Mesh;
UPROPERTY(EditAnywhere)
float GravStrength = -2000.0f;
};
#include "SceneGravity.h"
// Sets default values for this component's properties
USceneGravity::USceneGravity()
{
// Set this component to be initialized when the game starts, and to be ticked every frame. You can turn these features
// off to improve performance if you don't need them.
PrimaryComponentTick.bCanEverTick = true;
// ...
}
// Called when the game starts
void USceneGravity::BeginPlay()
{
Super::BeginPlay();
Mesh = Cast<UStaticMeshComponent>(GetOwner()->GetRootComponent());
// ...
if (Mesh == nullptr){
UE_LOG(LogTemp, Warning, TEXT("Gravity references nullptr"));
}
}
// Called every frame
void USceneGravity::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
const FVector Force = this->GetUpVector();
if (Mesh != nullptr) {
/*Exception thrown : read access violation.
this->Mesh was nullptr.*/
Mesh->AddForce(Force * Mesh->GetMass() * GravStrength);
}
// ...
}
.
#include "SceneGravity.h"
// Sets default values for this component's properties
USceneGravity::USceneGravity()
{
// Set this component to be initialized when the game starts, and to be ticked every frame. You can turn these features
// off to improve performance if you don't need them.
PrimaryComponentTick.bCanEverTick = true;
// ...
}
// Called when the game starts
void USceneGravity::BeginPlay()
{
Super::BeginPlay();
Mesh = Cast<UStaticMeshComponent>(GetOwner()->GetRootComponent());
// ...
if (Mesh == nullptr){
UE_LOG(LogTemp, Warning, TEXT("Gravity references nullptr"));
}
}
// Called every frame
void USceneGravity::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
const FVector Force = this->GetUpVector();
if (Mesh != nullptr) {
/*Exception thrown : read access violation.
this->Mesh was nullptr.*/
Mesh->AddForce(Force * Mesh->GetMass() * GravStrength);
}
// ...
}