初学UE5 C++①

游戏类

1.创建所需项的类

初学UE5 C++①_第1张图片

2.创建游戏模式类,在该类上实现所需项,引入头文件和构造函数时实例化

初学UE5 C++①_第2张图片

初学UE5 C++①_第3张图片


三种时间函数类型函数和提示类型

初学UE5 C++①_第4张图片

初学UE5 C++①_第5张图片


FName、FString、FText类型相互转化

 FName用FName

FString用ToString()

FText用FText::FromString、FromName

//转化
FString MyString = TEXT("I am String");
FName MyName = FName("I am Name");
FString x = TEXT("I am a FString");
FText MyText = FText::FromString(x);

//FString-》FName
FName fName = FName(*MyString);//将string解引用为字符数组?
//FText->FName
fName = FName(*(MyText.ToString()));

//FName->FString
FString fString = fName.ToString();
//FText->Fstring
fString = MyText.ToString();

//FString-》FText
FText fText = FText::FromString(MyString);
//FName->FText
fText = FText::FromName(MyName);

初学UE5 C++①_第6张图片

数组容器

	TArrayarr;

	//增
	arr.Add(10);
	arr.Add(25);
	arr.Add(40);
	arr.Add(60);
	arr.AddUnique(35);
	arr.AddUnique(40);
	printArr();
	//删
	arr.Remove(10);//移除10元素
	arr.RemoveSingle(40);//移除第一个40
	arr.RemoveAt(1);//移除第一个
	arr.Empty();//移除所有元素
	arr.Reset();//全部为0
	printArr();
	//改
	arr.Insert(80, 0);//在index处插入,原元素后移
	int& b = arr[0];
	b = 24;
	printArr();
	//查
	arr.Contains(10);//是否包含
	arr.Find(24);//是否包含,是返回index,不是返回-1
	arr.FindLast(24);
void ASGameMode::printArr() {
	for (auto It= arr.CreateConstIterator();It;It++)
	{
		UE_LOG(LogTemp,Warning,TEXT("%d"),*It);
		GEngine->AddOnScreenDebugMessage(-1, 5.F, FColor::Blue, FString::Printf (TEXT("%d"),*It));
	}
}

 

键值容器

TMapmap;
	map.Emplace(0, 1);
	map.Emplace(1, 3);
	map.Emplace(2, 5);

	//删
	map.Remove(1);//按Key删除
	map.Empty();

	//查找
	map.Contains(2);//按key查找
	int* isFind= map.Find(5);//找5,返回指针
	const int*isFindKey= map.FindKey(2);//值找键

	//获取查找
	TArrayarrkey;
	TArrayarrayVal;
	map.GenerateKeyArray(arrkey);
	map.GenerateValueArray(arrayVal);
void ASGameMode::printmap()
{
	for (auto& TestMap:map) {
		GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Blue, FString::Printf(TEXT("key:%d,Value:%d"), TestMap.Key,TestMap.Value));
		UE_LOG(LogTemp,Display,TEXT("key:%d,Value:%d"), TestMap.Key, TestMap.Value);
	}
}

 

集合容器

TSetFruitSet;
//增
FruitSet.Add(TEXT("Apple"));
FruitSet.Add(TEXT("Orange"));
FruitSet.Add(TEXT("Banana"));
FruitSet.Emplace("Purple");//比add好,在插入集合时,避免创建临时文件
PrintFruit();
TSet TestSet2;
TestSet2.Emplace(TEXT("aaa"));
TestSet2.Emplace(TEXT("bbb"));
TestSet2.Emplace(TEXT("ccc"));
FruitSet.Append(TestSet2);
PrintFruit();
FruitSet.Remove(TEXT("aaa"));
FruitSet.Reset();
FruitSet.Empty();
PrintFruit();
int32 len=FruitSet.Num();
bool isFind=FruitSet.Contains(TEXT("bbb"));
FString* isFind2=FruitSet.Find(TEXT("ccc"));

TArray FruitArr = FruitSet.Array();

TSetTS2 = { TEXT("a"),TEXT("aa") ,TEXT("aaa") ,TEXT("aaaa") };
//长度排序
TS2.Sort([](FString A, FString B)
	{return A.Len() > B.Len(); });
void ASGameMode::PrintFruit()
{
	for (auto& TestSet : FruitSet) {
		GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Blue, FString::Printf(TEXT("%s"),* TestSet));
		UE_LOG(LogTemp, Display, TEXT("%s"), *TestSet);
	}
}

 

TSetMySet;
MySet.Add(TEXT("abc"));
FSetElementId index = MySet.Add(TEXT("bbc"));
MySet[index] = TEXT("abd");

//预留内存
TSet NewSet2;
NewSet2.Reserve(10);

for (int32 i=0;i<10;i++)
{
	NewSet2.Add(FString::Printf(TEXT("No:%d"), i));
}
for (int32 i=0;i<10;i+=2)
{
	NewSet2.Remove(FSetElementId::FromInteger(i));
}
NewSet2.Shrink();//删除末端空白元素
NewSet2.Compact();//删除空白元素

 

基本类型打印

	int32 myInt = 10;
	float myFloat = 5.f;
	bool myBool = true;
	char myChar = 'c';
	FString myString = TEXT("xxx");
	FVector myVector = FVector(1,1,1);

	UE_LOG(LogTemp,Display,TEXT("%d,%f,%d,%c,%s,%s"), myInt, myFloat, myBool, myChar, *myString, *myVector.ToString());

