FindObject withough ANY_PACKAGE

Heyho :slight_smile:

since ANY_PACKAGE got deprecated in UE 5.1 I am searching for alternatives to search for a c++ class by name. The suggested solution withing the deprecation msg isnt working, either when I pass an outer (for example the UWorld object) or by specifiying the package path (/Game/)

Any Help would be appreciated :slight_smile:

1 Like

I also searching for this. Getting warning in 5.1.
UClass* SaveGameClass = FindObject(ANY_PACKAGE, *SaveGameClassName);

Figured it out. Here is the solution for UEnums, but it can be generalized to other class types as well:

You have to replace ANY_PACKAGE with nullptr and prefix the class name (eg. ECustomEnum) with “/Script/[YourProjectName].”

For example, if your project name is “TPSGame”:

Before: const UEnum* enumObject = FindObject(ANY_PACKAGE, TEXT(“ECustomEnum”));
After: const UEnum* enumObject = FindObject(nullptr, TEXT(“/Script/TPSGame.ECustomEnum”));

Note that this will only find enums that are defined within your project (Engine enums will return nullptr).

Also, for anyone who happens to be dealing with UEnums, I found another more general solution here:

Bonus tip: If you don’t know a class’s context, one way you can find it out is by using FindObject on the class’s name with the deprecated ANY_PACKAGE macro, then printing *GetNameSafe(class->GetOuter()) to the log to find out what it is.

7 Likes

UE5.1 Documentation mentions:
Outer object to look inside. If this is ANY_PACKAGE it will search all in memory packages, if this is null then InName should start with a package name.
So I replaced ANY PACKAGE with nullptr.
Then I found the class I was looking for in UE Content Drawer and copied reference.

UClass* classObj = FindObject<UClass>(nullptr ,L"/Script/CoreUObject.Class'/Script/[MyProjectName].[MyClassName]'");

I hope that will be helpful.

You can use FindFirstObjectSafe() instead of FindObject()
for example, is my case this:
UClass* Result = FindObject<UClass>(ANY_PACKAGE, *Name)
changed to:
UClass* Result = FindFirstObjectSafe<UClass>(*Name);

9 Likes