查看: 612|回复: 9

[编程指南] 【2. Expose variables and functions to the editor | Unreal Engine】

[复制链接]

1

主题

342

帖子

7万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
75866
发表于 2016-6-27 01:04:17 | 显示全部楼层 |阅读模式



Previous Step
Next Step



   
Programming Tutorials Home


Our countdown timer is currently hard-coded to use a value of 3 seconds. It would be more useful if we could set the countdown time to any value we want in the editor, and this is easy to do. In Visual Studio, we can open Countdown.h and find the line that says:

int32 CountdownTime;
In order to expose this variable to Unreal Engine, we need to make it a UPROPERTY. This enables the engine to preserve the value of the variable when launching the game or loading a saved level. The UPROPERTY tag, with empty parentheses, is added right above the variable it affects:

UPROPERTY()int32 CountdownTime;
UPROPERTY supports arguments that change how Unreal Engine will use the variable. Since we want our variable to be editable, we can add the EditAnywhere argument:

UPROPERTY(EditAnywhere)int32 CountdownTime;
【虚幻4翻译文档-2. Expose variables and functions to the editor | Unreal Engine】[虚幻4中文文




We can also add a comment to our variable in C++, and our comment will become the description of the variable in the Unreal Editor, like this:

//How long, in seconds, the countdown will runUPROPERTY(EditAnywhere)int32 CountdownTime;
【虚幻4翻译文档-2. Expose variables and functions to the editor | Unreal Engine】[虚幻4中文文





There is a lot more we can do with UPROPERTY, and looking into other arguments such as BlueprintReadWrite and Category might be good next steps, but we have all that we need at the moment.


When we return to the Unreal Editor and press Compile, our variable will appear in the Details Panel for the ACountdown we placed earlier, and we can test out different timer values by changing this number and pressing Play.

In addition to changing the value of the timer, let's also enable non-programming developers to change what happens when the timer is up. In Visual Studio, we'll open Countdown.h and find the following line:

void CountdownHasFinished();
We can expose this function to the Unreal Engine by making it a UFUNCTION, like this:

UFUNCTION()void CountdownHasFinished();
Just like the UPROPERTY macro, we need to provide information about what can be done with it in order to enable more features and access for non-programming developers. There are three options to consider:

BlueprintCallable functions are written in C++ and can be called from the Blueprint Graph, but cannot be changed or overridden without editing C++ code. Functions marked this way are usually features that have been programmed for non-programmer use, but that are not supposed to be changed or wouldn't make sense to change. An easy example of this would be any kind of math function.

BlueprintImplementableEvent functions are set up in a C++ header (.h) file, but the body of the function is written entirely in the Blueprint Graph, not in C++. These are usually created to give a non-programmer the ability to create custom reactions to special situations that have no expected default action or standard behavior. An example of this might be an event that happens when a powerup touches the player's ship in a spaceship game.

BlueprintNativeEvent functions are like a combination of BlueprintCallable and BlueprintImplementableEvent functions. They have default behaviors programmed in C++, but these can be supplemented or replaced by overriding in the Blueprint Graph. When programming these, the C++ code always goes in a virtual function with "_Implementation" added to the end of the name, as shown below. This is the most flexible option, so we will use it for this tutorial.

To grant non-programmers the ability to call our C++ function and to override it with Blueprints, we need to make the following changes to Countdown.h:

UFUNCTION(BlueprintNativeEvent)void CountdownHasFinished();virtual void CountdownHasFinished_Implementation();
Then, in Countdown.cpp, we will need to change the line that says:

void ACountdown::CountdownHasFinished()
To:

void ACountdown::CountdownHasFinished_Implementation()

We have now made a variable and a function accessible to, and alterable by, non-programmers, while providing our own default value and functionality in C++. To see how a non-programmer might use this, we'll make a Blueprint extension of our ACountdown class and modify it ourselves.


Finished Code


Countdown.h

// Copyright 1998-2016 Epic Games, Inc. All Rights Reserved.#pragma once#include "GameFramework/Actor.h"#include "Countdown.generated.h"UCLASS()class HOWTO_VTE_API ACountdown : public AActor{    GENERATED_BODY()public:     // Sets default values for this actor's properties    ACountdown();    // Called when the game starts or when spawned    virtual void BeginPlay() override;    // Called every frame    virtual void Tick( float DeltaSeconds ) override;    //How long, in seconds, the countdown will run    UPROPERTY(EditAnywhere)    int32 CountdownTime;    UTextRenderComponent* CountdownText;    void UpdateTimerDisplay();    void AdvanceTimer();    UFUNCTION(BlueprintNativeEvent)    void CountdownHasFinished();    virtual void CountdownHasFinished_Implementation();    FTimerHandle CountdownTimerHandle;};
Countdown.cpp

// Copyright 1998-2016 Epic Games, Inc. All Rights Reserved.#include "HowTo_VTE.h"#include "Countdown.h"// Sets default valuesACountdown::ACountdown(){    // 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;    CountdownText = CreateDefaultSubobjectUTextRenderComponent(TEXT("CountdownNumber"));    CountdownText-SetHorizontalAlignment(EHTA_Center);    CountdownText-SetWorldSize(150.0f);    RootComponent = CountdownText;    CountdownTime = 3;}// Called when the game starts or when spawnedvoid ACountdown::BeginPlay(){    Super::BeginPlay();    UpdateTimerDisplay();    GetWorldTimerManager().SetTimer(CountdownTimerHandle, this, ACountdown::AdvanceTimer, 1.0f, true);}// Called every framevoid ACountdown::Tick( float DeltaTime ){    Super::Tick( DeltaTime );}void ACountdown::UpdateTimerDisplay(){    CountdownText-SetText(FString::FromInt(FMath::Max(CountdownTime, 0)));}void ACountdown::AdvanceTimer(){    --CountdownTime;    UpdateTimerDisplay();    if (CountdownTime  1)    {
// We're done counting down, so stop running the timer.
GetWorldTimerManager().ClearTimer(CountdownTimerHandle);
//Perform any special actions we want to do when the timer ends.
CountdownHasFinished();    }}void ACountdown::CountdownHasFinished_Implementation(){    //Change to a special readout    CountdownText-SetText(TEXT("GO!"));}


   
Previous Step
Next Step



   
Programming Tutorials Home
回复

使用道具 举报

0

主题

844

帖子

2754

积分

vip会员

Rank: 1

积分
2754
发表于 2016-7-2 19:37:38 | 显示全部楼层
看看, 顶起. 谢谢楼主
回复 支持 反对

使用道具 举报

0

主题

859

帖子

2874

积分

vip会员

Rank: 1

积分
2874
发表于 2016-7-3 09:54:21 来自手机 | 显示全部楼层
看看有近期需要的
回复 支持 反对

使用道具 举报

0

主题

876

帖子

2927

积分

vip会员

Rank: 1

积分
2927
发表于 2016-7-4 06:46:29 来自手机 | 显示全部楼层
这个必须可以有
回复 支持 反对

使用道具 举报

0

主题

96

帖子

2060

积分

禁止发言

积分
2060
发表于 2016-7-7 17:21:47 来自手机 | 显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽
回复 支持 反对

使用道具 举报

0

主题

826

帖子

2765

积分

vip会员

Rank: 1

积分
2765
发表于 2016-7-8 23:42:59 来自手机 | 显示全部楼层
不错哦  下载看一下啊
回复 支持 反对

使用道具 举报

0

主题

864

帖子

2841

积分

vip会员

Rank: 1

积分
2841
发表于 2016-7-11 03:08:05 | 显示全部楼层
楼主真是好人吖~~非常感谢
回复 支持 反对

使用道具 举报

0

主题

907

帖子

2989

积分

vip会员

Rank: 1

积分
2989
发表于 2016-7-12 02:54:53 来自手机 | 显示全部楼层
楼主辛苦
回复 支持 反对

使用道具 举报

0

主题

825

帖子

2730

积分

vip会员

Rank: 1

积分
2730
发表于 2016-7-15 04:27:09 来自手机 | 显示全部楼层
啊啊啊啊,好多好多好多
回复 支持 反对

使用道具 举报

0

主题

1219

帖子

3800

积分

vip会员

Rank: 1

积分
3800
发表于 2016-7-19 22:25:31 来自手机 | 显示全部楼层
这个多少金币啊。
回复 支持 反对

使用道具 举报

*滑块验证:
您需要登录后才可以回帖 登录 | enginedx注册

本版积分规则

 
 



邮件留言:


 
返回顶部