UPROPERTY宏

	//在哪些地方可见
UPROPERTY(VisibleAnywhere)
	int32 Int32_VisibleAnywhere;
UPROPERTY(VisibleDefaultsOnly)
	int32 Int32_VisibleDefaultsOnly;
UPROPERTY(VisibleInstanceOnly)
	int32 Int32_VisibleInstanceOnly;

	//在哪些地方可编辑
UPROPERTY(EditDefaultsOnly)
	FVector V3_EditDefaultsOnly;
UPROPERTY(EditAnywhere)
	FVector V3_EditAnywhere;
UPROPERTY(EditInstanceOnly)
	FVector V3_EditInstanceOnly;

	//在蓝图中可get和getset
UPROPERTY(EditAnywhere,BlueprintReadOnly)
	int32 int32_EditAnywhere_BlueprintReadOnly;
UPROPERTY(EditAnywhere,BlueprintReadWrite)
	int32 int32_EditAnywhere_BlueprintReadWrite;

	//目录
UPROPERTY(EditAnywhere,BlueprintReadWrite,Category="MyIntValue")
	int32 valueB1;
	//子目录
UPROPERTY(EditAnywhere,BlueprintReadWrite,Category="MyIntValue|MySubIntValue")
	int32 ValueB2;

	//起别名
UPROPERTY(EditAnywhere,BlueprintReadWrite,meta=(DisplayName="displayName"))
	int32 ValueB3;

	//条件控制编辑,上者影响下者是否能修改
UPROPERTY(EditAnywhere,BlueprintReadWrite,meta=(DisplayName="Controller"))
	bool isController;
UPROPERTY(EditAnywhere,BlueprintReadOnly,meta=(EditCondition="isController"))
	float ValueB4;

	//变量提示
UPROPERTY(EditAnywhere,BlueprintReadOnly,meta=(ToolTip="isControllerTrue"))
bool isTrue;

函数

//暴露在蓝图,可调用
UFUNCTION(BlueprintCallable,category="MyFunction")
void PrintF1();
//纯虚函数,仅返回值
UFUNCTION(BlueprintCallable,BlueprintPure,category="MyFunction")
bool PrintF2();

//不能定义(CPP不实现),只能重载
//无返回值的是事件、有返回值的是函数
UFUNCTION(BlueprintImplementableEvent)
void Test1();
UFUNCTION(BlueprintImplementableEvent)
int Test2();
UFUNCTION(BlueprintImplementableEvent)
void Test3(const FString &MyString);
UFUNCTION(BlueprintImplementableEvent)
int Test4(const FString& MyString);

//在C++中声明蓝图重载或不重载
//有连线-用连线的方法(重载),否则用CPP写好的方法(不重载)
UFUNCTION(BlueprintNativeEvent)
	void TestA();
UFUNCTION(BlueprintNativeEvent)
	int TestB();
UFUNCTION(BlueprintNativeEvent)
	void TestC(const FString& MyString);
UFUNCTION(BlueprintNativeEvent)
int TestD(const FString& MyString);

//起别名
UFUNCTION(BlueprintCallable,Category="MyFunction",meta=(Display="MyPrintTest"))
	void Printtest();

重载不重载那个要加_implementation


void AMyPawn::TestA_Implementation()
{
}

void AMyPawn::TestB_Implementation()
{
}

void AMyPawn::TestC_Implementation(const FString& MyString)
{
	UE_LOG(LogTemp, Display, TEXT("%s"), *MyString);
}

void AMyPawn::TestD_Implementation(const FString& MyString)
{
}

枚举 

位置同UCLASS

法1

UENUM(BlueprintType)
namespace MyEnumType 
{
	enum MyCustomEnum 
	{
		type1,
		type2,
		type3
	};
}
	UPROPERTY(EditAnywhere,BlueprintReadWrite,Category="MyEnum")
		TEnumAsByte MyCustomEnumInst;

 法2


UENUM(BlueprintType)
enum class MyCustomEnum2 :uint8
{
	a UMETA(DisplayName="type1"),
	b UMETA(DisplayName="type2"),
	c UMETA(DisplayName="type3")
};
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "MyCustomStruct")
		MyCustomEnum2 myCustomStruct;

  初学UE5 C++①_第7张图片

 结构体

//命名必须以F开头
USTRUCT(BlueprintType)//作为蓝图类型,可被蓝图调用
struct FMyStruct
{
	GENERATED_USTRUCT_BODY()
	UPROPERTY(EditAnywhere,BlueprintReadWrite,Category="MyTestStruct")
	int32 Health;
	UPROPERTY(EditAnywhere,BlueprintReadWrite,Category="MyTestStruct")
	FString MyName;
};
	//结构体
	UPROPERTY(EditAnywhere,BlueprintReadWrite,Category="MyCustomStruct")
		FMyStruct myCustomStruct;

 

其他

蓝图生成时暴露

	//蓝图生成时暴露
	UPROPERTY(EditAnywhere,BlueprintReadWrite,Category="MyExposeOnSpawn",meta=(ExposeOnSpawn="ExposeOnSpawnValue"))
		float Health;

你可能感兴趣的:(ue5)