Announcement

Collapse
No announcement yet.

Proper use of TAssetSubclassOf

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

    Proper use of TAssetSubclassOf

    Hello again.

    During a PIE session the initial object described by a blueprint is loaded and made available:

    Code:
    .h
        TAssetSubclassOf<AMyPawn> myPawnTemplate;
    
    
    .cpp
        void APawnManager::BeginPlay()
        {
            :
            gameInstance->AssetLoader.SimpleAsyncLoad(myPawnTemplate.ToStringReference());
            :
        }

    Then for spawning the following steps are taken:

    Code:
        void APawnManager::Tick(float DeltaSeconds)
        {
            :
                if (myPawnTemplate.IsValid())
                {
                    AMyPawn* pawn = GetWorld()->SpawnActor<AMyPawn>(myPawnTemplate.Get(), spawnLocation, spawnRotation);
                }
            :
        }
    I'm not sure what I'm doing wrong, or incorrectly, but it seems that this will only work the first time. What changes need to be made in order to have it work reliably? That is, always spawn each time the session is started.

    For the record, this was modeled from the following link: https://wiki.unrealengine.com/TAsset..._Asset_Loading

    Any help you can provide is appreciated.

    Thank you.

    #2
    I've added EndPlay and destroying the object but that doesn't seem to help either:

    Code:
    void APawnManager::EndPlay(const EEndPlayReason::Type EndPlayReason)
    {
        myPawnTemplate.Get()->GetDefaultObject<AMyPawn>()->Destroy();
    
        Super::EndPlay(EndPlayReason);
    }

    Comment


      #3
      I need to keep this at the top. I need a solution to this issue since it would be extremely difficult to proceed without one. As it stands now I need to restart the UE4 Editor for each testing cycle which is counterproductive. Does anyone know how to work around this problem?

      Comment


        #4
        I still need help with this issue. When launch, and more importantly, re-launching the game I'd like to make sure that 10 out of 10 times it works as expected. Unfortunately I cannot re-spawn pawns in the game. It only works the first time. Does anyone know how to correct this?

        Comment


          #5
          This is a little disappointing. I'm using FStreamableManager::SimpleAsyncLoad in BeginPlay(), and FStreamableManager::Unload in EndPlay() and yet when I restart the level it appears to contain remnants of the previous run. How do I get around this?

          Funny thing is I believe someone had posted this issue too. . .but I can't recall the thread.

          Comment


            #6
            I don't know much about how this works, you'd have to debug it and get more information for anyone to be able to say why it only works the first time. But I suspect it's related to how the game instance works within the editor. See this post here.

            Also, you shouldn't be doing this:
            Code:
            myPawnTemplate.Get()->GetDefaultObject<AMyPawn>()->Destroy();
            The result of GetDefaultObject is a template object for the class; you shouldn't be trying to destroy it.

            Comment

            Working...
            X