SpawnActor with template crash

Hello!

I’m doing an item system and got to a point where I need to split an item stack. Basically, I have an item stack that is an actor, and I need to create a copy of it and it’s components. I’ve found SpawnActor() method does what I need when supplied with the pointer to object that needs to be copied as a template parameter. Code goes something like this:

FActorSpawnParameters Parameters;
Parameters.Template = this;
ACharacterItemBase* Clone = GetWorld()->SpawnActor<ACharacterItemBase>(this->GetClass(), Parameters);

Problem is, this works perfectly for components generated in code, but if I ever call it on an object that has additional components added through the editor, program crashes during the SpawnActor function call (no log). I have tried running the debugger and caught an “Access violation writing location” exception at mallocTBB.cpp line 93.

Am I doing something terribly wrong here or is it an engine bug perhaps?

Ok, I did a bit of experimenting, it turns out minimum I have to do to reproduce the issue is to create a component on an actor from code (arrow for testing) by calling CreateDefaultSubobject, then add another component from the editor and then call SpawnActor with Parameters.Template = this;. Bug or am I missing something?

Sounds like classic nullptr.

The reason you have this problem is (I think) because you need the Blueprint Generated Class as the spawn template, however, I can’t remember how you get to it. Kamrann would probably know the answer to that…

Thanx for answering!

I really hope there is a way to fix this. I’m losing a ton of time at a pretty critical moment.

A little bump on this one since I haven’t really goten an answer yet.