当前位置:   article > 正文

Ue4反射使用_ue containerptrtovalueptr

ue containerptrtovalueptr
// Fill out your copyright notice in the Description page of Project Settings.


#include "HelloGameModeBase.h"
#include"MyObject.h"
#include "MiddleStudent.h"
#include "UObject/UObjectHash.h"
#include "UObject/UObjectIterator.h"
AHelloGameModeBase::AHelloGameModeBase()
{
	UE_LOG(LogTemp, Warning, TEXT("ddddddddddddddddddddddddd"));

	//1.1 创建MyObject对象
	UMyObject* MyObject = NewObject<UMyObject>();
	//2.1 得到MyObject对象的UClass,并且得到类的名称
	UClass* ObjectClass = MyObject->GetClass();
	FString ClassName = ObjectClass->GetName();
	
	//1.2 通过对象得到对象的名称
	FString ObjectName = MyObject->GetName();
	UE_LOG(LogTemp, Warning, TEXT("MyObjectname..%s"),*ObjectName);
	UE_LOG(LogTemp, Warning, TEXT("classname..%s"),*ClassName);

	//2.2 获取父类的名称
	UMiddleStudent* middleStudent = NewObject<UMiddleStudent>();
	UClass* parentClass = middleStudent->GetClass()->GetSuperClass();
	FString parentClassName = parentClass->GetName();
	UE_LOG(LogTemp, Warning, TEXT("parentClass name;::%s"), *parentClassName);

	//2.3.1 获取类(string)属性
	for (UProperty* Property = ObjectClass->PropertyLink; Property; Property = Property->PropertyLinkNext) {
		FString propertyName = Property->GetName();
		FString propertyType = Property->GetCPPType();
		if (propertyType == "FString") {
			UStrProperty* StringProperty = Cast<UStrProperty>(Property);
			void* addr = StringProperty->ContainerPtrToValuePtr<void>(MyObject);
			FString PropertyValue = StringProperty->GetPropertyValue(addr);
			UE_LOG(LogTemp, Warning, TEXT("myObject has type is %s,name is %s,Value is %s"), 
				*propertyType,*propertyName ,*PropertyValue);
	//2.3.2 设置类(string)属性				
			StringProperty->SetPropertyValue(addr, "lisi");
			FString afterValue = StringProperty->GetPropertyValue(addr);
			UE_LOG(LogTemp, Warning, TEXT("after set value:::%s"), *afterValue);
		}
	}
	//2.4 获取类的方法
	for (TFieldIterator<UFunction> iterOfFunc(ObjectClass); iterOfFunc; ++iterOfFunc) {
		UFunction* function = *iterOfFunc;
		FString funcName = function->GetName();
		UE_LOG(LogTemp, Warning, TEXT("MyObject function:::%s"), *funcName);
	}
	//2.4.2通过名称获取方法
	UFunction* funPtr = UMyObject::StaticClass()->FindFunctionByName(TEXT("Study"), EIncludeSuperFlag::ExcludeSuper);
	if (funPtr) {
		FString studyFucName = funPtr->GetName();
		UE_LOG(LogTemp, Warning, TEXT("find study function name:::%s"), *studyFucName);

	//2.4.3 调用函数
		//1.给参数分配空间
		uint8* params = static_cast<uint8*>(FMemory_Alloca(funPtr->ParmsSize));
		//2.参数赋值
		for (TFieldIterator<UProperty> iterOfParam(funPtr); iterOfParam; ++iterOfParam) {
			UProperty* fucParam = *iterOfParam;
			FString pName = fucParam->GetName();
			if (pName == FString("InGame"))
			{
				*fucParam->ContainerPtrToValuePtr<FString>(params) = "ball";
			}
		}
		//3.调用
		MyObject->ProcessEvent(funPtr, params);
	}

	//2.5 查找特定类的所有子类
	TArray<UClass*> derivedClassArr;
	GetDerivedClasses(UMyObject::StaticClass(), derivedClassArr, true);
	for (int32 index = 0; index < derivedClassArr.Num(); index++)
	{
		UClass* curDerClass = derivedClassArr[index];
		FString curDerClassName = curDerClass->GetName();
		UE_LOG(LogTemp, Warning, TEXT("derived class name:::%s"), *curDerClassName);
	}

	//2.6 查找由指定类生成出的所有对象
	TArray<UObject*> objectArr;
	GetObjectsOfClass(UMyObject::StaticClass(), objectArr, true);
	for (int32 index = 0; index < objectArr.Num(); index++) {
		FString curObjName = objectArr[index]->GetName();
		UE_LOG(LogTemp, Warning, TEXT("curObjName::::%s"), *curObjName);
	}

	//2.7 通过名称查找类
	UClass* findedClass = FindObject<UClass>(ANY_PACKAGE, *FString("MyObject"), true);
	if (findedClass)
	{
		UE_LOG(LogTemp, Warning, TEXT("find class::::%s"), *findedClass->GetName());
	}

	//2.8遍历所有类
	/*for (TObjectIterator<UClass>cIt; cIt; cIt++) {
		FString cName = cIt->GetName();
		UE_LOG(LogTemp, Warning, TEXT("all class::::%s"), *cName);
	}*/

	//3.1 根据名称查enum
	UEnum* findedEnum = FindObject<UEnum>(ANY_PACKAGE, *FString("MyEnum"), true);
	if (findedEnum)
	{
		UE_LOG(LogTemp, Warning, TEXT("find class::::MyEnum"));
	}










	//100.判断是否是父类
	UClass* class1 = UMiddleStudent::StaticClass();
	UClass* class2 = UMyObject::StaticClass();
	UClass* class3 = AActor::StaticClass();

	bool ischild = class1->IsChildOf(class2);
	UE_LOG(LogTemp, Warning, TEXT("IsChildOf ::%d"), ischild);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/133287
推荐阅读
相关标签
  

闽ICP备14008679号