如何获取一个Actor的所有属性和公开的变量。

虚幻引擎确实是提供这种功能的,它运用了虚幻引擎的反射,这个反射不是光线传播的那个反射。
https://dev.epicgames.com/documentation/zh-cn/unreal-engine/reflection-system-in-unreal-engine?application_version=5.4

SomeInterface.h

...//some code
virtual SetActorProperty(AActor* InActor, const FString& PropertyName, const FString& Value )
{} = 0;
...//Some other code
SomePlayerController.h

UCLASS()
class SOME_API ASomePlayerController : public APlayerController, SomeInterface
{
...// Some code
    UFUNCTION(BlueprintCallable)
    virtual SetActorProperty(AActor* InActor, const FString& PropertyName, const FString& Value ) override;
...// Some other code
};
SomePlayerController.cpp

bool ASomePlayerController::SetActorProperty(AActor* InActor, FName InPropertyName, const FString& InValue)
{
	if (InActor == nullptr) return false;

	// Get the actor class
	UClass* ActorClass = InActor->GetClass();
	if (ActorClass == nullptr) return false;

	// Get the property
	FProperty* Property = ActorClass->FindPropertyByName(InPropertyName);
	if (Property == nullptr) return false;

	// Set the property value
	Property->ImportText_Direct(*InValue, Property->ContainerPtrToValuePtr<void>(InActor), InActor, PPF_None);

	return true;
}
SomeActor.h

UCLASS()
class ASomeActor : AActor
{
...// Some code
    UPROPERTY(BlueprintReadWrite, EditAnywhere)
    FString testString = "Hello world!";
...// Some other code
};

主楼的需求实现应该就完成了,代码是纯手打的,没有智能提示有些地方可能有错误,交给IDE吧。

1 Like