How to get the the Max number of U Objects in UE4?

Hi,

Both of the following are available in CoreSettings.cpp.

MaxObjectsInEditor = 12 * 1024 * 1024;
MaxObjectsInGame = 2 * 1024 * 1024;

I want to calculate the Max UObject in game (2 * 1024 * 1024), but not through fetching this value (MaxObjectsInGame).

I tried the to create an instance in UObjectArrah.h/cpp and it worked but sometimes the dll forget that its there [could not locate in the dynamic link library], and throws some errors, and to fix them, I would need to recompile the project. So this solution is not 100% stable.

Anyone has an idea of how to get the same value but with permanent solution?

here is the new declared function

int32 FUObjectArray::GetObjectArrayLimit() const
{
	return ObjObjects.Capacity();
}

That variable’s size is just the default, it will be overridden by whatever is in the .ini files.

So you can just query the config system for it, since that’s the same way the engine will decide on that number:

	int32 MaxUObjects = 0;
	GConfig->GetInt(TEXT("/Script/Engine.GarbageCollectionSettings"), TEXT("gc.MaxObjectsInGame"), MaxUObjects, GEngineIni);

Depending on what you’re trying to do you might also find this useful; if you want to know how many UObjects you can still create, you can directly call GUObjectArray.GetObjectArrayEstimatedAvailable().

The GUObjectArray is the actual array instance holding all the UObjects alive and is exported so it’s available in all code that depends on the CoreUObject module.

I need to calculate how many UObjects are in scene and how many are Unreal Max Limit. If we are above 90% of the Limit, then I want to display the remaining on screen

Example:

Unreal UObject Limit = 100
Used UObjects in scene= 93

Display  7 on screen

 Im using the following code to get the objects in scene.
/* Get the current number of UObjects in the scene */
const int32 UObjectCount = GUObjectArray.GetObjectArrayNum();

I need the number of Max Limit for Mobile Devices. ObjObjects.Capacity(); should return it but the DLL is not happy about it

Because the ObjObjects variable is private, that’s why you should use the config method I mentioned which returns that same number:

To get the current object count, you don’t need to call ObjObjects.GetObjectArrayNum(), that function is already exposed through the GUObjectArray variable, so you can call GUObjectArray.GetObjectArrayNum() instead to get that exact number.
As you can see it just gets it from the internal object:

	/**
	 * Returns the size of the global UObject array, some of these might be unused
	 *
	 * @return	the number of UObjects in the global array
	 */
	FORCEINLINE int32 GetObjectArrayNum() const 
	{ 
		return ObjObjects.Num();
	}

If you really need access to ObjObjects then it’s exposed via GUObjectArray.GetObjectItemArrayUnsafe(), so you can do things like

GUObjectArray.GetObjectItemArrayUnsafe().GetObjectArrayNum();

Thats helpful, I ended up exposing another function to get the Capacity, and it seems to work so far

UObjectArray.h


	int32 GetObjectArrayLimit() const;

UObjectArray.cpp

int32 FUObjectArray::GetObjectArrayLimit() const
{
	return ObjObjects.Capacity();
}

if that causes any issues in the future, i’ll use the following instead

GUObjectArray.GetObjectItemArrayUnsafe().GetObjectArrayNum();
1 Like