查看: 212|回复: 9

[编程指南] 【1. Creating an Actor that uses a timer | Unreal Engine】

[复制链接]

1

主题

273

帖子

7万

积分

管理员

Rank: 9Rank: 9Rank: 9

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


Previous Step


   
Next Step



   
Programming Tutorials Home



If you are new to Unreal Engine 4, you might want to read our 编程快速入门 tutorial first. For this tutorial, we will assume you are familiar with creating a project and adding C++ code to it.


We will begin by creating a new, Basic Code project, with starter content, named "HowTo_VTE", and then adding an Actor class to it. We'll name it "Countdown" in this tutorial.

ChooseParentClass.jpg




NameYourActor.jpg




We'll start by creating a simple countdown timer that we can see in-game. In Countdown.h, add the following lines to the end of our class definition:

int32 CountdownTime;UTextRenderComponent* CountdownText;void UpdateTimerDisplay();
In Countdown.cpp, we can create our renderable text Component and initialize our countdown time to 3 seconds. We can also turn Ticking off for this type of Actor, since we will not need it. ACountdown::ACountdown should look like this:

PrimaryActorTick.bCanEverTick = false;CountdownText = CreateDefaultSubobjectUTextRenderComponent(TEXT("CountdownNumber"));CountdownText-SetHorizontalAlignment(EHTA_Center);CountdownText-SetWorldSize(150.0f);RootComponent = CountdownText;CountdownTime = 3;
ACountdown::UpdateTimerDisplay should update our text display to show the time remaining, or zero if the time is up. This code should run when we first spawn our ACountdown into the game, and once per second until our CountdownTime variable hits zero.

void ACountdown::UpdateTimerDisplay(){    CountdownText-SetText(FString::FromInt(FMath::Max(CountdownTime, 0)));}
Whenever we assign a Timer to run a function, we are given a Timer Handle. We need to hold onto that handle so that we can shut the Timer down when the countdown finishes. Let's add the function to count time down, and the Timer Handle we'll need to control it, to the class definition in Countdown.h. While we're there, let's also add a function to do something special when the countdown ends:

void AdvanceTimer();void CountdownHasFinished();FTimerHandle CountdownTimerHandle;
We can also write the body of ACountdown::AdvanceTimer and ACountdown::CountdownHasFinished in Countdown.cpp now:

void ACountdown::AdvanceTimer(){    --CountdownTime;    UpdateTimerDisplay();    if (CountdownTime  1)    {
//We're done counting down, so stop running the timer.
GetWorldTimerManager().ClearTimer(CountdownTimerHandle);
CountdownHasFinished();    }}void ACountdown::CountdownHasFinished(){    //Change to a special readout    CountdownText-SetText(TEXT("GO!"));}
Let's initialize the text display in ACountdown::BeginPlay by adding a call to our new update function, and setting a timer to advance and update the countdown once per second:

UpdateTimerDisplay();GetWorldTimerManager().SetTimer(CountdownTimerHandle, this, ACountdown::AdvanceTimer, 1.0f, true);

We are updating the display in ACountdown::BeginPlay rather than ACountdown::ACountdown because values set to variables in the Unreal Editor will be assigned after the constructor, but before BeginPlay. We will want to respect those values later, when we expose CountdownTime to the editor.


Let's check our progress so far by going to the Unreal Editor and pressing Compile.

CompileFromEditor.jpg




We can then drop our updated ACountdown class from the Content Browser into the Level Editor.

ClassInContentBrowser.jpg




LevelEditorText.jpg





Because we set our countdown text during ACountdown::BeginPlay and not ACountdown::ACountdown, the default "Text" is shown in the Level Editor.
When we press Play, the countdown will progress as expected, saying "3", "2", "1", and finally "GO!"


At this point, we've already created a simple class that uses a timer. Non-programming users would get much more out of it if they could set the countdown time, or change the behavior when the countdown finishes. Next, we'll expose these features to the editor.


Work-In-Progress 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    int32 CountdownTime;    UTextRenderComponent* CountdownText;    void UpdateTimerDisplay();    void AdvanceTimer();    void CountdownHasFinished();    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(){    //Change to a special readout    CountdownText-SetText(TEXT("GO!"));}

  Previous Step


   
Next Step



   
Programming Tutorials Home
回复

使用道具 举报

0

主题

848

帖子

2818

积分

vip会员

Rank: 1

积分
2818
发表于 2016-7-2 18:19:41 | 显示全部楼层
全就行啊,,支持 一下
回复 支持 反对

使用道具 举报

0

主题

826

帖子

2723

积分

vip会员

Rank: 1

积分
2723
发表于 2016-7-3 09:54:21 来自手机 | 显示全部楼层
要查看本帖隐藏内容请回复
回复 支持 反对

使用道具 举报

0

主题

886

帖子

2913

积分

vip会员

Rank: 1

积分
2913
发表于 2016-7-4 06:46:29 来自手机 | 显示全部楼层
我现在有的是钱,我要下了!
回复 支持 反对

使用道具 举报

0

主题

911

帖子

2983

积分

vip会员

Rank: 1

积分
2983
发表于 2016-7-7 17:21:47 来自手机 | 显示全部楼层
谢谢支持。
回复 支持 反对

使用道具 举报

0

主题

861

帖子

2822

积分

vip会员

Rank: 1

积分
2822
发表于 2016-7-8 23:42:59 来自手机 | 显示全部楼层
这个不会又是超级贵吧.
回复 支持 反对

使用道具 举报

0

主题

893

帖子

2922

积分

vip会员

Rank: 1

积分
2922
发表于 2016-7-11 03:08:05 来自手机 | 显示全部楼层
来看看......
回复 支持 反对

使用道具 举报

0

主题

865

帖子

2817

积分

vip会员

Rank: 1

积分
2817
发表于 2016-7-12 02:54:53 来自手机 | 显示全部楼层
看看贵不贵,不贵就下
回复 支持 反对

使用道具 举报

0

主题

833

帖子

2729

积分

vip会员

Rank: 1

积分
2729
发表于 2016-7-15 04:27:09 来自手机 | 显示全部楼层
楼主辛苦
回复 支持 反对

使用道具 举报

0

主题

867

帖子

2882

积分

vip会员

Rank: 1

积分
2882
发表于 2016-7-19 22:25:31 | 显示全部楼层
好棒好棒好棒,谢谢楼主分享
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | enginedx注册

本版积分规则

 
 



邮件留言:


 
返回顶部