Unreal Engine04: Collaborative development of Visual Studio and Editor

written in front

Here is mainly to record the relationship between Visual Studio and Editor and how to use them for UE4 collaborative development.

1. Start

1. Start Visual Studio first and then start Editor

  • xxx.sln(1) Open the solution in the root directory of the project to start Visual Studio, which is the same as opening a normal C++ project;
    Open the C++ project

  • (2) In the opened C++ project, click the local Windows debugger to compile the program to start the Editor;
    start the editor

  • But in this way, the modified code will not take effect in the Editor, you need to stop debugging, recompile, debug and start the Editor to make the modified code take effect;

  • So this method is not recommended;

2. Start Editor first and then Visual Studio

  • (1) There are four ways to start the Editor:

  • Open an existing UE4 project :

  • If there is an existing UE4 project, you can open the root directory of the project xxx.uprojectto start the Editor corresponding to the project;
    Open the corresponding Editor

  • Create a new UE4 project or open an existing UE4 project :

  • If there is no UE4 project that has been created before, or you need to create a new UE4 project, you can click the UE4 Unreal Engine shortcut (usually it will be on the desktop after the official installation) to start the Editor, and then follow the instructions to create a new UE4 project;
    UE4 icon

  • [Recommended] Of course, you can also click on the UE4 Epic Games Launcher shortcut to start the corresponding version of the engine in the library , or enter the editor to create a new project guide. This method is generally recommended because different versions of the engine can be managed;
    Launcher icon

  • In addition, if the Unreal Engine is obtained through source code compilation, you need to open it in the source code UE4.sln, first call Visual Studio to open Unreal Engine, then right-click the UE4 project -> Debug -> Start new instance to call up the Editor interface, so you can also enter Editor new project guidelines;

  • Of course, in addition to creating a new UE4 project, the above three methods can also open an existing UE4 project. As long as the Editor is started, it is basically easier to operate;

  • (2) After creating a new project in the Editor or opening an existing project, click File -> Open Visual Studio on the toolbar to open the UE4 project code corresponding to the project with Visual Studio;
    Open VS

  • Note that the Editor of the project is different from the Editor started when creating a new project. The latter can be regarded as a bootstrap program, while the former is the xxx.uprojectcorresponding Editor;

2. Directory structure

1. C++ project file directory structure

  • The project file structure of a basic UE4 project is as follows:
    file structure
  • The corresponding meanings of each folder are as follows:
    • Binaries: Executable files and DLL files ;
    • Config: configuration file , including project settings, configuration files such as keyboard input;
    • Content: Store the content files of the engine or game , including maps, textures, models, materials, and blueprints;
    • Intermediate: Temporary construction file , including temporary files generated when compiling the engine or game;
    • Saved: Saved content , including automatically saved files, local configuration files ( *.inifiles), screenshots and log files;
    • Source: source code , including all source code of the project;
  • The most important folders are Contentand Source, the former is used to manage various content files, and the latter is used to manage source code, which is a bridge between C++ projects and Editor;

2. Editor content browser file directory structure

  • The file directory structure of a basic Editor content browser is as follows:
    content browser

  • (1) 内容The folder and the folder in the C++ project are basically completely corresponding, and the resource files inside can be found in the folder Contentthrough the file explorer , and the directory structure is consistent;Content

  • (2) C++类Folders and folders are basically completely corresponding, all the C++ classes that appear can be found in the folders Sourceof the file explorer , and the directory structure is consistent;Source

  • A simple example is as follows:

  • TestCppGameModeBaseThis class Sourcehas a corresponding .cppand in the folder .h, and a corresponding mapping in the content browser;

Classes in Editor
Classes in VS

  • If you find that some folders are missing in the content browser, you can click the view option in the lower right corner to check the required content, and you can see it in the content browser;
    view options

