官术网_书友最值得收藏!

Creating a new C++ class

We're now going to create a new C++ class with the following steps:

  1. 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).
  2. 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 open MyNewActor.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.
  3. 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).
  4. The MyNewActor.h file (which should already be open at this point) should contain the following code after the GENERATED_BODY() line:
    public:   virtual void BeginPlay();
  5. Then, in MyNewActor.cpp, add a log that prints Hello, world! in the void AnyNewActor::BeginPlay() function, which runs as soon as the game starts:
    void AnyNewActor::BeginPlay()
    {
        Super::BeginPlay();
        
        UE_LOG( LogTemp, Warning, TEXT("Hello, world!" ) );
    }
  6. Then, switch back to the editor and click on the Compile button in the main toolbar.
  7. 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:
  8. 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.

主站蜘蛛池模板: 辽阳市| 广汉市| 阳朔县| 兰州市| 韩城市| 虞城县| 余干县| 吴桥县| 仙居县| 崇州市| 涿鹿县| 沐川县| 榕江县| 无为县| 北海市| 日土县| 时尚| 怀安县| 卢龙县| 乐昌市| 沈丘县| 静安区| 青铜峡市| 黄平县| 南靖县| 天津市| 驻马店市| 淅川县| 盐山县| 乐陵市| 濉溪县| 类乌齐县| 逊克县| 阜南县| 福州市| 莱西市| 湛江市| 渝北区| 油尖旺区| 洮南市| 台湾省|