x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

Saving Asset Ptrs

For the game I'm working on we're going to be having a lot data representing assets being passed around, to minimise the potential overheads of having references to materials and textures etc, I am storing data to objects as TAssetPtrs.

The issue comes when trying to save this data, I've set up a simple example, and I can save and load the data fine, but Unreal doesn't like serialising AssetPtrs out of the box, and advises that I use FArchiveUObject to store the data. I've looked around quite a lot for reference on this, but outside of Ramas saving and loading tutorial (which I don't think QUITE applies to this scenario) I'm struggling to work out how to use them. My basic understanding is that << is both reading or writing data into the archive based on whether ArIsLoading or ArIsSaving is true, so I'm simply making use of that (clearly in some way I'm either slightly or hugely mistaken in my implementation)

Like I said saving and loading for everything else is working fine, so I think other aspects of my process are okay, it's just particularly how to save AssetPtrs. My understanding of the archive system isn't great, so I imagine it's something I'm doing at the actual point of trying to read the information into/out of the archive.

Here's the struct data for my saves:

 USTRUCT()
 struct FSavableDataFile
 {
     GENERATED_USTRUCT_BODY()
 
     UPROPERTY()
         FText textContent;
 
     FArchiveUObject texture;
 
     FSavableDataFile(){}
 
     FSavableDataFile(FDataFile fileToCopy)
     {
         textContent = fileToCopy.textContent;
         texture.ArIsSaveGame = true;
         texture.ArIsSaving = true;
         texture << fileToCopy.texture;
     }
 
     void LoadFile(FDataFile& fileToLoad)
     {
         texture.ArIsSaveGame = true;
         texture.ArIsLoading = true;
         fileToLoad.textContent = textContent;
         texture << fileToLoad.texture;
     }
 };

In case it's useful I'll include the related save classes below.

The actual saved struct:

 USTRUCT()
 struct FNoteSaveData
 {
     GENERATED_USTRUCT_BODY()
 
     UPROPERTY(SaveGame)
         UClass* noteType;
     UPROPERTY(SaveGame)
         FVector location;
     UPROPERTY(SaveGame)
         FRotator rotation;
     UPROPERTY(SaveGame)
         FSavableDataFile data;
 
     FNoteSaveData(){}
 
     FNoteSaveData(UClass* classType, FVector newLoc, FRotator newRot, FDataFile fileData)
     {
         noteType = classType;
         location = newLoc;
         rotation = newRot;
         data = FSavableDataFile(fileData);
     }
 };

and the actual save and load functions:

 void ANoteTestGameMode::SaveGame()
 {
     FString PlayerName = TEXT("PlayerOne");
     UNoteTest_SaveGame* SaveGameInstance = Cast<UNoteTest_SaveGame(
         UGameplayStatics::CreateSaveGameObject(UNoteTest_SaveGame::StaticClass()));
     SaveGameInstance->PlayerName = PlayerName;
 
     TArray<AActor*> foundNotes;
     UGameplayStatics::GetAllActorsOfClass(GetWorld(), ANote_Base::StaticClass(),
         foundNotes);
 
     for (int i = 0; i < foundNotes.Num(); ++i)
     {
         SaveGameInstance->savedNotes.Add(Cast<ANote_Base>(foundNotes[i])->GetSaveData());
 
         FDataFile tempData;
         SaveGameInstance->savedNotes.Last().data.LoadFile(tempData);
     }
 
     UGameplayStatics::SaveGameToSlot(SaveGameInstance, SaveGameInstance->SaveSlotName, 
         SaveGameInstance->UserIndex);
 }
 
 void ANoteTestGameMode::LoadGame()
 {
     UNoteTest_SaveGame* LoadGameInstance = Cast<UNoteTest_SaveGame>(
         UGameplayStatics::CreateSaveGameObject(UNoteTest_SaveGame::StaticClass()));
     LoadGameInstance = Cast<UNoteTest_SaveGame(
         UGameplayStatics::LoadGameFromSlot(LoadGameInstance->SaveSlotName, 
             LoadGameInstance->UserIndex));
 
     FString PlayerNameToDisplay = LoadGameInstance->PlayerName;
     if (GEngine)
     {
         GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Yellow, PlayerNameToDisplay);
     }
 
     FNoteSaveData tempData;
     ANote_Base* tempNote;
     FDataFile tempFile;
     for (int i = 0; i < LoadGameInstance->savedNotes.Num(); ++i)
     {
         tempData = LoadGameInstance->savedNotes[i];
         tempNote = GetWorld()->SpawnActor<ANote_Base>(tempData.noteType, 
                 tempData.location, tempData.rotation);
 
         if (tempNote)
         {
             tempData.data.LoadFile(tempFile);
             tempNote->SetData(tempFile);
         }
     }
 }

Thanks ahead of time for any information anyone can provide!

Product Version: UE 4.17
Tags:
more ▼

asked Oct 09 '17 at 01:46 PM in C++ Programming

avatar image

Ungalyant
102 11 16 23

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

0 answers: sort voted first
Be the first one to answer this question
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question