天天看点

UE C++ 修改蓝图变量

参考文档:

How to check struct type included in UStructProperty? - C++ - Epic Developer Community Forums

How to access blueprint vars in c++? - #4 by ue4-archive - C++ - Epic Developer Community Forums

应用案例代码:

bool FLightUtils::SpawnRectangleLight(UWorld* InWorld, const TSharedPtr<FJsonObject>* LightElementJsonObject)
{
	const FString BPRectLightClassPath = TEXT("/Lights/BP_LightRect.BP_LightRect_C");
	UClass* BPRectLightUClass = LoadClass<AActor>(nullptr, *BPRectLightClassPath);

	const FString Energy = (*LightElementJsonObject)->GetStringField("Energy");
	const FString Position = (*LightElementJsonObject)->GetStringField("Position");
	const FString Color = (*LightElementJsonObject)->GetStringField("Color");

	const FActorSpawnParameters Params;

	AActor* BPRectLight = InWorld->SpawnActor(BPRectLightUClass,&(FVector::ZeroVector), &(FRotator::ZeroRotator),Params);
	FVector VPosition = FLightUtils::GetParamVector(Position, 10.0);
	VPosition.Y = -VPosition.Y;
	BPRectLight->SetActorLocation(VPosition);

	FindFieldChecked<UFloatProperty>(BPRectLight->GetClass(),"Width")->SetPropertyValue_InContainer(BPRectLight,200);

	const UStructProperty* VectorProp = FindFieldChecked<UStructProperty>(BPRectLight->GetClass(),"AA");
	if(VectorProp != nullptr && VectorProp->Struct == TBaseStructure<FVector>::Get())
	{
		*VectorProp->ContainerPtrToValuePtr<FVector>(BPRectLight) = FVector(1,2,3);
	}
	return false;
}
           

继续阅读