UObject class in Unreal Engine

What class to select to fit the needs? How they differ each other and for what purpose are they intended?

Inheritance Hierarchy

Iterating over UObjects

The function below can be used for all Run-Time instances of UObject. Iterators in Unreal Engine are always accurate. More info about Iterators in the Iterators handbook

#include "EngineUtils.h"
void AExampleClass::PrintAllObjectsNamesAndClasses()
{
  for (TObjectIterator<UObject> Itr; Itr; ++Itr )
  {
    ClientMessage(Itr->GetName());
    ClientMessage(Itr->GetClass()->GetDesc());
  }
}
  • Object iterator is going to iterate over objects in the Pre-PIE world / the Editor World. This is not an issue when the game is running as an independent Game Instance / the editor is closed. Otherwise it can lead to unexpected results.
  • Object Iterator does not require a UWorld* Context - so Object Iterator is the way to get the proper context and access the entire living game world even when there's not possible to obtain the World().
  • UObject iterator can contain as well as iterate over all components that are inherited from this class, see Get Asset based of Class example. It can also iterate over TObjectIterator<AActor> Itr;, TObjectIterator<ACharacter> Itr; and others.

Get Asset based of Class

As UObject is a class from which many Classes is inherited, it may be used for a parameter that allow to transfer any type of its inherited assets, see the code below:

#include "Engine/StaticMesh.h" // Engine
#include "Engine/SkeletalMesh.h" // Engine
#include "Engine/Texture.h" // Engine
#include "Animation/AnimSequence.h" // Engine
#include "Engine/DataTable.h" // Engine
#include "Sound/SoundWave.h" // Engine
UAssetImportData* UExampleClass::GetAssetImportData(UObject* Asset)
{
  if (Cast<UStaticMesh>(Asset) != nullptr) // UStaticMesh
  {
    return Cast<UStaticMesh>(Asset)->AssetImportData;
  }
  else if (Cast<USkeletalMesh>(Asset) != nullptr) // USkeletalMesh
  {
    return Cast<USkeletalMesh>(Asset)->GetAssetImportData();
  }
  else if (Cast<UTexture>(Asset) != nullptr) // UTexture
  {
    return Cast<UTexture>(Asset)->AssetImportData;
  }
  else if (Cast<UAnimSequence>(Asset) != nullptr) // UAnimSequence
  {
    return Cast<UAnimSequence>(Asset)->AssetImportData;
  }
  else if (Cast<UDataTable>(Asset) != nullptr) // UDataTable
  {
    return Cast<UDataTable>(Asset)->AssetImportData;
  }
  else if (Cast<USoundWave>(Asset) != nullptr) // USoundWave
  {
    return Cast<USoundWave>(Asset)->AssetImportData;
  }
  // .. any other asset type inherited from UObject
  return nullptr;
}