3. Collaboration between C++ classes and blueprints

  • Visual Studio is responsible for managing the source code of C++ classes, and Editor is responsible for the organization and management of blueprints and resources. The most important communication bridge between them is the collaboration between C++ classes and blueprints;
  • Here's how to create a basic UObjectclass and then call it in the blueprint;

1. Create a C++ class

  • To create a C++ class in the Editor, right-click in the C++ class->project folder directory of the content browser , and click New C++ class ;
  • Check Show all classes to display all existing classes;

Create C++ class

  • Click Next to check whether the current class is created under the project folder ;
  • Check the public button, and the files of this class will be automatically .hplaced in the Public folder, and .cppthe files will be placed in the Private folder;
  • Modify the name of the class, pay attention to use big hump , all the first letters are capitalized, no need to add a prefix , because this is the class file name, the real class name UE4 will automatically add it for us;

create class

  • If the new class is not displayed in the content browser, you can:
    • Use the Editor's compile button to compile;
    • Generate solution compilation in Visual Studio ;
    • Or try to re-enter the corresponding directory and refresh the interface to display the content;

2. Create the blueprint class corresponding to the C++ class

  • The class created for UObjectthe parent class is the most basic class, and the content in the class is very concise;
  • Its header file includes class declarations, class member variables and class member function declarations, as follows:

header file content

  • Its cppheader file contains only one line, which reads:

cpp content

  • At this time, PrintOnScreenObjectthe class cannot create a blueprint class in the Editor, and Blueprintablea specifier needs to be added to it, as follows:

add specifier

  • Back in the Editor, right-click on the C++ class to create a blueprint class based on xxx , and you can create the corresponding blueprint class, as follows:

Create a blueprint class

  • Blueprint is a kind of resource, so the created blueprint class will be Contentunder the folder;
  • After creating the blueprint class, move it to the appropriate folder and classify it, as follows:

Created blueprint class

3. Implement C++ member functions

  • In order to make PrintOnScreenObjectthe member variables and functions of the class visible in the blueprint, it is necessary to add corresponding macro specifiers to them, as follows:

member variables and member functions

  • Then, .cpprealize the printing function in the file, refer to the blog: UE4 C++ printing ;
  • (1) UE4 prints to the log:
// 日志实现的格式如下
UE_LOG(日志变量名, 日志级别, 日志内容);
// 日志的级别有三种:Log, Warning, Error
// 输出文本
UE_LOG(LogTemp, Log, TEXT("This Log is grey text."));
// 输出含普通类型字符串
int32 ID = 5;
UE_LOG(LogTemp, Warning, TEXT("This Log's id = %d"), ID);
  • (2) UE4 prints to the screen:
// 打印到屏幕的实现格式如下
GEngine->AddOnScreenDebugMessage(-1, 显示持续时间, 显示颜色, 显示内容);
// 打印文本
GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Red, TEXT("Hello world!"));
// 打印含普通类型的字符串
FString MyName = "Jeremy";
GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Red, FString::Printf(TEXT("My name is: %s."), *MyName));
float Height = 179.0f
GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Red, FString::Printf(TEXT("I am %f cm tall."), Height));
  • Therefore, the implemented member functions are as follows:

Member function implementation

4. Build a blueprint that calls a C++ class

  • Because only Actors can be placed in the map, and the classes created earlier are UObjecttyped, an additional Actor blueprint class must be created, as follows:

Actor blueprint class

  • Double-click the Actor blueprint class to open the blueprint editing interface, and build PrintOnScreenObjecta blueprint that calls the blueprint class, as follows:

