UE4 C++常用方法总结

1、常用宏

  UPROPERTY( )

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Zed|Components")
	UStaticMeshComponent* FinalLeftPlane;

 UFUNCTION()

BlueprintNativeEvent在ue4中是用来修饰UFUNCTION的,和BlueprintImplementableEvent有点类似,

BlueprintImplementableEvent用于实现C++调蓝图(声明在C++,实现在蓝图)

BlueprintNativeEvent除了实现C++调用蓝图外,同样会调用一个本地方法,本地方法为 声明的函数名+_Implementation

这样就可以实现一次调用,两个实现(c++实现,蓝图实现)

BlueprintNativeEvent还有一个比较有用的用法,是C++实现可以是virual,具体用法如下:

   //xx.h

	//C++实现,蓝图调用
	UFUNCTION(BlueprintCallable,Category="LowLevel")
    void FuncA(int a );

	//C++实现默认版本,蓝图可override实现
	UFUNCTION(BlueprintNativeEvent, Category="LowLevel")
	void FuncB();
	virtual void FuncB_Implementation(); //默认版本,这个要在C++中实现一下

	//C++不实现,蓝图实现.主要用于C++调用蓝图
	UFUNCTION(BlueprintImplementableEvent, Category="LowLevel")
	void Func();

引擎中很多地方就是用这种用法来实现派生类重写BlueprintNativeEvent函数的C++实现

2、程序中控制鼠标的显示

GetWorld()->GetFirstPlayerController()->bShowMouseCursor=true;

3、生成继承自Actor的对象

spawnActor = GetWorld()->SpawnActor<AActor1>(AActor1::StaticClass(),SpawnLocation);

4、初始化物体组件

//Component creation
RootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("RootComponent"));	
FinalLeftPlane = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("FinalLeftPlane"));

//Attachment
FinalLeftPlane->SetupAttachment(RootComponent );

// Add static mesh to planes
static ConstructorHelpers::FObjectFinder<UStaticMesh> PlaneMesh(TEXT("StaticMesh'/
Stereolabs/ZED/Shapes/SM_Plane_100x100.SM_Plane_100x100'"));
FinalLeftPlane->SetStaticMesh(PlaneMesh.Object);

//setup	
FinalLeftPlane->SetRelativeRotation(FRotator(0, 90, 90));
FinalLeftPlane->SetCollisionEnabled(ECollisionEnabled::NoCollision);
FinalLeftPlane->SetCastShadow(false);

5、获取当前关卡中的某一个Actor

(1)

#include "Kismet/GameplayStatics.h"
 
TArray<AActor*> Actors;
UGameplayStatics::GetAllActorsOfClass(GetWorld(), AActor::StaticClass(), Actors);
 
for (AActor* Actor : Actors)
{
		
 
}

(2)通过设置tag属性 

#include "Kismet/GameplayStatics.h"
TArray<AActor*> Actors;
UGameplayStatics::GetAllActorsWithTag(GetWorld(), TEXT("actorName"), Actors);
for (AActor* Actor: Actors)
{
 
	
}


 

6、定时器的基本使用方法

FTimerHandle Timer;
GetWorldTimerManager().SetTimer(Timer,this,&AUE4GameMode::DestroyActorFunction,10);

7、UE4 Log

UE_LOG(LogTemp,Warning,TEXT("Message %d"),1);

//Debug信息到屏幕上:
GEngine->AddOnScreenDebugMessage(-1, 1, FColor::Red,FString::Printf(TEXT("%s left me"), *(OtherActor->GetName())));

FString filePath;
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Green, FString::Printf(TEXT("DLL_Init")));

GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Green, FString::Printf(TEXT("%s"), *filePath));

 

8、委托

1、委托写法(无参):
DECLARE_DELEGATE(FStandardDelegateSignature)

定义成员:
FStandardDelegateSignature  MyStandardDelegate;

绑定:
MyStandardDelegate.BindUObject(this,&ADelegateListener::EnableLight);
简单的C++类对象函数绑定要用BindRaw
静态函数的绑定:BindStatic
执行:
MyStandardDelegate.ExecuteIfBound();

解除当前的绑定:
MyStandardDelegate.Unbind();

2、委托写法(含参):
DECLARE_DELEGATE_OneParam(FParamDelegateSignature,FLinearColor)
DECLARE_EVENT( OwningType, EventName )
创建一个事件。
DECLARE_EVENT_OneParam( OwningType, EventName, Param1Type )
创建带一个参数的事件。
DECLARE_EVENT_TwoParams( OwningType, EventName, Param1Type, Param2Type )
创建带两个参数的事件。
DECLARE_EVENT_<Num>Params( OwningType, EventName, Param1Type, Param2Type, ...)
创建带 N 个参数的事件。

添加成员:
FParamDelegateSignature MyParamDelegate;

绑定:
同无参绑定类似

执行:
MyParamDelegate.ExecuteIfBound(FLinearColor)

3,多播委托的写法:
DELCARE_MULTICAST_DELEGATE(FMulticastDelegateSignature)

添加成员到指定的类中:
FMulticastDelegateSignature FMultiDelegate;

绑定类似:
FMultiDelegate->AddUObject(this,AMulticastDelegateListener::ToggleLight);

执行:
FMultiDelegate->Broadcast();

解除绑定:
FMultiDelegate->Remove(AMulticastDelegateListener::ToggleLight);
DECLARE_MULTICAST_DELEGATE[_Const, _RetVal, etc.]( DelegateName )
创建一个多播的多播代理。
DECLARE_DYNAMIC_MULTICAST_DELEGATE[_Const, _RetVal, etc.]( DelegateName )
创建一个动态的多播代理。
DECLARE_DYNAMIC_MULTICAST_DELEGATE[_Const, _RetVal, etc.]( DelegateName )
创建一个封装的动态多播代理。

4、自定义事件:
DECLARE_EVENT(AMyTriggerVolume,FPlayerEntered)

定义:
FPlayerEntered OnPlayerEntered;

执行:
OnPlayEntered.Broadcast();
绑定:
OnPlayEntered.AddUObject(this,&ATriggerVolEventListener::OnTriggerEvent);    
 

  删除一个非使用的UObject:

   objectInstance->ConditionalBeginDestroy();

   强制GC进行资源回收的方式:

   GetWorld()->ForceGarbageCollection(true)

9、实例化游戏对象

1.Components:

     UMeshComponent* meshComponent = CreateDefaultSubobject<UMeshComponent>("MeshComponent");(组件的创建一般在构造中完成)

 

2.UObjects:

     UElementStructure* structure = NewObject<UElementStructure>(GetTransientPackage(), UElementStructure::StaticClass());

 

3.AActor:

       GetWorld()->SpawnActor<AActor>(AActor::StaticClass())

 

4.Blueprint:

   UClass* elementClass = LoadClass<ABlueprintClass>(NULL, TEXT("Blueprint'/Game/BluePrint/BP_Name.BP_Name_C'"));

  if (elementClass == nullptr) {

                   return;

      }

else

   {

       GetWorld()->SpawnActor<ABlueprintClass>(elementClass);

   }

  • 5
    点赞
  • 39
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值