You have to load the asset into memory before you can access it.
Thank you for the reply, but I still have a problem with casting. The objectInMemory is not null, but the object still is.
UObject* objectInMemory = AssetDatas[0].ToStringReference().TryLoad();
AObject* object = Cast<AObject>(objectInMemory);
Are you certain that the first asset data object is of class AObject?
Yes, I found out that i can access the variable if i spawn the object with:
SpawnActor<AObject>
But i think it is not very efficient
This works, thank you!
I want to get a variable from my objects in UObjectLibrary. The variable is defined in c++ in the AObject class, only the cast to this class returns null. The object in the library that i use for testing is a blueprint with as parentclass AObject. I hope someone has a solution.
UClass* cls = TSubclassOf<class AObject>();
auto ObjectLibrary = UObjectLibrary::CreateLibrary(cls, false, true);
ObjectLibrary->LoadAssetDataFromPath(FString(TEXT("/Game/Objects")));
TArray<FAssetData> AssetDatas;
ObjectLibrary->GetAssetDataList(AssetDatas);
UE_LOG(LogTemp, Warning, TEXT("Found objects: %d"), AssetDatas.Num());
AObject* object = Cast<AObject>(AssetDatas[0].GetAsset()); //Always returns null
UE_LOG(LogTemp, Warning, TEXT("Variable: %i", object->variable));
AssetDatas[0].ToStringReference().TryLoad()
returnes the UObject if it can
If it is a class that you want to spawn you can do something like:
getClass()->GetDefaultObject()
That way you get the CDO of the class. Doesn’t work with pure assets though.