跳转到主要内容

UE4 C++ 常用节点

demi 提交于

<a href="https://blog.csdn.net/qq_43021038/article/details/126181734"><font color="#9a9a9a">本文来源:CSDN - 飞起的猪</font></a>

<hr>

<font size="3" style="line-height: 45px;" color="#c200ff"><strong>一、打开关卡</strong></font>

UGameplayStatics::OpenLevel(GetWorld(), TEXT("Map1"));

<font size="3" style="line-height: 45px;" color="#c200ff"><strong>二、退出关卡</strong></font>

//退出关卡
UGameplayStatics::GetPlayerController(GWorld, 0)->ConsoleCommand("quit");

<font size="3" style="line-height: 45px;" color="#c200ff"><strong>三、TActorIterator 遍历</strong></font>

<pre>for (TActorIterator&lt;AStaticMeshActor&gt; ActorIter(GetWorld()); ActorIter; ++ActorIter)
{
UKismetSystemLibrary::PrintString(GetWorld(), FString::Printf(TEXT("%s"), *ActorIter-&gt;GetName()));
}</pre>

UKismetSystemLibrary::GetAllActorsOfClass

<pre>TArray&lt;AActor*&gt; OutActors;
UGameplayStatics::GetAllActorsOfClass(GetWorld(), AStaticMeshActor::StaticClass(), OutActors);
for (AActor* actor : OutActors)
{
UKismetSystemLibrary::PrintString(GetWorld(), actor-&gt;GetName());
}</pre>

<font size="3" style="line-height: 45px;" color="#c200ff"><strong>四、蓝图指认类</strong></font>

UPROPERTY(VisibleAnywhere,Category = UI)

TSubclassOf<AActor>Actor_Instance;

<font size="3" style="line-height: 45px;" color="#c200ff"><strong>五、加载蓝图资源</strong></font>

<strong>静态加载资源</strong>
<pre>//静态加载模型资源
MeshObj = CreateDefaultSubobject&lt;UStaticMeshComponent&gt;(TEXT("Mesh1"));
static ConstructorHelpers::FObjectFinder&lt;UStaticMesh&gt; TmpMesh(TEXT("StaticMesh'/Game/StarterContent/Shapes/Shape_NarrowCapsule.Shape_NarrowCapsule'"));
MeshObj->SetStaticMesh(TmpMesh.Object);

静态加载类
static ConstructorHelpers::FClassFinder&lt;AActor&gt;MyActor2(TEXT("BBlueprint'/Game/Blueprints/BP_LoadTest.BP_LoadTest_C'"));
MyActor = MyActor2.Class;</pre>

<strong>动态加载资源</strong>
<pre>//动态加载资源类
UMaterial *mt = LoadObject&lt;UMaterial&gt;(nullptr, TEXT("/Game/Map/Materials/grass.grass"));
UMaterial *mt = Cast&lt;UMaterial&gt;(StaticLoadObject(UMaterial::StaticClass(), nullptr, TEXT("/Game/Map/Materials/grass.grass")));

//动态加载类
Class = LoadClass&lt;AActor&gt;(nullptr,TEXT("Blueprint'/Game/BluePrint/BP_Test.BP_Test_C'"))
Class = Cast&lt;UClass&gt;(StaticLoadObject(UClass::StaticClass(), nullptr, TEXT("Blueprint'/Game/BluePrint/BP_Test.BP_Test_C'")));</pre>

<font size="3" style="line-height: 45px;" color="#c200ff"><strong>六、获取GameMode</strong></font>
<pre>AXuHuanVSGameModeBase *MyGameMode = Cast&lt;AXuHuanVSGameModeBase&gt;(GetWorld()-&gt;GetAuthGameMode());</pre>

<font size="3" style="line-height: 45px;" color="#c200ff"><strong>七、获取Pawn</strong></font>
<pre>AMyPawn *MyPawn = Cast&lt;AMyPawn&gt;(GetWorld()-&gt;GetFirstPlayerController()-&gt;GetPawn());</pre>

<font size="3" style="line-height: 45px;" color="#c200ff"><strong>八、获取GameState</strong></font>
<pre>AGameState *MyState = Cast&lt;AGameState&gt;(GetWorld()-&gt;GetGameState());</pre>

<font size="3" style="line-height: 45px;" color="#c200ff"><strong>九、获取PlayerController</strong></font>
<pre>AMyPlayerController *MyPlayerController = Cast&lt;AMyPlayerController&gt;(GetWorld()-&gt;GetFirstPlayerController());</pre>

<font size="3" style="line-height: 45px;" color="#c200ff"><strong>十、设置UE4世界设置默认的类</strong></font>
<pre></pre>

<font size="3" style="line-height: 45px;" color="#c200ff"><strong>十二、RInterp To</strong></font>
<center><img width="600" src="https://cdn.eetrend.com/files/2022-10/%E5%8D%9A%E5%AE%A2/100564823-2746…; alt="UE4 C++ 常用节点"></center><br>

<pre>(FMath::RInterpTo(GetControlRotation(), DesireRotation, GetWorld()-&gt;GetRealTimeSeconds(), 10);</pre>

<font size="3" style="line-height: 45px;" color="#c200ff"><strong>十三、枚举</strong></font>
<pre>UENUM()
namespace MyEnum
{
enum MyTpe
{
Type1,
Type2,
Type3,
}
}

//声明变量
UPROPERTY(EditDefaultsOnly, Category = KWorld)
TEnumAsByte&lt;MyCharacterType::MyType>CustomA;</pre>

<font size="3" style="line-height: 45px;" color="#c200ff"><strong>十四、结构体</strong></font>
<pre>USTRUCT()
struct FGameCharacterDataAttribute
{
GENERATED_USTRUCT_BODY()
//角色名称
UPROPERTY()
FName Character;
//角色ID
UPROPERTY()
int32 CharacterID;
public:
FGameCharacterDataAttribute();
}

//声明变量

UPROPERTY()
FGameCharacterDataAttribute CharacterDataBase;</pre>

<hr>
版权声明:本文为CSDN博主「飞起的猪」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_43021038/article/details/126181734
<br>