Is there a way to dump an entire object?

I am implementing a log system for helping my team with the development process. At certain moments I need to be able to dump actors into a readable file, including all of their variables and values. I could do this by hand just printing and formatting each one, but there are a lot of different entities that I’d like to be able to dump. So doing it one by one is not doable, furthermore considering that the variable list of each one may change during development. I tried using DumpObject, but it only shows basic info related to the actor, not my specific type.

We are working 90% with Blueprints, so our actors come from them. In order to try the DumpObject approach I created a C++ Blueprint Function Library and passed the actor from a BP to a function there.

I would go with one of 2 routes

  • use json to serialize object if it needs to be human readable (unfortunately takes only ustructs)
  • Implement Serialize with FArchive for the object (would probably need a base object, a bit more messy to maintain)

Oh and turn on JSON Blueprint Utilities in plugins or use the json implementation in c++

1 Like

Considering you are working mostly with blueprints, your actors and properties should be fully exposed to the reflection system. You can use that to write a generic boilerplate to dump data.

Something like this :

void DumpObjectProperties(UObject* Obj, const FString& FileName)
{
	FString Data;

	if (Obj)
	{
		Data += FString::Printf(TEXT("Object path : %s\n"), *Obj->GetFullName());
		UClass* Class = Obj->GetClass();
		Data += FString::Printf(TEXT("Object class : %s\n"), *Class->GetFullName());
		while (Class)
		{
			Data += FString::Printf(TEXT("--Properties inherited from class '%s'\n"), *Class->GetName());
			for (TFieldIterator<FProperty> Prop(Class, EFieldIteratorFlags::ExcludeSuper); Prop; ++Prop)
			{
				FString Value;
				Prop->ExportText_InContainer(0, Value, Obj, Obj, NULL, 0);
				Data += FString::Printf(TEXT("\t- %s = %s\n"), *Prop->GetName(), *Value);
			}
			Class = Class->GetSuperClass();
		}
	}
	else
	{
		Data = "NULL";
	}

	FString FilePath = FPaths::ProjectSavedDir() / FileName;
	FFileHelper::SaveStringToFile(Data, *FilePath);
}
1 Like