- 承接上文,新建一个TestActor的蓝图类
- 在蓝图中找到InitLacation
通过以上可以看到,在蓝图中可以找到变量:InitLocation,说明只要是变量添加BlueprintReadWrite这个字段,就可以在蓝图中使用。
但是在细节面板中并没有找到类别:MyVars
因为EditInstanceOnly这个字段只有类在创建对象的时候使用,在类中并不起作用。
3. 在这个类中添加新的变量,用来记录Actor刚刚被放在场景中的位置
UPROPERTY(VisibleInstanceOnly, BlueprintReadWrite, Category = "MyVars")
FVector placeInWordLoaction;
并且在BeginPlay中获取实体变量位置
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "TestActor.generated.h"
UCLASS()
class FIRSTPROJECT_API ATestActor : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
ATestActor();
UPROPERTY(VisibleAnywhere, Category = "MyMess")
UStaticMeshComponent* mess = nullptr;
//初始化Actor的位置
UPROPERTY(EditInstanceOnly, BlueprintReadWrite, Category = "MyVars")
FVector initLocation = FVector(0.0, 0.0, 0.0);
//获取Actor被拖动进场景中的位置
UPROPERTY(VisibleInstanceOnly, BlueprintReadWrite, Category = "MyVars")
FVector placeInWordLoaction;
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
};
// Fill out your copyright notice in the Description page of Project Settings.
#include "TestActor.h"
// Sets default values
ATestActor::ATestActor()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = false;
mess = CreateDefaultSubobject<UStaticMeshComponent>("TestMess");
}
// Called when the game starts or when spawned
void ATestActor::BeginPlay()
{
Super::BeginPlay();
placeInWordLoaction = GetActorLocation();
SetActorLocation(initLocation);
}
// Called every frame
void ATestActor::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
- 把创景的TestActor_BP拖动到场景中
点击【运行】,在细节面板中输入“MyVars”,可以看着这个变量,一个是我们设置的位置initLocation,一个Actor初始化场景中的位置placeInWordLoaction
可以看到:我们把TestActor拖动到场景中的位置是(-90.0,120.0,110.212)但是开始运行游戏的时候,我们的设置的位置是(0.0,0.0,500.0)
4.现在在类中重新添加一个bool 类型的变量
//判断是否允许别初始化位置
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category="MyVars")
bool bInitLocation = false;
在BeginPlay中调用
void ATestActor::BeginPlay()
{
Super::BeginPlay();
placeInWordLoaction = GetActorLocation();
if (bInitLocation){
SetActorLocation(initLocation);
}
}
备注:EditDefaultsOnly:在蓝图类中中默认编辑
当不勾选的时候,运行场景,Actor的位置都不会发生变化;
勾选以后,场景中的位置都会发生变化。
aaa