Hi,
I’ve written this method to spawn a class inheriting ASpawnable, which inherits from AActor:
ASpawnable* MemoryHelper::SpawnEntity(UObject* context, TSubclassOf<ASpawnable> entityClass, std::vector<object> initParams)
{
FTransform spawnArgs(FRotator::ZeroRotator, FVector::ZeroVector);
auto entity = Cast<ASpawnable>(UGameplayStatics::BeginDeferredActorSpawnFromClass(context, entityClass.StaticClass(), spawnArgs));
entity->Init(initParams);
UGameplayStatics::FinishSpawningActor(entity, spawnArgs);
}
What it should do is this:
- Create an empty FTransform
- Begin spawning the actor, cast to the derived class which is inheriting ASpawnable
- Call Init() with those params. This is the tricky part, ASpawnable has a pure virtual function Init which AFuelTank, a class which inherits from ASpawnable, overrides. This pure virtual function in ASpawnable is there so this method can call the Init function of any class deriving from ASpawnable, as each of them can have different arguments for Init and I don’t know how to call this properly to pass the arguments. I believe it will have to involve reflection.
I’m getting a few errors with this;
std::vector<object> initParams;
error on 'object', undefined
entityClass.StaticClass();
error on 'StaticClass', TSubclassOf<ASpawnable> has no member 'StaticClass'
entity->Init(initParams);
error on 'initParams' as I have no idea how to have params in the method signature
If you could help me figure out how to do this, I’d really appreciate it.