Netobject class
In the previous article, I introduced the simobject class. This article mainly introduces its subclass. Among all the simobject subclasses, The netobject class is the most important subclass. However, other classes such as simset, simdatablock, scriptobject, and scriptclass are important classes. Before introducing netobject, Let's first look at these classes.
1. simset class
Simset is a container class. To be precise, it is a container class that stores simobject objects. Like all container classes, this class provides basic operations such as adding, deleting, accessing, and searching. The internal implementation method is a dynamic array. When using simset, pay attention to the following points. 1. All elements in the simset container are not exclusive to the container, that is, one element can be added to multiple simset containers. 2. When destroy is a simset object, its elements are not destroy. From these two points, we can see that the elements in simset are actually simobject pointers. When adding a pointer, it just copies the pointer to the container. In addition, a simgroup subclass is derived from the simset class. The main difference between simgroup and its parent class is that the elements in the simgroup class are unique and can only be contained by one simgroup container. Before adding an element to simgroup, the system first checks whether the element is in another container. If the element is in, it is removed from the previous container and then added to the current container. A very important subclass of simgroup is guicontrol, which is the base class of all interface controls in TGE. I will introduce the interface control class of TGE in detail in the future.
Ii. simdatablock class
We know that an object in a game, such as a player object, has many attributes, including names, lifecycles, models, quality, speed, and location. In the game, we need to update objects all the time. In fact, we need to update these attributes. However, we know that some attributes, such as the model, quality, and gravity acceleration of a character, are inherent attributes that will not be changed during the game. These attributes only need to be initialized at one time during game loading and do not need to be updated later. These attributes are abstracted as simdatablock. This class has several important functions: preload (), packdata (), and unpackdata (). Among the three functions, the first function is useful to both the server and the client. It mainly checks attribute errors and parses data before loading datablock. The packdata function is only executed on the server and is responsible for sending datablock. The unpackdata function is executed on the client and receives datablock.
Iii. scriptobject class
In scripts, we often need to define some simple objects, such as skill objects and task objects. TGE provides the scriptobject class for us to easily define the objects we need. Two attributes in scriptobject are very important. One is class, and the other is superclass. The following is a simple example:
- New scriptobject (myobject)
- {
- Class = bar;
- Superclass = Foo;
- };
- Function myobject: dosomething (% This)
- {
- Echo ("Hi, myobject! ");
- Parent: dosomething (% This );
- }
- Function bar: dosomething (% This)
- {
- Echo ("Hi! Bar ");
- Parent: dosomething (% This );
- }
- Function FOO: dosomething (% This)
- {
- Echo ("Hi! Foo ");
- }
- Function bar: Go (% This)
- {
- % This. dosomething ();
- Parent: dosomething (% This );
- }
Execute myobject. dosomething () and output: "Hi, myobject! "" Hi! Bar "" Hi! Foo"
Execute myobject. Go () and output: "Hi! Bar "" Hi! Foo"
This section describes several important sub-classes of simobject. The netobject class will be introduced below.
The TGE engine is a C/S-based engine. We require that all game objects be centrally managed on the server side and then copied on each client. The logic processing is performed on the server, and then the data is updated to each client. To support this communication between the server and the client, TGE references the netobject class.
Let's take a look at the process of communication between the server and the client in TGE. First, there will be a loading process before we enter the game. In this process, the server will clone all datablocks to all clients connected to the server. datablock includes the static attributes of the object, that is, you do not need to update attributes in real time during the game. After entering the game, the server will have a list of all game objects that will be updated in the game. When the camera of a client moves, it will crop the objects in the scene. From the rendering layer, objects out of the field of view are not rendered. Of course, these objects do not need to be updated. The engine regularly detects the camera's field of view and adds objects within the field of view to a list. The server sends an update package to update the objects in the list, including the object location and action. In addition, in order to improve communication efficiency and reduce network bandwidth, TGE uses the dirty technique: divides the attributes to be updated into several categories, such as location, action, and damage, each type is represented by a mask. If a certain attribute of an object on the server needs to be updated, the corresponding flag location is dirty and then transmitted to the client. The client updates the object based on the mask, this effectively saves bandwidth and improves performance.
After learning about the communication process in TGE, we can better understand the netobject class. The two most important functions in this class are packupdate and unpackupdate. packupdate is responsible for sending update packages to the server, and unpackupdate is responsible for receiving update packages from the client. Both functions are virtual functions, and each subclass of netobject must rewrite the function. It also provides auxiliary functions such as setmaskbits and clearmaskbits.