一些常用代码示例

反正记不住

1.获取世界UWorld

1 UWorld* World = GEngine->GetWorldFromContextObjectChecked(GetOuter()); //GetOuter()返回该对象所在的UObject

 

2.判断是在游戏模式下运行(Game、PIE)

if (GWorld->IsGameWorld())
{
    // 在游戏模式下运行
}
************************************************************
if (GEngine->GetWorld()->IsPlayInEditor())
{
    // 在PIE模式下运行
}
else
{
    // 在Game模式下运行
}

 

3.类型转换Cast

ASGameMode* MyGameMode = Cast<ASGameMode>(GetWorld()->GetAuthGameMode());  //GetWorld()->GetAuthGameMode();获取当前游戏模式
return (T*)Ret; //把类 Ret 转换成 T

 UE5增加了CastCheck;用于在编辑器阶段提前发现错误

ASGameMode* MyGameMode = CastCheck<ASGameMode>(GetWorld()->GetAuthGameMode());

 

 

 

4.获取游戏模式GameMode

1 /**在 World.h 中:
2         * Returns the current Game Mode instance, which is always valid during  gameplay on the server.
3         * This will only return a valid pointer on the server. Will always return  null on a client.
4         */
5        AGameModeBase* GetAuthGameMode() const { return AuthorityGameMode; }
6 
7 应用:
8     ASGameMode* MyGameMode = Cast<ASGameMode>(GetWorld()->GetAuthGameMode());

 

5.定时器

//用定时器的前提是World已经存在了。
FTimerHandle Handle; GWorld
->GetTimerManager().SetTimer(Handle, this,&UMaJaComponent::ShakeLoop,0.1f,true); Lambda表达式写法 GWorld->GetTimerManager().SetTimer(Handle, [=]() {...代码区},1.0f,false);

 

6.使用延时

1 FPlatformProcess::Sleep(0.03)   //include "Runnable.h"

 

7.c++中引用蓝图资源

1.通过ConstructorHelpers来加载;这种加载方式只能在构造函数内使用
例子1:加载蓝图类
static ConstructorHelpers::FClassFinder<AActor> BP_Class(TEXT("/Game/BluePrint/TestObj"));  
if(BP_Class.Class != NULL)  
{  
    GetWorld()->SpawnActor(BP_Class.Class);  
//这里的FClassFinder的成员变量类型是RSubClassof<T>
 
例子2:加载模型资源
HandsMesh = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("HandsMesh"));
static ConstructorHelpers::FObjectFinder<USkeletalMesh>
  HandMeshObj(TEXT("/Script/Engine.SkeletalMesh'/Game/Meshes/SKM_MannyXR_left.SKM_MannyXR_left'"));
if (HandMeshObj.Object)
{
  HandsMesh->SetSkeletalMesh(HandMeshObj.Object);
}
//两个例子一个是加载Class,一个是加载Object的。

 

8.获取游戏屏幕大小

 1 #includ "GEngine.h"
 2 
 3 FVector2D SSlAiMeunHUDWidget::GetViewportSize() const
 4 {
 5        FVector2D Result(1920,1080);
 6        if (GEngine && GEngine->GameViewport)
 7        {
 8            GEngine->GameViewport->GetViewportSize(Result); //参数是引用
 9        }
10        return Result;
11 }

 

9.const FString常量字符串

1 const static FString PassCode;
2 第一:static修饰的 需要静态的初始化值,由于是const修饰的常量,初始化也要用const修饰
3 const FString ULockCodeSG::PassCode = FApp::GetProjectName();
4 第二:const 修饰的参数不能参与反射
5 会提示 Const properties are not supported.不支持Const属性。

 

10.获取项目的项目名称

1 FApp::GetProjectName();
2 蓝图不知道,C++可以这样获取

 

11.UE使用windows.h头文件加入方法

