I try to spawn a custom actor using C++,but it do not work,is there someone can help me?thanks!
here is my actor:
head file:
#pragma once
#include "GameFramework/Actor.h"
#include "PlaceableActor.generated.h"
UCLASS()
class YC_API APlaceableActor : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
APlaceableActor();
// Called when the game starts or when spawned
virtual void BeginPlay() override;
// Called every frame
virtual void Tick( float DeltaSeconds ) override;
UPROPERTY(VisibleDefaultsOnly,BlueprintReadOnly,Category = Placeable)
TSubobjectPtr<USphereComponent> BaseCollisionCom;
UPROPERTY(VisibleDefaultsOnly, BlueprintReadWrite, Category = Placeable)
TSubobjectPtr<UStaticMeshComponent> PlaceableMesh;
UFUNCTION()
void OnHit(AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit);
};
CPP file:
// Fill out your copyright notice in the Description page of Project Settings.
#include "YC.h"
#include "PlaceableActor.h"
#include "Engine.h"
// Sets default values
APlaceableActor::APlaceableActor()
{
if (GEngine)
{
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Yellow, TEXT("OBJECT CREATE..."));
}
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
BaseCollisionCom = CreateDefaultSubobject<USphereComponent>(TEXT("BaseCollisionCom"));
BaseCollisionCom->OnComponentHit.AddDynamic(this, &APlaceableActor::OnHit);
RootComponent = BaseCollisionCom;
PlaceableMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("PlaceableMesh"));
PlaceableMesh->SetSimulatePhysics(true);
PlaceableMesh->AttachTo(RootComponent);
}
// Called when the game starts or when spawned
void APlaceableActor::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void APlaceableActor::Tick( float DeltaTime )
{
Super::Tick( DeltaTime );
}
void APlaceableActor::OnHit(AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit)
{
if (GEngine)
{
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Yellow, TEXT("HIT"));
}
}
i use SpawnActor to create it , but the actor do not spawn sucess.
Can someone tell me where am i wrong?
Thanks!!!
UPROPERTY(EditDefaultsOnly, Category = Placeable)
TSubclassOf PlaceableActorClass;
UWorld* const World = GetWorld();
World->SpawnActor(PlaceableActorClass, SpawnLocation, SpawnRotation);