Creating and adding properties at runtime

Hi,

I want to add UProperties (FProperty in c++) to some UObject at runtime.

this is for a plugin that should change assets in the editor. i add a short use case to make it clear. in the editor i have blueprint “MyBP” and i want to add “MyProp” to it via code (triggered by an editor widget). as a result, when i open “MyBP” in the asset editor, “MyProp” should be visible under the variables section of the blueprint and when i save the asset, the property should be there after loading. in general the whole blueprint should just work as if i added the property via asset editor or if i added a UPROPERTY in the class header in c++.

I don’t want any workaround for that. i already have workarounds. i specifically want to add properties via code ^^

at the moment i can add properties but they don’t show in the editor and are not saved. here is my code

			EObjectFlags Flags = EObjectFlags::RF_Transient | EObjectFlags::RF_Public | EObjectFlags::RF_MarkAsNative;
			EPropertyFlags PFLags = EPropertyFlags::CPF_Edit 
				| EPropertyFlags::CPF_BlueprintVisible 
				| EPropertyFlags::CPF_SaveGame
				| EPropertyFlags::CPF_Net
				| EPropertyFlags::CPF_SaveGame
				| EPropertyFlags::CPF_IsPlainOldData
				| EPropertyFlags::CPF_Interp
				| EPropertyFlags::CPF_SimpleDisplay;
			//LDK_PROPERTY_ANIM_PERCENTAGE is the name
			FFloatProperty* Prop = new FFloatProperty(ObjectClass, FName(LDK_PROPERTY_ANIM_PERCENTAGE), Flags);
			//FFloatProperty* Prop = new FFloatProperty(ObjectClass, FName(LDK_PROPERTY_ANIM_PERCENTAGE), Flags, MaxOffset, PFLags);
			Prop->SetPropertyFlags(PFLags);
			ObjectClass->AddCppProperty(Prop);
			ObjectClass->Modify();

thanks in advance

Hi, did you ever find a solution to this?

well, actually kind of :slightly_smiling_face:

first, this only works for blueprint assets when selecting in editor. then you have to open up the asset editor for the asset after adding. they should show up there. then you can save from there. havent found a way to skip these two steps yet. if somebody knows, please tell me :wink:

so here is an example

if (UBlueprint* BlueprintAsset = Cast<UBlueprint>(Object))
{
  FName PropFName = FName("MyProperty");
  FEdGraphPinType PinType;
  PinType.ContainerType = EPinContainerType::None;
  PinType.PinCategory = "float";
			 
FBlueprintEditorUtils::AddMemberVariable(BlueprintAsset, PropFName, PinType, "0.0");
  const int32 VarIndex = 
  FBlueprintEditorUtils::FindNewVariableIndex(BlueprintAsset, PropFName);
if (VarIndex != INDEX_NONE)
  {
    BlueprintAsset->NewVariables[VarIndex].PropertyFlags &= ~CPF_DisableEditOnInstance;
    BlueprintAsset->NewVariables[VarIndex].PropertyFlags |= CPF_Interp;
  }
			FBlueprintEditorUtils::SetVariableSaveGameFlag(BlueprintAsset, PropFName, true);
}

with

PinType.ContainerType = EPinContainerType::None;
PinType.PinCategory = "float";

you can create arrays and set the base type of elements as well

hope that helps :wink:

just a small update on that. there seems to be a bug in UE5.0 that prevents from creating float properties and creates integer properties instead.

see post here:
https://forums.unrealengine.com/t/adding-float-property-to-blueprint-asset-via-c-creates-integer-property-instead/639654