UE4 | Inventory for Multiplayer # 2 | Connecting Blueprint to C ++

List of articles [/b]
UE4 | Inventory for Multiplayer # 1 | Data warehouse on the DataAsset
 
UE4 | Inventory for Multiplayer # 2 | Connecting Blueprint to C ++
 
In the previous article I told you how to create
DataAsset
, and why it is so good and comfortable. Here we will look at how to access
DataAsset
, more precisely to the data assigned to it, from
Blueprint
and
C ++
.
 
In passing, we will answer the question of gaining access to any
Blueprint
of
C ++
. previous article , then probably noticed that other functions call such an attribute do not have. This is done only because the data they call are currently in
Blueprint
not needed. If someone does not need to know something, do not hurry to report it.
 
The next step is to create in any
Blueprint
variable of the type of the database we created (in my case this is
.BP_DreampaxItemsDataAsset
).
 
After that, easily remove the assigned texture.
 
 
Now consider how to access information in
C ++
.
 
We can not simply refer to class
DreampaxItemsDataAsset
because it does not contain any information. We need to get access to
BP_DreampaxItemsDataAsset
.
 
There are two basic methods for accessing
Blueprint
.
 
First, let's look at the inconvenient way of connecting using ConstructorHelpers. In this case, it's access to the texture.
 
ASHUD :: ASHUD (const class FObjectInitializer & ObjectInitializer)
: Super (ObjectInitializer)
{
/* You can use the FObjectFinder in C ++ to reference content directly in code. Although it's advisable to avoid this and instead of assigning content through the Blueprint child classes. * /
static ConstructorHelpers :: FObjectFinder
HUDCenterDotObj (TEXT ("/Game /UI /HUD /T_CenterDot_M.T_CenterDot_M"));
CenterDotIcon = UCanvas :: MakeIcon (HUDCenterDotObj.Object);
}

 
An example is taken from the remarkable project EpicSurvivalGameSeries , ideally suited for the study of
Multiplayer
on
C ++
. The author set the goal to show as many methods and techniques of programming the game as possible for
C ++
.
 
Why is this method uncomfortable? The same trouble as with
DataTable
- when changing the name
Blueprint
or location, the file will not be found.
 
Most preferable is the method in which we declare a variable in the header file, for the subsequent assignment of it already in the inherited
Blueprint
. For the example above, it might look like this:
 
UPROPERTY (EditDefaultsOnly, Category = "AimPointer")
FCanvasIcon CenterDotIcon;

 
Much easier, right?
 
 
Now, knowing how to access any
Blueprint
we can connect our database without problems.
 
UCLASS ()
class ADreampaxGameMode: public AGameMode
{
GENERATED_BODY ()
public:
ADreampaxGameMode (const FObjectInitializer & ObjectInitializer);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Data Bases
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
public:
/* Connect data base in BP for items * /
UPROPERTY (EditDefaultsOnly, Category = "Database")
class UDreampaxItemsDataAsset * DreampaxItemsDataAsset;
FORCEINLINE UDreampaxItemsDataAsset * GetDreampaxItemsDataAsset () const;

 
A small digression for a non-pro on the topic of variable declaration [/b]
As a man almost unfamiliar with
C ++
, I broke a lot of copies, trying to figure out how to declare
correctly. custom
variables.
 
If the goal is to declare a variable of the class we created, like, for example,
 
UDreampaxItemsDataAsset * DreampaxItemsDataAsset;
//or
class UDreampaxItemsDataAsset * DreampaxItemsDataAsset;

 
I personally did not understand for a while when to use
class
, and when not.
 
Everything turned out to be painfully simple.
 
 
If you do not put
class
, then you need to perform the inclusion
#include "Data /DreampaxItemsDataAsset.h"
, containing the declaration of this class.
 
If you bet
class
, then
#include "Data /DreampaxItemsDataAsset.h"
can be done already in
. cpp
.
 
And one more option of the previous paragraph, if you want to declare at once a lot of variables of this class. Immediately after all
#include
preliminary declare our class
class UDreampaxItemsDataAsset;
, and after declaring the variables already without the prefix
class
.
 
 
Which of these methods is correct - I can not say. If someone explains, I will be grateful.
 
We make a variable in
C ++
class
ADreampaxGameMode
, since it is visible only to the server, and all that is associated with
spawn
objects must go only through the server. This class is the parent for
BP_DreampaxGameMode
, where we will connect our
BP_DreampaxItemsDataAsset
.
 
Now all the power is
C ++
can be used to work with the data of our database.
 
In the next article (finally!) We'll talk about creating the inventory and find out why we can not do without the already created
DataAsset
.
 
If there are questions or wishes to disclose any aspect in more detail, please write in the comments.
+ 0 -

Add comment