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"

Crash when trying to load save game data in CPP

Hi there! My game crashes when I try to load data from my custom save class. Specifically, it crashes as soon as I try to run ContainsData(), which simply returns the boolean m_containsData. Help would be much appreciated! Thank you in advance.

 // UTMFSaveGame variable declarations in the constructor
 UTMFSaveGame::UTMFSaveGame()
 {
     m_containsData = false;
     m_defaultSaveSlotIndex = 10;
     m_defaultSaveSlotName = "";
 }
 void AInteractable::OnInteractableLoaded_Implementation()
 {
     // Create save object
     UTMFSaveGame* saveObject = Cast<UTMFSaveGame>(UGameplayStatics::CreateSaveGameObject(UTMFSaveGame::StaticClass()));
     UE_LOG(LogTemp, Error, TEXT("Creating save object"));
     // Check if the save object has an instance of the object stored
     saveObject = Cast<UTMFSaveGame>(UGameplayStatics::LoadGameFromSlot(saveObject->m_defaultSaveSlotName, saveObject->m_defaultSaveSlotIndex));
     UE_LOG(LogTemp, Error, TEXT("Loading save object"));
     // Check if the save object has information stored within it
     if(saveObject->ContainsData())
     {
         UE_LOG(LogTemp, Error, TEXT("Save object contains data")); // Crash before this gets logged
         // Check to see if information about our interactable is stored
         if (FInteractableInfo* loadedInfo = saveObject->GetInteractableInfoMap()->Find(this->GetName()))
         {
             // We know that we have information stored in the save file - now load this into the game instance
             Cast<UTMFGameInstance>(GetGameInstance())->GetInteractableInfoMap()->Add(this->GetName(), *loadedInfo);
             UE_LOG(LogTemp, Error, TEXT("Loaded interactable object information"));
         }
     }
 }

Product Version: UE 4.18
Tags:
more ▼

asked Jul 31 '18 at 10:34 PM in C++ Programming

avatar image

Borzi
174 44 39 49

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

1 answer: sort voted first

Found the fix and thought it would be worth sharing. First, its is better to assign a name to the slot, because otherwise you will not find a useful error log indicating that the save file could not be found in the folder (in fact, it could have been that not naming the file was partly what caused the crash). In addition, not having the actual file in the save folder will lead to a crash, even if you are just trying to read information that is assigned in the save file constructor. So the final code should look something like this:

 void AInteractable::OnInteractableLoaded_Implementation()
 {
     // Create save object
     UTMFSaveGame* saveObject = Cast<UTMFSaveGame>(UGameplayStatics::CreateSaveGameObject(UTMFSaveGame::StaticClass()));
     // Check if the save game file exists
     if (UGameplayStatics::DoesSaveGameExist(saveObject->m_defaultSaveSlotName, saveObject->m_defaultSaveSlotIndex))
     {
         // Check if the save object has an instance of the object stored
         saveObject = Cast<UTMFSaveGame>(UGameplayStatics::LoadGameFromSlot(saveObject->m_defaultSaveSlotName, saveObject->m_defaultSaveSlotIndex));
         // Check to see if information about our interactable is stored
         if (FInteractableInfo* loadedInfo = saveObject->GetInteractableInfoMap()->Find(this->GetName()))
         {
             // We know that we have information stored in the save file - now load this into the game instance
             Cast<UTMFGameInstance>(GetGameInstance())->GetInteractableInfoMap()->Add(this->GetName(), *loadedInfo);
         }
     }
 }
more ▼

answered Aug 01 '18 at 09:06 AM

avatar image

Borzi
174 44 39 49

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
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