Download

Why can’t I announce the queue?

I want to create objects and then destroy them, but I cannot save the created objects to the queue.



TQueue <AActor*> que = TQueue <AActor*>();

void AMyProjectCharacter::Spawn()
{
    if (ToSpawn)  
    {
        UWorld* world = GetWorld();

        if (world)
        {
            FActorSpawnParameters spawnParams;
            spawnParams.Owner = this;  
            FRotator rotator;  

            for (int i = 0; i < 100; i++)  
            {
                FVector spawnLocation = FVector(FMath::RandRange(0, 10000), FMath::RandRange(0, 10000), FMath::RandRange(0, 10000));
                AActor* actor = world->SpawnActor<AActor>(ToSpawn, spawnLocation, rotator, spawnParams);
                que.Enqueue(actor);
            }
        }
    }
}  

void AMyProjectCharacter::Despawn()
{
    while (que.IsEmpty == false)  
    {
        AActor a = que.Dequeue;
        a.Destroy();
    }
}


I get compilation errors.

*Building MyProjectEditor…

Using Visual Studio 2019 14.24.28314 toolchain (C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.24.28314) and Windows 10.0.18362.0 SDK (C:\Program Files (x86)\Windows Kits\10).

Building 4 actions with 6 processes…

[1/4] MyProjectCharacter.cpp

E:\Unreal Projects\MyProject\Source\MyProject\MyProjectCharacter.cpp(87) : error C2280: “TQueue<AActor *,EQueueMode::Spsc>::TQueue(const TQueue<AActor *,EQueueMode::Spsc> &)”: ?।?ਭ??? ???⪠ ??뫪? ?? 㤠??? ?㭪???

E:\Programs\Unreal Engine 4.23\UE_4.24\Engine\Source\Runtime\Core\Public\Containers/Queue.h(293): note: ??. ?????? “TQueue<AActor *,EQueueMode::Spsc>::TQueue”

E:\Programs\Unreal Engine 4.23\UE_4.24\Engine\Source\Runtime\Core\Public\Containers/Queue.h(293): note: “TQueue<AActor *,EQueueMode::Spsc>::TQueue(const TQueue<AActor *,EQueueMode::Spsc> &)”: ?㭪??? ? 㤠???.

E:\Unreal Projects\MyProject\Source\MyProject\MyProjectCharacter.cpp(114) : error C3867: “TQueue<AActor *,EQueueMode::Spsc>::IsEmpty”: ???⠭??? ᨭ⠪???; ?ᯮ??? “&”, ?⮡? ᮧ??? 㪠??⥫? ?? 童?

E:\Unreal Projects\MyProject\Source\MyProject\MyProjectCharacter.cpp(116) : error C3867: "TQueue<AActor ,EQueueMode::Spsc>::Dequeue": ???⠭??? ᨭ⠪???; ?ᯮ??? “&”, ?⮡? ᮧ??? 㪠??⥫? ?? 童?

A a couple things going wrong here:



TQueue <AActor*> que; // This should in your class header file. I guess you can keep it as a global, but it's bad form IMO. You can skip the "= blah blah". In C#, that would be used to new up object but in C++, it's unnecessary.

void AMyProjectCharacter::Spawn() // For the most part, this function seems fine.
{
    if (ToSpawn)  
    {
        UWorld* world = GetWorld();

        if (world)
        {
            FActorSpawnParameters spawnParams;
            spawnParams.Owner = this;  
            FRotator rotator;  

            for (int i = 0; i < 100; i++)  
            {
                FVector spawnLocation = FVector(FMath::RandRange(0, 10000), FMath::RandRange(0, 10000), FMath::RandRange(0, 10000));
                AActor* actor = world->SpawnActor<AActor>(ToSpawn, spawnLocation, rotator, spawnParams);
                que.Enqueue(actor); // You probably want to add an "if(actor)" check, as your actor can fail to spawn (can't find a spot to spawn it, collision, etc).
            }
        }
    }
} 

void AMyProjectCharacter::Despawn()
{
    while (que.IsEmpty() == false)   // IsEmpty is a function, not a property (like in C#). You were missing the "()"
    {
        AActor* a = que.Dequeue(); // Dequeue is also a function, can't forget the "()", also it returns a pointer - not an object.
        a->MarkPendingKill(); // You don't normally want to call destroy your self, just mark it as pending kill and the GC will get it.
    }
}


Thank you, it helped!