1 #include "Windows/AllowWindowsPlatformTypes.h"
2 #include "Windows/PreWindowsApi.h"  // 好像UE5不需要这个了
3 //此处写要调用的windows的头文件
4 #include "Windows/PostWindowsApi.h"   //好像UE5不需要这个了
5 #include "Windows/HideWindowsPlatformTypes.h"

 

12.C++内添加组件

1 .h
2 UPROPERTY()   //想在蓝图获取就要反射,不然无法从组件窗口拖到蓝图窗口
3 UAudioComponent* AudioComponent;
4 static FName AudioComponentName;
5 
6 .cpp
7 FName AActorT::AudioComponentName(TEXT("AudioComponent"));
8 AudioComponent = CreateDefaultSubobject<UAudioComponent>(AudioComponentName);

 

13.设置运行窗口大小

1 //窗口尺寸
2 r.setres 1920x1080w
3 //分辨率
4 r.setres 1920x1080f
调用 UKismetSystemLibrary::ExecuteConsoleCommand()

 

14.以UObject作为函数库使用


思维误区,每次写函数库都继承的是“蓝图函数库类”(不记得叫啥了)
其实如果把行常用函数写到Actor内,这个Actor不就是一个函数库了吗?
同理UObject比Actor更适合这样子。那么只需要在这个UObject类中写一个static方法创建这个类即可。
1 class  XXX_API UXXXLibrary : public UObject
2 {
3 public:
4     UFUNCTION(BlueprintCallable)
5     static UXXXLibrary* CreateXXXLibrary()
{
return NewUObject<UXXXLibrary>();
}
6 }

 

15.引擎版本号获取( 条件编译技术)

1 ENGINE_MAJOR_VERSION  引擎主版本 4.26中的4ENGINE_MINOR_VERSION  引擎次版本 4.26中的26
2 
3 #if ENGINE_MAJOR_VERSION == 4 && ENGINE_MINOR_VERSION <= 25
4       //如果主版本为4,次版本<=25 即4.25及以前的版本
5 #else
6        //4.25之后的版本
7 #endif

 

16.读取本地文件

1 本地文件都是以二进制数据存放在本地的,
2 FString FilePath = FPaths::ProjectSavedDir() + TEXT("image.jpg");
3 TArray<unit8> Content;
4 FFileHelper::LoadFileToArray(Content, *FilePath)
5 主要方法就是FFileHelper::LoadFileToArray

 

17.删除文件

IFileManager::Get().Delete(*FileName);//文件路径

 

18.函数多引脚输出

 1 1.需要定义一个枚举来描述输出引脚,
 2 2.反射宏里用标记指令指定枚举作为输出引脚;“ExpandEnumAsExecs”
 3 3.用枚举引用作为参数
 4 4.返回枚举值
 5 UENUM(BlueprintType)
 6 enum class EBlueprintExecNodePin : uint8
 7 {
 8        Success,
 9        Fail
10 };
11 /******/
12 UFUNCTION(BlueprintCallable, meta = (ExpandEnumAsExecs = "Result"))
13 static void MultiExecNode(EBlueprintExecNodePin& Result);
14 /******/
15 void UFHQBlueprintFunctionLibrary::MultiExecNode(EBlueprintExecNodePin & Result)
16 {
17    Result = EBlueprintExecNodePin::Success;
18 }

 

 19.UPARAM宏,参数的&不作为蓝图节点输出值

 1 UPARAM宏
 2 
 3 UFUNCTION(BlueprintCallable, meta=(...)
 4 static FTimerHandle K2_InvalidateTimerHandle(UPARAM(ref) FTimerHandle& Handle); 8 
 9 这个宏用在参数列表当中
10 用法:
11 1.UPARAM(ref)——被‘&’修饰的参数会作为Output输出,UPARAM(ref)可以逆反这个行为。

 

 

 

 

 

 
posted @ 2023-04-28 09:52  疯狂的凑脚丫  阅读(498)  评论(0)    收藏  举报