本文是老王學習UE4的筆記,如有錯誤敬請指正
文章目錄
概述
AActor
繼承於UObject
是所有可以放置到場景中的對象的基類。Gameplay框架的核心基類之一。
UCLASS聲明
AActor
的UCLASS
已經聲明瞭BlueprintType, Blueprintable
這兩個屬性都可以傳遞給子類。
UCLASS(BlueprintType, Blueprintable, config=Engine, meta=(ShortTooltip="An Actor is an object that can be placed or spawned in the world."))
class ENGINE_API AActor : public UObject
{
GENERATED_BODY()
...
}
生命週期
protected:
virtual void BeginPlay() override;
virtual void EndPlay(const EEndPlayReason::Type EndPlayReason) override;
public:
virtual void Tick(float DeltaTime) override;
Tick
相當於Unity中的Update
,UE4中的Tick
可以通過PrimaryActorTick.bCanEverTick
來開關。
virtual void Tick( float DeltaSeconds );
FORCEINLINE bool CanEverTick() const { return PrimaryActorTick.bCanEverTick; }
UPROPERTY(EditDefaultsOnly, Category=Tick)
struct FActorTickFunction PrimaryActorTick;
獲取ULevel和UGameInstance
UWorld
在UObject
中就可以獲取
- 獲取所屬
ULevel
ULevel* GetLevel() const;
- 獲取
UGameInstance
class UGameInstance* GetGameInstance() const;
Actor的名字
//定義在基類UObjectBaseUtility中
public:
FORCEINLINE FString GetName() const
{
return GetFName().ToString();
}
和組件相關的一些內容
AActor
有3個管理組件的容器
OwnedComponents
:集合InstanceComponents
:數組BlueprintCreatedComponents
數組
另外還有一個重要的RootComponent
組件
- 它是所有其它組件的根組件
- 它一定是
USceneComponent
類型即帶有變換的組件
private:
TSet<UActorComponent*> OwnedComponents;
UPROPERTY(Instanced)
TArray<UActorComponent*> InstanceComponents;
public:
UPROPERTY(TextExportTransient, NonTransactional)
TArray<UActorComponent*> BlueprintCreatedComponents;
protected:
UPROPERTY(BlueprintGetter=K2_GetRootComponent, Category="Utilities|Transformation")
USceneComponent* RootComponent;
public:
FORCEINLINE USceneComponent* GetRootComponent() const { return RootComponent; }
和場景中的AActor樹相關的一些內容
private:
UPROPERTY(ReplicatedUsing=OnRep_Owner)
AActor* Owner;
public:
UPROPERTY(Transient)
TArray<AActor*> Children;
UFUNCTION(BlueprintCallable, Category=Actor)
AActor* GetOwner() const;
UFUNCTION(BlueprintCallable, Category=Actor)
virtual void SetOwner( AActor* NewOwner );
和藍圖中的AActor樹相關的一些內容
UFUNCTION(BlueprintCallable, Category="Actor")
AActor* GetParentActor() const;
UFUNCTION(BlueprintCallable, Category="Actor")
UChildActorComponent* GetParentComponent() const;
一些奇葩的地方
AActor
應該是抽象層次很高的類,裏面竟然有關於傷害的定義- 竟然還引用了子類
APawn
和ACharacter
UInputComponent
也應該是在APawn
層級纔有交集
/**
* Whether this actor can take damage. Must be true for damage events (e.g. ReceiveDamage()) to be called.
* @see https://www.unrealengine.com/blog/damage-in-ue4
* @see TakeDamage(), ReceiveDamage()
*/
UPROPERTY(EditAnywhere, BlueprintReadWrite, SaveGame, Replicated, Category=Actor, meta=(AllowPrivateAccess="true"))
uint8 bCanBeDamaged:1;
private:
/** Pawn responsible for damage and other gameplay events caused by this actor. */
UPROPERTY(BlueprintReadWrite, ReplicatedUsing=OnRep_Instigator, meta=(ExposeOnSpawn=true, AllowPrivateAccess=true), Category=Actor)
class APawn* Instigator;
UPROPERTY(DuplicateTransient)
class UInputComponent* InputComponent;
/**
* Return true if the given Pawn can be "based" on this actor (ie walk on it).
* @param Pawn - The pawn that wants to be based on this actor
*/
virtual bool CanBeBaseForCharacter(class APawn* Pawn) const;
/**
* Apply damage to this actor.
* @see https://www.unrealengine.com/blog/damage-in-ue4
* @param DamageAmount How much damage to apply
* @param DamageEvent Data package that fully describes the damage received.
* @param EventInstigator The Controller responsible for the damage.
* @param DamageCauser The Actor that directly caused the damage (e.g. the projectile that exploded, the rock that landed on you)
* @return The amount of damage actually applied.
*/
virtual float TakeDamage(float DamageAmount, struct FDamageEvent const& DamageEvent, class AController* EventInstigator, AActor* DamageCauser);
protected:
virtual float InternalTakeRadialDamage(float Damage, struct FRadialDamageEvent const& RadialDamageEvent, class AController* EventInstigator, AActor* DamageCauser);
virtual float InternalTakePointDamage(float Damage, struct FPointDamageEvent const& PointDamageEvent, class AController* EventInstigator, AActor* DamageCauser);