Custom assertion for softptr fails even though it points to an asset

Engine version: 4.27.2

Hi! I’m currently working on a game where I have data tables that stores information about various things in the game. In the code I have softpointers pointing to those data tables. To make sure those pointers actually point to an asset I have written an assertion in the function that gets the datatable.

Now, The problem is that even though I have set those assets inside the editor, it crashes when I try to run the project, which it shouldn’t. I’m at a complete loss as to why and hope someone can illuminate where the problem may lie. Here’s some information on what I have:

(The softptr is stored inside of a custom gameinstance.)

Here’s the declaration of the variable inside the game instance .h file:

UPROPERTY(EditDefaultsOnly, Category = "Data Table References")
TSoftObjectPtr<UDataTable> AllItemsDataTableRef;

Here’s the function that loads and returns the datatable inside the .cpp file. With my assertion at the top. I have also tried using != nullptr instead of IsValid()

UDataTable* UMainGameInstance::GetAllItemsDataTable() const
{
	checkf(AllItemsDataTableRef.IsValid(), TEXT("'AllItemsDataTableRef' has no asset referenced, make sure you create and reference an items data table."));
	
	UE_LOG(MainGameInstanceLog, Display, TEXT("AllItemsDataTableRef was found.. Returning it."));
	return Cast<UDataTable>(AllItemsDataTableRef.LoadSynchronous());
	
}

Here’s the table asset being referenced inside the editor:

Am I missing something about how assertions work? From what I understand it checks if the condition is false and then halts the processing. The condition .IsValid() should return true based on my setup but it doesn’t.

Thanks

If you remove the checkf() does the DT get loaded and returned?

Looking at source it seems like IsValid on a TSoftObjectPtr would only return true when the object is already loaded:

	/**  
	 * Test if this points to a live UObject
	 *
	 * @return true if Get() would return a valid non-null pointer
	 */
	FORCEINLINE bool IsValid() const
	{
		// This does the runtime type check
		return Get() != nullptr;
	}

What you’re maybe looking for is IsNull - here’s what that one looks like in source:

	/**  
	 * Test if this can never point to a live UObject
	 *
	 * @return true if this is explicitly pointing to no object
	 */
	FORCEINLINE bool IsNull() const
	{
		return SoftObjectPtr.IsNull();
	}

Edit: I just realized the source I have on hand is 5.4.4, though I doubt it would be that different here

1 Like

I switched to IsNull() and it works wonder now. Thank you for taking the time! I didn’t think of going through the source to find out how it works, that’s a great approach and I’ll remember to do that in the future.

Thanks!

1 Like