Good Programming Practises

If a variable doesn’t need to be stored anywhere, and you are not checking it over multiple frames, there isn’t much point to keep it as a member var, just a local var would be fine.

The Order Of C++ Inline Initialization (New In C++11), Initialization List, And Initialize In Constructor

Talk is cheap. Play with the code below to understand the order of inline initialization, initialization list and initialize in the class constructor.

#include <iostream>

using namespace std;

class Base
{
public:
  int Id = 0;                       // 1st
  int Id2 = Id + 1;                 // 2nd

  Base () : Id2 (Id - 1), Id (1)    // 4th, 3rd  An warnning about different orders in declarations and the init list
  {
    Id++;                           // 5th
  }

  void SayId ()
  {
    cout << "My Id is " << Id << " Id2 is " << Id2 << endl;
  }
};

// Type of Inheritance https://www.tutorialspoint.com/cplusplus/cpp_inheritance.htm
class Derived : public Base         // The public is called access-specifier, if it's not used, it's private by default.
{
public:
  Derived ()/* : Id2 (99)*/         // Can't init Id2 in here because it's not a member in class Derived
  {
    Id = 3;                         // 6th
  }
};

int main(void)
{
  Base Object;
  Derived DerivedObject;

  Object.SayId();                   // Id 2   Id2 0
  DerivedObject.SayId();            // Id 3   Id2 0

  return 0;
}

 

Unreal Engine 4.15.3 Profiler Tool (Session Frontend)

Unreal Engine has provided a tool called Session Frontend which can show statistics about memory and time cost. Itā€™s very useful when we want to check if our changes are helpful or not.

We can find this tool by clicking Window->Developer Tools.

There are several steps to profile a game using the profiler tool.

1) Select a sessionĀ which we want to profile from the Session Browser.

We can select a session by left-clicking it. If it doesnā€™t work, I suggest click the This Application part first to hide and show the session options and left-click it again.

2) Run the game in editor.

3) Press the Data Capture button to start collecting data from the gameĀ Or you can type Stat StartFile in the game console which can be opened by button Ā¬ (on the left hand of button number 1).

4) Press the Data Capture button again to stopĀ Or you can typeĀ Stat StopFile in the game console. If we use the console command we will be able to create a .ue4stats file inside theĀ ProjectNameSavedProfilingUnrealStats folder which we can load by pressing the Load button in the Session Frontend tool. It should work the same way as we use the DataCapture button.Ā 

5) Click the Yes button in the pop-up window to transfer the stats file to the machine.

6) Press the Load file button.

If it goes well, we should be able to see the stats in the window. We are interested in the stats in the Navigation category. We can toggle the stats by clicking their names.

Disable the Start/Stop Sound When Playing in Editor UE 4.15 关闭åœØē¼–č¾‘å™Øč·‘ęøøęˆå¼€å§‹å’Œē»“ęŸēš„声音 Unreal Engine 4.15

Edit->Editor Preferences->Miscellaneous->Enable Editor Sounds, uncheck this checkbox, there is no sound when hitting the Play or Stop button in editor anymore : ).

å–ę¶ˆęŽ‰čæ™äøŖ选锹ļ¼Œå°±äøä¼šå†ęœ‰ēƒ¦äŗŗēš„声音当åœØē¼–č¾‘å™Øé‡Œč·‘ęøøꈏäŗ†ć€‚

 

C++ ēš„ Rule of Three/Five/Zero

꜀čæ‘åœØē”Ø C++ ēš„ę—¶å€™é‡åˆ°äŗ†å†…å­˜åŖ增加äøé‡Šę”¾ēš„问题ļ¼Œę€€ē–‘ę˜Æå†…å­˜ę³„ę¼ļ¼ŒåŽę„发ēŽ°ę²”꜉怂ꈑäŗŽę˜Æåƹ C++ ēš„å†…å­˜č§„åˆ™åˆåšäŗ†äø€äŗ›ęœē“¢ļ¼ŒēŽ°åœØꊊē»“č®ŗ分äŗ«äø€äø‹ļ¼Œå¦‚ęžœęœ‰é”™åøŒęœ›å¤§å®¶čƒ½ęŒ‡å‡ŗļ¼Œę„Ÿęæ€äøå°½ć€‚

