Hello !
I am trying to spawn class from an array of said classes and so far it has not been working at all and that’s an understatement.
Basically I just have an abstract class and I am inheriting from this class so I can have different effect I can apply to my actor from a list. I have the setup working in blueprint but it’s cleaning/optimising time and I want to try to put it in C++ since I am learning right now.
Here is my abstract class :
#pragma once
#include "CoreMinimal.h"
#include "UObject/NoExportTypes.h"
#include "BaseSpellEffector.generated.h"
UCLASS(Blueprintable, abstract)
class POWER_API UBaseSpellEffector : public UObject
{
GENERATED_BODY()
public:
UFUNCTION(BlueprintCallable)
virtual void transform(TArray<FVector>& positions) { UE_LOG(LogTemp, Log, TEXT("Please work")); }
};
Here is my child class :
#pragma once
#include "CoreMinimal.h"
#include "SpellSystem/BaseSpellEffector.h"
#include "SpellEffectSpawnCircle.generated.h"
/**
*
*/
UCLASS(Blueprintable)
class POWER_API USpellEffectSpawnCircle : public UBaseSpellEffector
{
GENERATED_BODY()
public:
void transform(TArray<FVector>& positions) override;
};
Here is the .cpp :
#include "SpellEffectSpawnCircle.h"
void USpellEffectSpawnCircle::transform(TArray<FVector>& positions)
{
for (FVector& pos : positions)
{
pos += FVector(0.f, 1.f, 0.f);
UE_LOG(LogTemp, Log, TEXT("It's working"));
}
}
My container class has this property :
UPROPERTY(EditAnywhere, BlueprintReadWrite, Meta = (ExposeOnSpawn = true))
TArray<TSubclassOf<UBaseSpellEffector>> spellEffects;
And the function where I am trying to create the classes is :
void ASpellContainer::BeginPlay()
{
Super::BeginPlay();
for (auto& effect : spellEffects)
{
//UBaseSpellEffector* newEffect = Cast<UBaseSpellEffector>(effect);
UBaseSpellEffector* newEffect = NewObject<UBaseSpellEffector>(this, effect->StaticClass());
//newEffect->transform(spellPositions);
}
}
I have tried several different versions of NewObject call and I get the message “Class must be a child of BaseSpellEffector”. I checked the name of the class and it indeed return my child class but effect->StaticClass() seems to return only class().
If you have any idea !
Thank you !