Spawn AActor with custom attributes | Template not working

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

For more context, I’m working on a procedural generation of a terrain.

Everything is going great so far, except for the spawn of the chunks actors.

My goal is to spawn actor around the player and destroy them when they are too far away.

Blueprint graphs contains hidden nodes and hidden pins everywhere.
That spawner node is actually 3. It’s a node that spawns another two nodes, it spawns these two hidden nodes and copy the values of inputs it has as pins. Then it move its exec pins to the hidden nodes and link them together to be executed by the blueprint graph.

The two hidden nodes it creates execute a specific function each…

“BeginDeferredSpawn” which does this in C++:

and “FinishDeferredSpawn” that does this:

it also will manually execute the “Construction” graph of the new spawned actor if actor placement adjustment didn’t fail.

2 Likes

Thank you a lot for your indications. I make my code work.

For those who may find this topic in the futur, here as an exemple the change that I have made to my code :



    //Spawn chunk
    FVector Loc = FVector(0.0f, 0.0f, 0.0f);
    FRotator Rot = FRotator(0.0f, 0.0f, 0.0f);
    FTransform Tran = FTransform(Rot, Loc);

    UWorld* const World = GetWorld();

    if (World) {

        AProcedural_Terrain_Chunk* spawn = World->SpawnActorDeferred<AProcedural_Terrain_Chunk>(this->ChunkClass,
            FTransform::Identity, this, NULL, ESpawnActorCollisionHandlingMethod::AlwaysSpawn);

        if (spawn) {
            spawn->Init(Chunk_X, Chunk_Y, this->Width, this->Height, this->Scale, this->Resolution, this->Seed, this->Frequency,
                this->Amplitude, this->Seed_X, this->Seed_Y);


            spawn->FinishSpawning(Tran);

            //Add new chunk in this->AllObject
            this->AllObject.Add(spawn);
        }

    }


Thank you again =)

1 Like