ue4标签测试与总结(UPROPERTY)

学习UE4框架中的标签,本篇是总结成员变量标签UPROPERTY。

引擎版本:4.12.5

前期准备:

1.新建项目,C++空模板,新建C++类,继承AActor,名称MyActor。

使用TestActor变量进行测试。

// MyActor.h

#pragma once

#include "GameFramework/Actor.h"
#include "MyActor.generated.h"

UCLASS()
class MYSTEAMONLINEC_API AMyActor : public AActor
{
    GENERATED_BODY()

public:

    AActor* TestActor;

public:
    // Sets default values for this actor's properties
    AMyActor();

    // Called when the game starts or when spawned
    virtual void BeginPlay() override;

    // Called every frame
    virtual void Tick( float DeltaSeconds ) override;

};
// MyActor.cpp

#include "MySteamOnlineC.h"
#include "MyActor.h"

// Sets default values
AMyActor::AMyActor()
{
     // Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
    PrimaryActorTick.bCanEverTick = true;

}

// Called when the game starts or when spawned
void AMyActor::BeginPlay()
{
    Super::BeginPlay();

}

// Called every frame
void AMyActor::Tick( float DeltaTime )
{
    Super::Tick( DeltaTime );

}

2.在ue4编辑器中新建蓝图BP_MyActor,继承MyActor。

ue4标签测试与总结(UPROPERTY)

UPROPERTY:

EditAnywhere

Indicates that this property can be edited by property windows, on archetypes and instances.(指出该属性可以在属性窗口,蓝图类编辑器,实例中进行修改)

UPROPERTY(EditAnywhere)
AActor* TestActor;

level中可以修改

ue4标签测试与总结(UPROPERTY)

原型中的属性窗口可以修改

ue4标签测试与总结(UPROPERTY)

EditDefaultsOnly

Indicates that this property can be edited by property windows, but only on archetypes. This operator is incompatible with the Visible* specifiers. (指出该变量只能出现在原型的属性窗口。这个操作与其他可视特性相冲突)

//与EditAnywhere冲突
UPROPERTY(EditDefaultsOnly) AActor* TestActor;

原型中的属性窗口可以修改

ue4标签测试与总结(UPROPERTY)

EditFixedSize

Only useful for dynamic arrays. This will prevent the user from changing the length of an array via the Unreal Editor property window.(只有在动态数组中有用,该标签将防止用户通过虚幻编辑器属性窗口改变数组长度)

普通数组

UPROPERTY(EditAnywhere)
TArray<int32> TestArray;

ue4标签测试与总结(UPROPERTY)

增加“EditFixedSize”标签后

UPROPERTY(EditAnywhere,EditFixedSize)
TArray<int32> TestArray;

ue4标签测试与总结(UPROPERTY)

EditInline

这个标签在4.12.5中已经没有了

EditInstanceOnly

Indicates that this property can be edited by property windows, but only on instances, not on archetypes. This operator is incompatible with the Visible* specifiers.(只能在场景中的实例的属性窗口修改,该操作与其他可视标签冲突)

UPROPERTY(EditInstanceOnly)
AActor* TestActor;

在场景中的实例属性窗口,可以做修改

ue4标签测试与总结(UPROPERTY)

在原型中已经没有该变量了

ue4标签测试与总结(UPROPERTY)

VisibleAnywhere

Indicates that this property is visible in property windows, but cannot be edited at all. This operator is incompatible with the Edit* specifiers.(可以在原型与场景中的实例的属性窗口中看到,但是不能修改,与其他修改类标签冲突)

UPROPERTY(EditInstanceOnly)
AActor* TestActor;

ue4标签测试与总结(UPROPERTY)

VisibleDefaultsOnly

Indicates that this property is only visible in property windows for archetypes, and cannot be edited. This operator is incompatible with the Edit* specifiers.(只能在原型的属性窗口中看到,但是不能修改,与其他修改类标签冲突)

UPROPERTY(VisibleDefaultsOnly)
AActor* TestActor;

在场景中的实例看不到属性了,在原型中可以看到(就不截图了)

ue4标签测试与总结(UPROPERTY)

VisibleInstanceOnly

Indicates that this property is only visible in property windows for instances, not for archetypes, and cannot be edited. This operator is incompatible with the Edit* specifiers.(只能在场景中的实例的属性窗口中看到,不能在原型中看到,不能修改,与其他修改类标签冲突)

UPROPERTY(VisibleInstanceOnly)
AActor* TestActor;

在原型的属性窗口已经看不到啦

ue4标签测试与总结(UPROPERTY)

场景实例可以看到

ue4标签测试与总结(UPROPERTY)

AdvancedDisplay

Properties are in the advanced dropdown in a Details panel.(属性在高级下拉列表中显示)

UPROPERTY(EditAnywhere,AdvancedDisplay)
AActor* TestActor;

用三角箭头进行收缩和展开

ue4标签测试与总结(UPROPERTY)

AssetRegistrySearchable

The AssetRegistrySearchable keyword indicates that this property and its value will be automatically added to the asset registry for any asset class instances containing this as a member variable. It is not legal to use on struct properties or parameters.

(指出这个属性和属性的值,为了包含到所有资源类型实例中,将自动的作为成员变量,添加到资源注册列表。在结构体属性或者参数中使用是非法的。)

不懂啊。。。

其他:

ObjectBase.h中,namespace UP,是标签的源码。

UFUNCTION

Reliable

The function is replicated over the network, and is guaranteed to arrive regardless of bandwidth or network errors. Only valid when used in conjunction with Client or Server.

UFUNCTION(Reliable, Client, Category = "Default")
void RespawnCharacter();

Server

The function is only executed on the server. Provide a body named [FunctionName]_Implementation instead of [FunctionName]; the autogenerated code will include a thunk that calls the implementation method when necessary.

ps:在参数传递时,不能使用引用,

  必须使用void,不能有返回值

//.h file
UFUNCTION(Reliable,Server, WithValidation, Category = "Default")
void RespawnCharacterOnServer();

//.cpp file
void ABasePlayerController::RespawnCharacterOnServer_Implementation()
{

}

bool ABasePlayerController::RespawnCharacterOnServer_Validate()
{
    return true;
}

Client

The function is only executed on the client that owns the Object the function belongs to. Provide a body named [FunctionName]_Implementation instead of [FunctionName]; the autogenerated code will include a thunk that calls the implementation method when necessary.

//.h file
UFUNCTION(Reliable, Client, Category = "Default")
void RespawnCharacter();

//.cpp file
void ABasePlayerController::RespawnCharacter_Implementation()
{
    UBaseGameInstance *tempGameInstanceRef = static_cast<UBaseGameInstance*>(GetGameInstance());
    tempGameInstanceRef->IsClient();
}
上一篇:C中的流程控制


下一篇:如何在Chrome下使用Postman进行rest请求测试