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

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.

主站蜘蛛池模板: 青河县| 定结县| 布尔津县| 张家口市| 宜丰县| 成武县| 黔江区| 崇信县| 陇西县| 南涧| 庐江县| 北流市| 象州县| 新安县| 金坛市| 烟台市| 屯昌县| 慈溪市| 门源| 麻栗坡县| 龙泉市| 静乐县| 平潭县| 汾西县| 绿春县| 黄石市| 乌兰察布市| 揭东县| 浮梁县| 泸西县| 榆社县| 胶州市| 高雄县| 武平县| 英超| 崇左市| 博客| 庆元县| 惠州市| 金塔县| 哈巴河县|