Announcement

Collapse
No announcement yet.

Spawning actor from a struct

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Spawning actor from a struct

    Hello. I've been pondering this for a few days now and can't find the solution

    When I set up an AActor variable inside a UStruct like this:

    Code:
     // IN .H FILE
     //Enum keys to be exposed in blueprint
     UENUM(BlueprintType)
     namespace EObjectType
     {
         enum Type
         {
             STATIC        UMETA(DisplayName = "Static Object"),
             MOVABLE     UMETA(DisplayName = "Movable Object"),
             // More types
         };
     }
     
     // Struct to assign object type to an actor in editor
     USTRUCT(BlueprintType)
     struct FObjectActor
     {
         GENERATED_USTRUCT_BODY()
     
         // Type of the game object
         UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Game Object Types")
         TEnumAsByte<EObjectType::Type> ObjectType;
     
         // The blueprint for a game object
         UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Game Object Types")
         TSubclassOf<class AActor> ObjectActor;
     };
     
             // I create an array that will hold all the Object Actors with their types
         UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Game Object Types")
         TArray<FObjectActor> ObjectActors;
     
             // I create a TMap which will assign object actors to object types
         UPROPERTY()
         TMap<TEnumAsByte<EObjectType::Type>, TSubclassOf<class AActor>> ObjectLibrary;
     
             // IN .CPP FILE
             // I copy items from ObjectActors array into the TMap
             for (int i = 0; i < ObjectActors.Num(); i++) {
             if (!ObjectLibrary.Contains(ObjectActors[i].ObjectType)) {
                 ObjectLibrary.Add(ObjectActors[i].ObjectType, ObjectActors[i].ObjectActor);
             }
         }
     
             // Now I need to spawn the actor with enum key STATIC
             // Set the spawn parameters.
         FActorSpawnParameters SpawnParams;
         SpawnParams.Owner = this->GetOwner();
         SpawnParams.Instigator = NULL;
     
         SpawnParams.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
                         
         // Spawn the actor
         AActor* NewObject = GetWorld()->SpawnActor<AActor>(ObjectLibrary.Find(EObjectType::STATIC), FVector(0, 0, 0), FRotator::ZeroRotator, SpawnParams);
    I get this error
    Code:
    C2664: 'T *UWorld::SpawnActor<AActor>(UClass *,const FVector &,const FRotator &,const FActorSpawnParameters &)': cannot convert argument 1 from 'TSubclassOf<AActor> *' to 'UClass *'
    From what I've read so far about this error is that I should use
    Code:
    TSubclassOf<SomeActorClass>
    which I already do.

    The main thing I don't understand is why does it spawn a UClass in the first place if the ObjectActor variable is AActor. There are no examples of spawning actors with this kind of setup so I really need help with understanding what I'm doing wrong.

    #2
    I think the issue here is that the first parameter of SpawnActor is
    Code:
    TSubclassOf<AActor>
    but you are trying to call it with
    Code:
    TSubclassOf<AActor> *
    This a subtle difference, but a very important one. TMap->Find returns a pointer to the value of the found reference and not just the value itself.
    Dereferencing the pointer you get from
    Code:
    ObjectLibrary.Find(EObjectType::STATIC)
    should fix your issue.

    Comment


      #3
      You are my savior. De-referencing did the trick. I also learned that I have to check whether this Type exists before spawning it. Otherwise the project crashes on launch. Thank you very much.

      Comment


        #4
        Glad to help

        Comment

        Working...
        X