Hello everyone,
I’m working on a project and I’m just beginning to use the C++ script instead of the blueprints.
I don’t find much information on the web on how to properly spawn a new actor in the world like I did with the blueprints :
I have find that we can put in the FActorSpawnParameters a Template so the spawn object will be initialized using the property values of the template Actor. So I tried to do something like that :
//Spawn chunk
FVector Loc = FVector(0.0f, 0.0f, 0.0f);
FRotator Rot = FRotator(0.0f, 0.0f, 0.0f);
AProcedural_Terrain_Chunk* temp = NewObject<AProcedural_Terrain_Chunk>(this, "temp");
temp->Init(Chunk_X, Chunk_Y, this->Width, this->Height, this->Scale, this->Seed, this->Frequency,
this->Amplitude, this->Seed_X, this->Seed_Y);
FActorSpawnParameters params;
params.Template = temp;
params.Owner = this;
AProcedural_Terrain_Chunk* spawn = GetWorld()->SpawnActor<AProcedural_Terrain_Chunk>(Loc, Rot, params);
spawn->DisplayStats();
[HR][/HR]
My problem is that, the object spawn don’t have its values initialized to what I wanted. The function DisplayStats print in the log the value of Chunk_X, Width and Height. Everything work just fine when I execute it on temp but everything is displayed at 0 when I execute it on spawn.
I don’t know how to fix my problem, I need to have the correct value when the function BeginPlay is executed by the object that I just spawn. I don’t know if there is a easier way to do what I want.
Thanks for the ones that have taken the time to read me and to help me.
[HR][/HR]
Source :
UWorld::SpawnActor : UWorld::SpawnActor | Unreal Engine Documentation
FActorSpawnParameters : FActorSpawnParameters | Unreal Engine Documentation
Template : Template | Unreal Engine Documentation