I made a C++ class derived from ActorComponent (“BaseSkill”) and then some C++ classes derived from BaseSkill (e.g. JumpSkill). Then I made blueprint classes from those (e.g. BP_JumpSkill). Then I loaded them up into an array of CharacterSkills in the Character blueprint. I want to be able to create those skills based on the index of the CharacterSkills array.
Also, where are you calling this from ? Are you calling from a class that derives from UObject directly? Because “this” won’t work as it needs a world context(correct me if I’m wrong) object, so if you are calling this from a class derived directly from UObject, then I would do:
Correct me if I’m wrong, but by no longer using CharacterSkills[index] in NewObject<>()
I believe this will only create a UBaseSkill object and assign it the name of your BP, not creating the actual BP class.
TSubclassOf is a pointer to a class, which in this instance specifies what subclass of UBaseSkill you would like to create.
An example from my own project:
UAITask_Base& UAITasksManager::CreateTask(TSubclassOf<UAITask_Base> inClass, EAITaskPriority priority, bool activate, FName name, ETask inTaskContext)
UAITask_Base* task = NewObject<UAITask_Base>(this, inClass);
task->InitAITask(*owner->GetThisAIController(), *this, (uint8)priority);
//We need to initialize task here as well since this can be used to create task outside of manager
//If CreateTask() is called by manager, then this functionality will be overridden
task->InitializeTask(owner, owner->currentManager, inTaskContext);
// UE_LOG(LogTemp, Log, TEXT("%s is creating a task of %s, address is %d. AITasksManager::CreateTask()"), GetOwner() ? *GetOwner()->GetName() : TEXT("Owner Unknown"), inClass ? *inClass->GetName() : TEXT("class Unknown"), task);
// UE_LOG(LogTemp, Log, TEXT("Task class is %s. AITasksManager::CreateTask()"), task ? *task->GetName() : TEXT("task Unknown"));
task->VisualLogAndLog(FString::Printf( TEXT("%s is activating a task of %s. AITasksManager::CreateTask()"), GetOwner() ? *GetOwner()->GetName() : TEXT("Owner Unknown"), inClass ? *inClass->GetName() : TEXT("class Unknown")));
Has the OP verified the objects created are of the actual BP, and not just using the names?
I am having issues after upgrading to 4.24 latest. There are checks now that see if the class being created is the same as the class type passed in. They are not. One is the native c++ class, the other is a UBlueprintGeneratedClass… And so it fails. A Blueprint is not a UMyClass.
Old Posts from epic indicate to use NewObject<UObject>(…) this also fails that UObject is abstract and cannot be used.
For whatever reason, the devs have broken this feature.