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

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.

主站蜘蛛池模板: 行唐县| 民丰县| 定西市| 弋阳县| 广宗县| 岑溪市| 翼城县| 平泉县| 韶关市| 桦甸市| 芒康县| 抚远县| 彩票| 大关县| 铅山县| 普定县| 大庆市| 临澧县| 西丰县| 临颍县| 青河县| 宿迁市| 正安县| 盈江县| 明光市| 康定县| 武山县| 西乌珠穆沁旗| 广河县| 黄梅县| 六枝特区| 剑阁县| 阿坝县| 沅陵县| 新蔡县| 沙洋县| 大石桥市| 江华| 尼玛县| 长垣县| 宁城县|