- Building an RPG with Unreal
- Steve Santello Alan R. Stagner
- 407字
- 2021-07-23 14:35:39
Creating a new C++ class
We're now going to create a new C++ class with the following steps:
- To do this, from the Unreal editor, click on File | Add Code To Project. We'll be creating an Actor class, so select Actor as the base class. Actors are the objects that are placed in the scene (anything from meshes, to lights, to sounds, and more).
- Next, enter a name for your new class, such as
MyNewActor
. Hit Create Class. After it adds the files to the project, click on Yes to openMyNewActor.h
in Visual Studio. When you create a new class using this interface, it will generate both a header file and a source file for your class. - Let's just make our actor print a message to the output log when we start our game. To do this, we'll use the
BeginPlay
event.BeginPlay
is called once the game has started (in a multiplayer game, this might be called after an initial countdown, but in our case, it will be called immediately). - The
MyNewActor.h
file (which should already be open at this point) should contain the following code after theGENERATED_BODY()
line:public: virtual void BeginPlay();
- Then, in
MyNewActor.cpp
, add a log that prints Hello, world! in thevoid AnyNewActor::BeginPlay()
function, which runs as soon as the game starts:void AnyNewActor::BeginPlay() { Super::BeginPlay(); UE_LOG( LogTemp, Warning, TEXT("Hello, world!" ) ); }
- Then, switch back to the editor and click on the Compile button in the main toolbar.
- Now that your actor class has compiled, we need to add it to the scene. To do this, navigate to the Content Browser tab located at the bottom of the screen. Search for
MyNewActor
(there's a search bar to help you find it) and drag it into the scene view, which is the level viewport. It's invisible, so you won't see it or be able to click on it. However, if you scroll the Scene/World Outliner pane (on the right-hand side) to the bottom, you should see the MyNewActor1 actor has been added to the scene: - To test your new actor class, click on the Play button. You should see a yellow Hello, world! message printed to the console, as shown in the following screenshot. This can be seen in the Output Log tab on the right-hand side of the Content Browser tab at the bottom of the screen:
Congratulations, you have created your first actor class in Unreal.
推薦閱讀
- Python自然語言處理實戰:核心技術與算法
- JavaScript+jQuery開發實戰
- WSO2 Developer’s Guide
- 云原生Spring實戰
- 深入淺出Android Jetpack
- Mastering C# Concurrency
- HTML5 and CSS3 Transition,Transformation,and Animation
- WordPress Plugin Development Cookbook(Second Edition)
- PhpStorm Cookbook
- Unity UI Cookbook
- 深入實踐Kotlin元編程
- Everyday Data Structures
- Illustrator CS6設計與應用任務教程
- 零基礎學Scratch 3.0編程
- 交互設計師成長手冊:從零開始學交互