ęˆ‘ä»¬éƒ½ēŸ„道åœØ堆 heap äøŠåˆ†é…ēš„内存ę˜Æ要ē”±ē؋åŗå‘˜č‡Ŗå·±é‡Šę”¾ēš„ļ¼Œé€šåøøęƒ…å†µäø‹čæ™ę²”ęœ‰ä»€ä¹ˆé—®é¢˜ļ¼Œä½†ę˜Æå¦‚ęžœęˆ‘ä»¬ęœ‰äø€äøŖ vector å­˜ę”¾ object ēš„ęŒ‡é’ˆć€‚čæ™äøŖ vector 又ę˜Æäø€äøŖ class ēš„ attribute ęƒ…å†µå°±å˜å¾—å¤ę‚äŗ†ć€‚čæ™ē§ęƒ…况äø‹ļ¼Œå½“ class č¢«ęžęž„ destruct ēš„ę—¶å€™ļ¼Œvector ä¼šé‡Šę”¾ęŽ‰å…¶äø­ęŒ‡é’ˆå ē”Øēš„内存ļ¼Œä½†ę˜Æäøä¼šé‡Šę”¾ęŒ‡é’ˆęŒ‡å‘ēš„åÆ¹č±”ę‰€å å†…å­˜ļ¼Œę­¤ę—¶ļ¼Œå°±ä¼šęœ‰å†…å­˜ę³„ę¼å‘ē”Ÿć€‚

ꉀ仄čæ™ē§ęƒ…况äø‹ęˆ‘ä»¬éœ€č¦ē»™čæ™äøŖ class å£°ę˜Žäø€äøŖč‡Ŗ定义ēš„ęžęž„å‡½ę•°ļ¼ŒåœØ其äø­mannually é‡Šę”¾ęŽ‰ęƏäøŖęŒ‡é’ˆęŒ‡å‘åÆ¹č±”ę‰€å å†…å­˜ć€‚C++ 里꜉äøŖ Rule of Three čÆ“ēš„å°±ę˜Æå¦‚ęžœä½ å®šä¹‰äŗ†ęžęž„å‡½ę•°(如äøŠę‰€čæ°ęƒ…况)ļ¼Œé‚£ä¹ˆä½ åŗ”čÆ„čæ˜éœ€č¦č‡Ŗå®šä¹‰ę‹·č“ęž„é€ å‡½ę•°å’Œčµ‹å€¼čæē®—ē¬¦ć€‚

另äø€äøŖę›“å„½ēš„ę–¹ę³•ę˜Æä½æē”Ø smart pointer, ęˆ‘ä»¬å°±äøéœ€č¦č‡Ŗå®šä¹‰ęžęž„å‡½ę•°ę„é‡Šę”¾ęŒ‡é’ˆęŒ‡å‘ēš„åÆ¹č±”äŗ†ć€‚

čæ™é‡Œå†čÆ“äø€ē‚¹å°±ę˜Æå·® C++ ēš„äøœč„æꈑäø€čˆ¬äøŠ cppreference.com ęÆ”č¾ƒęƒå؁

Fatal Error C1083: Cannot open include file: 'xxx.generated.h': No such file or directory Unreal Engine 4 Plugins

I am trying to develop a game plugin for Unreal Engine 4.15. I’d like to do something with the FoliageType, so I include the FoliageType.h like this

#include "FoliageType.h"

