~~
**2 Two Nodes For You
Create UObject
Create Primitive Component, Added to Scene at Location!**
These two nodes let you create UObjects at runtime!
I recently needed to create UObjects in Blueprints for a special inventory system! Please note you can use my node to create UObjects that you make Blueprintable via C++ !
Please note you absolutely must save off the return value to a variable or UE4 will Garbage Collect your new UObject within a short!
Please especially note that if you create a Primitive Component, I actually add it to the world for you so it is visible and has collision!
**C++ Code For You**
Here's the code!
```
UObject* UVictoryBPFunctionLibrary::**CreateObject**(UObject* WorldContextObject,UClass* TheObjectClass, FName Name)
{
if(!TheObjectClass) return NULL;
//~~~~~~~~~~~~~~~~~
//using a context to get the world!
UWorld* const World = GEngine->GetWorldFromContextObject(WorldContextObject);
if(!World) return NULL;
//~~~~~~~~~~~
return StaticConstructObject( TheObjectClass, World, Name);
}
```
```
UPrimitiveComponent* UVictoryBPFunctionLibrary::**CreatePrimitiveComponent**(
UObject* WorldContextObject,
TSubclassOf<UPrimitiveComponent> CompClass,
FName Name,
FVector Location,
FRotator Rotation
){
if(!CompClass) return NULL;
//~~~~~~~~~~~~~~~~~
//using a context to get the world!
UWorld* const World = GEngine->GetWorldFromContextObject(WorldContextObject);
if(!World) return NULL;
//~~~~~~~~~~~
UPrimitiveComponent* NewComp = ConstructObject<UPrimitiveComponent>( CompClass, World, Name);
if(!NewComp) return NULL;
//~~~~~~~~~~~~~
NewComp->SetWorldLocation(Location);
NewComp->SetWorldRotation(Rotation);
NewComp->RegisterComponentWithWorld(World);
return NewComp;
}
```
♥