Build a blueprint

  • Some points to note are as follows:
    • First, let the current Actor class have a PrintOnScreenObjectclass member variable, just add it in My Blueprint -> Variable at the bottom left;
    • The logic of the blueprint is: construct a PrintOnScreenObjectclass object whose owner is the current Actor class, then assign the constructed object to PrintOnScreenObjecta variable, and then call PrintOnScreenObjectthe member function of the class through the variable PrintToScreen;
    • The connection of white dots represents the transfer of execution process , and the connection of other points represents the transfer of variable data ;
    • If the name of a member function or variable is modified in the C++ code, the corresponding bubble in the blueprint needs to be rebuilt;
    • Right-click the bubble node to perform quick node annotation ;
    • When compiling the C++ project fails, you can go to Editor to compile first, and then return to Visual Studio to compile;
  • After building the blueprint, click the compile button in the upper left corner;
  • Then go back to the main panel and drag it BP_PrintOnScreenActorinto the map to generate an instance, otherwise the blueprint will not take effect;

into the Actor class

  • Finally click compile and play ;
  • The effect is that 5 seconds of content is printed on the screen, and warning level log information is output in the log;

5. Delete class

  • Delete the blueprint class:

    • Right-click the blueprint to be deleted in the content browser, and select Delete ;
    • When deleting, if you choose to replace the reference , all instances of the blueprint class will be replaced, and if you choose to force delete , all instances of the blueprint class will be deleted forcibly;
  • Deleting a C++ class is a little more cumbersome:

    • Close all Editor and Visual Studio windows;
    • Delete the corresponding .cppand .hfile of the class;
    • Delete the Binaries folder;
    • Right-click .uprojectthe file, select Generate Visual Studio project files , and regenerate the vs project file;

4. Problems encountered and solutions

1. There is a red wavy line error when calling a class member variable, but the compilation can pass

  • Scenario : When creating a C++ class, the public button is checked, and .hthe file is placed in the Public folder. When .cppcalling members of the class, there are a large number of red wavy lines reporting errors. Visual Studio cannot recognize the variable type, nor can it give the class name. Function prompt, although the compilation can pass, but this programming will be very uncomfortable, there is no prompt and it is difficult to judge whether there is a spelling error;
  • Reason : The directory structure of the code is too complicated, and Visual Studio does not include the declaration files required by the current code in the search path;
  • Solution :
    • Right-click project properties ;
    • Under the search path in NMake , add the path where the declaration file is located. More specifically, it is the path of the declaration file (usually a file) where the variable or function is located in the current code where the red wavy line is displayed;.h

include search path

2. There is a red wavy line error when calling the system class function, but the compilation can pass

  • Scenario : When calling some other classes that come with UE4, it prompts that the class declaration cannot be found, or prompts that pointers are not allowed to point to incomplete class types , and there is no function prompt, but the compilation can pass;
  • Reason : Because there is no header file corresponding to this class , but UE4 has some repair mechanism, the compilation can pass;
  • Solution : Right-click the class name in the code, click Go to Declaration , then the editor will give the location of all header files containing the variable declaration, select the appropriate header file and import it into the current code;

3. Compilation was unsuccessful in Visual Studio but the error could not be located

  • One solution : Compile in the Editor, and the error information given will be more detailed, which is convenient for locating errors;
  • In fact, compiling in Editor is equivalent to generating a solution in Visual Studio ;

4. The class in the header file prompts that the identifier is not defined

  • Scenario : If you need to use some UE4 classes in the header file, it may prompt that the identifier is not defined and report an error, because the header file corresponding to this class is indeed not introduced in the header file, and it is not suitable to introduce other classes in the header file. The header file of the class is generally only .cppintroduced in the file, and the header file is only for declaration;
  • Solution : Add keywords before the class name class. Of course, you can directly declare all the classes used with keywords under the header file class, so that you don’t have to declare every time you write the class name;

declare class

5. After modifying the C++ class, there is no real-time update information on the corresponding blueprint class panel

  • Scenario : After the blueprint class is derived from the C++ class, if the content of the C++ class is modified, the blueprint class may not be updated synchronously in real time;
  • Solution : Modify it to another class in Class Settings -> Parent Class , and then modify it back to the original class to refresh the information in the C++ class to the blueprint class;

Modify the parent class

Guess you like

Origin blog.csdn.net/weixin_43992162/article/details/128926068