《Unreal Engine4 Scripting with C++ CookBook》:Chapter 2: Creating classes

                                                   


 这个时代进步得太快,有时需要回头关爱以下那些生活在社会阴影角落里的人,这是一个健全的人格应该做的,而不是一味地关注成功学的人和事。 --- Mr. Hill


This chapter focuses on how to create C++ classes and structs that integrate well with the UE4 Blueprints editor. These classes are graduated versions of the regular C++ classes,and are called UCLASS。
 


关键的宏:

UCLASS
UPROPERTY
UFUNCTION
 


Code generated by the UCLASS macro will be located in a ClassName.generated.h fle, which will be the last #include required in your UCLASS header fle, ClassName.h。  注意一定要放到最后一个#include


 In order to be placed in levels, your C++ class must derive from the Actor base class, or below it.  -- 原来,只有继承子Actor的类才能被放到leve中。


创建一个uclass,即继承自UObject的类:

继承自UObject类的好处:  UCLASS use UE4's Smart Pointers and memory management routines for allocation and deallocation according to Smart Pointer rules, can be loaded and read by the UE4 Editor, and can optionally be accessed from Blueprints.

创建步骤:


UE4会将类内的宏进行预处理吧,并将展开的结构放在xxx.generated.h里面:


在上面的第六步中,使用UCLASS宏的时候可以给该宏传入参数:

1) 传入Blueprintable的作用是 该类可以生成蓝图,只有这样才能在UE editor创建基于这个类的蓝图:

2) 传入 BlueprintType: 使得该类可以作为其他蓝图的变量


Creating a Blueprint from your custom  UCLASS
 

上面创建了一个继承自UObject的可以用来创建蓝图的c++类,现在基于自己的这个类来创建个蓝图。

Blueprinting is just the process of deriving a Blueprint class for your C++ object。 创建一个蓝图的意义就是其可以在ue editor中被编辑哦。

步骤:



借助UPROPERTY宏创建可以在蓝图中编辑的成员变量:

步骤(在前面创建的继承子UObjet的类的基础上):

------ 原来,蓝图是要有c++类作为其基础的。

UPROPERTY的入参的意义:



Accessing a UPROPERTY from Blueprints:

--在editor中做一些操作动作以访问蓝图类的UPROPERTY成员。

举例子: blueprint diagram是什么?  https://blog.csdn.net/qq_35865125/article/details/103742952



Specifying a UCLASS as the type of a UPROPERTY
 

需要继续查资料。 本节的意思好像是:你在ue editor里面用鼠标操作创建了一些c++类的蓝图,这些蓝图都有自己的名字,但是,这些蓝图对于你的c++代码是不可见的,于是乎UE4提供了一种方法或者说机制,来让c++代码能看到这些蓝图类。 方法是: 在c++类(能用于创建蓝图的类)中增加一个下拉框成员,以存储所有的蓝图。

::前面章节提到了如何在ue editor里面鼠标创建一个c++类的蓝图,因此,这里讲得应该是如何在c++代码中通过使用ConstructObject函数来创建一个蓝图实例吧。 看来ConstructObject函数需要的入参有: c++类(可以用来创建蓝图,记作A), 蓝图类(基于A类的蓝图类)。

::你在editor中基于某些c++类创建了蓝图,但是c++代码不知道。

::方法来了,假设你写了一个c++类,叫Ab,该类继承自UObject, 可以用于生成蓝图, 你可以给该类增加UPROPERTY成员,

该成员是一个下拉框,框内存储了在ue editor中创建的各个蓝图类的名字(同一个c++类可以创建出很多个蓝图类吧)。这样的话,在editor中创建 了类Ab的蓝图,并打开该蓝图后,可以看到可以操作的下拉框,用户可以选择一个。  ---- 这样就能让c++代码看到蓝图类名了???

::需要查SpawnObject的意思。





Instantiating UObject-derived classes (ConstructObject < > and NewObject < >)
 

Creating class instances in C++ is traditionally done using the keyword new. However, UE4 actually creates instances of its classes internally, and requires you to call special factory functions。 When you create UObjectderived classes, you will need to instantiate them using special UE4 Engine functions。

用UE4提供的专门函数来创建类对象的好处多多:

The factory method allows UE4 to exercise some memory management on the object, controlling what happens to the object when it is deleted. This method allows UE4 to track all references to an object so that on object destruction, all references to the object can be easily unlinked. This ensures that no dangling pointers with references to invalidated memory exist in the program
 

::第一个参数是BluePrint class,指的是在editor中鼠标操作生成的BP吗。

步骤:

::该节与上一节紧密相连。这前两步操作时上节的内容,把蓝图类名传给c++ code。

两个函数的比较:

使用工厂方法模式的好处:



Destroying UObject-derived classes


Removing any UObject derivative is simple in UE4. When you are ready to delete your UObject-derived class, we will simply call a single function (ConditionalBeginDestroy()) on it to begin teardown. We do not use the native C++ delete command on UObject derivatives. We show this in the following recipe.
 

You need to call ConditionalBeginDestroy() on any unused UObject-derived classes so that they get removed from memory. Do not call delete on a UObject-derived class to recoup the system memory. You must use the internal engine-provided memory management
functions instead。
 

::介绍了UE的垃圾回收机制。类似我在苏州做的机器人控制语言解释器的垃圾回收。



Creating a USTRUCT
 

-- 写ue的一个蓝图类代码时,我没有将某些相关数据聚合在一块,主管说过我可以用struct,说的就是这种吧。

You may want to construct a Blueprints editable property in UE4 that contains multiple members. The FColoredTexture struct that we will create in this chapter will allow you to group together a texture and its color inside the same structure for inclusion and specifcation in any other UObject derivative, Blueprintable class:

回顾历史:

例子:



Creating a UENUM( )

C++ enum are very useful in typical C++ code. UE4 has a custom type of enumeration called UENUM(), which allows you to create an enum that will show up in a drop-down menu inside a Blueprint that you are editing(给BP用的哈,之前遇到过).



Creating a UFUNCTION
 

UFUNCTION() are useful because they are C++ functions that can be called from both your C++ client code as well as Blueprints diagrams. Any C++ function can be marked as a UFUNCTION()。 (是为了能在蓝图中调用呀)。

::需要实践体会。

发布了341 篇原创文章 · 获赞 87 · 访问量 22万+

猜你喜欢

转载自blog.csdn.net/qq_35865125/article/details/103449770