(Unreal Engine has its own rule to include all .h file from the Public folders so if the .h file is inside a Public folder, you can just write #include “xxx.h”, no more relative or absolute path is needed.)

Then, I compile my project, I get the error “fatal error C1083: Cannot open include file: ‘FoliageType.generated.h’: No such file or directory”.

The way to solve this error is adding “Foliage” to the PublicDependencyModuleNames in the PluginName.Build.cs file and regenerate your Visual Studio project files (right click the uprojectĀ file to open this. In this case this the generating project step isn’t necessary. The way I understand this generating project step is it updates the solution files tree in Visual Studio or any other IDEs, so if you put some new source files in the project but you can’t see them in Visual Studio, this generating process will solve it. Another usage of this process is it creates the xxx.generated.h files.).

My guess is because I’ve included the FoliageType.h, my plugin is dependent on the Foliage Module so I have to add it.

How To Create A Game Plugin In Unreal Engine 4.15 From Scratch

I am working on adding a plugin for a game project in Unreal Engine 4.15. I have done some searches about this topic and I almost couldn’t find anything up-to-date.

So, I decide to write one myself and hope it helps.

BTW, there are two types of plugins in Unreal Engine 4, engine plugins which are in the Engine/Plugins folder and game plugins which are in the game project folder. There is no Plugins folder under the game project folder by default, so if you are creating the first plugin for a game project, you need to create a folder named Plugins and regenerate the game project file (right click the GameProjectName.uproject and choose Generate Visual Studio project files).

1. To copy and paste one of the sample plugins(BlankPlugin) from the Unreal Engine plugins folder(Engine/Plugins/Developer) to your project Plugins folder(create one inside your project folder if you don’t have one), and rename the sample plugin to the name of your plugin. You will need to go through and rename the files, class names and plugin names throughout so that it does not conflict with the existing plugins.

1) Don’t forget to add IncludePaths and DependencyModuleNames in the PluginName.Build.cs file. (My understanding about the IncludePaths is where the compiler should go to get the include files and DependencyModules are modules should be linked by the linker. Here is a good answer about what is the difference between the public/private dependency modules) One problem about this step is most times you don’t know which module should be used, the way I use is looking at other plugins and see what they have. Another way is by guessing, you can take a look at the folder name and try to it as a DependencyModuleName. I am not sure if there is a better way to handle this or there is a doc talking about this. If so, please leave a comment and let me know.

{
        public PluginName(TargetInfo Target)
        {
                PublicDependencyModuleNames.AddRange(
                    new string[]
                    {
                        // ... add other public dependencies that you statically link with here ...
                        "Core",
                        "CoreUObject",
                        "Engine",
                    }
                );

                 PrivateDependencyModuleNames.AddRange(
                    new string[]
                    {
                        // ... add private dependencies that you statically link with here ...
                        "Slate",
                        "SlateCore",
                        "InputCore",                // Missing this may cause fatal error LNK1120: xxx unresolved externals if you use Input related Slates like SButton STextBlock
                        "WorkspaceMenuStructure",   // For #include "WorkspaceMenuStructure.h" and #include "WorkspaceMenuStructureModule.h"
                        "Projects",                 // For the implementation of IPluginManager
                     }
                );

                PrivateIncludePaths.AddRange(new string[] { "PluginName/Private" });
                PublicIncludePaths.AddRange(new string[] { "PluginName/Public" });
         }
}

2. regenerate your project files so you can see the latest files of your project inside the Solution Explorer Window of visual studio.

1) Right click ProjectName.uproject
2) Select Generate Visual Studio project files

Here is a ppt talking about how to create plugins in UE4.Ā UE4Creatingplug-ins

Github Is Not Always Connectable

When you are trying to connect to the github.com and a TIME OUT happens. You may see a fatal error log looks like this

https://github.com/xxx/xxx.git/': Failed to connect to github.com port 443: Timed out

Either because you are blocked. So, you need a proxy to connect to github.com which has been talked a lot on stackoverflow.

Or there is a problem on the github end. You can see the status of github.com through this website.