Hi, I’ve create an actor and an ActorComponent that should move the actor like is done for CharacterMovementComponent.
All works when the CPP instance Actor is put in the world (using editor).
The problems occurs when I extend the actor from CPP using BP. When I insert this BP_Actor in the world (using editor) the actor remain stationary.
The tick function is running because in the message log I can see the position FVector (that return a position from 0, 0, 0 for Bp Actor, that is wrong because the Actor is not at center of world).
I think that the component is not registered correctly when the Actor is extended by BP because if I try to destroy the actor from the component I get this warning:
LogSpawn:Warning: Destroying /Game/MyTrackedPawnaa.Default__MyTrackedPawnaa_C, which doesn’t have a valid world pointer
This warning never occurs if I use directly the CPP actor.
Now I will post the entire code of Actor and Actor component. Thanks for help in advance.
ACTOR HEADER
class TANKS_API ATrackedPawn : public APawn
{
GENERATED_BODY()
public:
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Tank")
class UBoxComponent* BoxComponent;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Tank")
class UTrackedVehicleMovementComponent* TrackedVehicleMovementComponent;
public:
ATrackedPawn();
public:
UBoxComponent* GetBoxComponent() const {
return BoxComponent;
};
};
ACTOR CPP
ATrackedPawn::ATrackedPawn()
: Super()
{
PrimaryActorTick.bCanEverTick = true;
BoxComponent = CreateDefaultSubobject<UBoxComponent>(TEXT("BoxComponent"));
RootComponent = BoxComponent;
BoxComponent->SetCollisionProfileName(TEXT("BlockAll"));
BoxComponent->SetSimulatePhysics(false);
BoxComponent->SetMobility(EComponentMobility::Movable);
TrackedVehicleMovementComponent = CreateDefaultSubobject<UTrackedVehicleMovementComponent>(TEXT("TrachedVehicleMovementComponent"));
TrackedVehicleMovementComponent->TrackedPawn = this;
}
ACTOR COMPONENT HEADER
UCLASS(Blueprintable, BlueprintType, ClassGroup = (Custom), meta = (BlueprintSpawnableComponent))
class TANKS_API UTrackedVehicleMovementComponent : public UActorComponent
{
GENERATED_BODY()
public:
UPROPERTY(BlueprintReadWrite, Category = "Tracked vehicle movement")
class ATrackedPawn* TrackedPawn;
public:
UTrackedVehicleMovementComponent();
virtual void BeginPlay() override;
virtual void TickComponent( float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction ) override;
};
ACTOR COMPONENT CPP
UTrackedVehicleMovementComponent::UTrackedVehicleMovementComponent()
: Super()
{
PrimaryComponentTick.bCanEverTick = true;
}
void UTrackedVehicleMovementComponent::BeginPlay(){
Super::BeginPlay();
}
void UTrackedVehicleMovementComponent::TickComponent( float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction ){
Super::TickComponent( DeltaTime, TickType, ThisTickFunction );
if (TrackedPawn) {
UBoxComponent* BoxComponent = TrackedPawn->BoxComponent;
if (BoxComponent) {
FVector BoxLocation(BoxComponent->GetComponentLocation());
BoxLocation.X += 20.f;
BoxComponent->SetWorldLocation(BoxLocation);
UE_LOG(LogTemp, Error, TEXT("Tick:%s"), *BoxLocation.ToString());
}
}
}