反射和宏
C++本身没有反射机制,是通过”MyObject.generated.h”这个头文件实现反射机制,在每一个类声明前使用宏,就可以实现与蓝图的通信,垃圾回收等功能。
步骤
1、创建一个Uobject
2、创建后自动打开VS,我们在头文件中把类创建完整,并给上宏实现,代码如下。
头文件:
// Fill out your copyright notice in the Description page of Project Settings.#pragma once#include "CoreMinimal.h"#include "UObject/NoExportTypes.h"#include "MyObject.generated.h"/****/UCLASS(Blueprintable)class TESTCPP_API UMyObject : public UObject{GENERATED_BODY()public:UMyObject();UPROPERTY(BlueprintReadWrite)float value = 15;UFUNCTION(BlueprintCallable)void myfunction();};
实现文件:
// Fill out your copyright notice in the Description page of Project Settings.#include "MyObject.h"UMyObject::UMyObject() {}void UMyObject::myfunction(){GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Yellow, FString::Printf(TEXT("value = %d"), value));}
3、右键创建蓝图派生类,打开蓝图编辑器,测试我们写好的类,注意勾选显示继承变量,如图。
