一些常用代码示例

疯狂的凑脚丫 / 2023-04-28 / 原文

反正记不住

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

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

 

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.定时器

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++中引用蓝图资源

这是第三人称模板里的构造函数之一
AHaingGamgGameMode::AHaingGamgGameMode()
{
       // set default pawn class to our Blueprinted character
       static          ConstructorHelpers::FClassFinder<APawn>  PlayerPawnBPClass(TEXT("/Game/ThirdPersonCPP/Blueprints/ThirdPersonCharacter"));
       if (PlayerPawnBPClass.Class != NULL)
       {
              DefaultPawnClass = PlayerPawnBPClass.Class;
       }
}

 

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);//文件路径