UE4中中参数BlueprintReadWrite、EditInstanceOnly解析

  1. 承接上文,新建一个TestActor的蓝图类
    在这里插入图片描述
    在这里插入图片描述
  2. 在蓝图中找到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);

}


  1. 把创景的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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wb175208

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值