初学UE5 C++①

游戏类

1.创建所需项的类

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


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


FName、FString、FText类型相互转化

FName用FName

FString用ToString()

FText用FText::FromString、FromName

cpp 复制代码
//转化
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);

数组容器

cpp 复制代码
	TArray<int>arr;
cpp 复制代码
	//增
	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);
cpp 复制代码
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));
	}
}

键值容器

cpp 复制代码
TMap<int, int>map;
cpp 复制代码
	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);//值找键

	//获取查找
	TArray<int>arrkey;
	TArray<int>arrayVal;
	map.GenerateKeyArray(arrkey);
	map.GenerateValueArray(arrayVal);
cpp 复制代码
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);
	}
}

集合容器

cpp 复制代码
TSet<FString>FruitSet;
cpp 复制代码
//增
FruitSet.Add(TEXT("Apple"));
FruitSet.Add(TEXT("Orange"));
FruitSet.Add(TEXT("Banana"));
FruitSet.Emplace("Purple");//比add好,在插入集合时,避免创建临时文件
PrintFruit();
TSet<FString> 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<FString> FruitArr = FruitSet.Array();

TSet<FString>TS2 = { TEXT("a"),TEXT("aa") ,TEXT("aaa") ,TEXT("aaaa") };
//长度排序
TS2.Sort([](FString A, FString B)
	{return A.Len() > B.Len(); });
cpp 复制代码
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);
	}
}
cpp 复制代码
TSet<FString>MySet;
MySet.Add(TEXT("abc"));
FSetElementId index = MySet.Add(TEXT("bbc"));
MySet[index] = TEXT("abd");

//预留内存
TSet<FString> 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();//删除空白元素

基本类型打印

cpp 复制代码
	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宏

cpp 复制代码
	//在哪些地方可见
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;

函数

cpp 复制代码
//暴露在蓝图,可调用
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

cpp 复制代码
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

cpp 复制代码
UENUM(BlueprintType)
namespace MyEnumType 
{
	enum MyCustomEnum 
	{
		type1,
		type2,
		type3
	};
}
cpp 复制代码
	UPROPERTY(EditAnywhere,BlueprintReadWrite,Category="MyEnum")
		TEnumAsByte<MyEnumType::MyCustomEnum> MyCustomEnumInst;

法2

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

结构体

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

其他

蓝图生成时暴露

cpp 复制代码
	//蓝图生成时暴露
	UPROPERTY(EditAnywhere,BlueprintReadWrite,Category="MyExposeOnSpawn",meta=(ExposeOnSpawn="ExposeOnSpawnValue"))
		float Health;
相关推荐
我救我自己12 分钟前
UE5运行时创建slate窗口
java·服务器·ue5
Deveuper12 分钟前
UE5 设置Sequence播完后返回起始位置
ue5
心怀梦想的咸鱼14 分钟前
Ue5 umg学习(三)文本控件
学习·ue5
XR-AI-JK15 分钟前
UE5遇到问题记录
ue5
心怀梦想的咸鱼15 分钟前
Ue5 umg学习(二)图像控件,锚点
学习·ue5
UTwelve19 分钟前
【UE5】在材质Custom写函数的方法
ue5·材质
流行易逝19 分钟前
18.UE5怪物视野、AI感知、攻击范围、散弹技能
ue5
心怀梦想的咸鱼3 小时前
Ue5 umg学习(一)
学习·ue5
暖和_白开水12 小时前
材质(三)——材质参数集和材质函数
ue5·材质
心怀梦想的咸鱼14 小时前
ue5 蓝图学习(一)结构体的使用
学习·ue5