This article is based on official document data driven gameplay elements.
Have done the game should be clear, if the game a little bit of scale, then use data-driven to do the game is a necessary step, generally is planning through the way to solve the table. Let's briefly talk about how to use DataTable in UE4 for data-driven development.
As the name implies, a data table is a table that classifies various related data in a meaningful and useful way, where a data field can be any valid Uobject attribute, including an asset reference. Before the designer imports the CSV file into the datasheet, the programmer must create a row container to indicate how the engine interprets the data. These data tables contain column names, which correspond to the code-based USTRUCT structure and its (child) variable one by one, and the structure of the ustruct must inherit from Ftablerowbase to be recognized by the importer.
We built a random test list (CSV) as follows:
Id,hp,icon,blueprintkey
1,100,texture2d '/game/firstperson/textures/test.test ', Class '/game/firstperson/bp_datatabletest.bp_ Datatabletest_c '
2,200,texture2d '/game/firstperson/textures/test.test ', Class '/game/firstperson/bp_datatabletest.bp_ Datatabletest_c '
Where Bp_datatabletest is a blueprint class that inherits from Aactor, Bp_datatabletest_c is the actual generated blueprint class.
The corresponding C + + code is as follows:
/** Note that the name of the member variable in this struct is the same as in the CSV table, because it is the UE4 that implements the initialization of the data through the reflection system, and of course the name is different, but the DisplayName in its metadata must correspond to the field values in the table. You can refer to the implementation in the DataTableCSV.cpp for more details. */ustruct (blueprinttype) struct fdatatabletestdata:public ftablerowbase{generated_ustruct_body () Public: Fdatatabletestdata (): hp (0) {}uproperty (Editanywhere, blueprintreadwrite, Category = "DataTable Test") int32 hp; Uproperty (Editanywhere, blueprintreadwrite, Category = "DataTable Test") tassetptr<utexture2d> icon;// Note that this variable name differs from the CSV, but I set it to "Blueprintkey" as well as DisplayName, and note that I use tassetsubclassof instead of tassetptr, because if it is a class, you need to use this. Uproperty (Editanywhere, blueprintreadwrite, Category = "DataTable Test", DisplayName = "Blueprintkey") tassetsubclassof <AActor> BP;};
Once the code has been compiled, we can import the CSV file you just created into the editor, import the file via import, as shown in the following example:
Inside the blueprint, use the following:
Attention:
in the above data representation example , the referenced asset is a deferred load asset ( and to implement asynchronous loading, note the clock icon in the upper-right corner). utexture , all assets will be loaded when the data table is loaded. About
Using data table in UE4