Torque source code analysis

Source: Internet
Author: User

Transferred from

Dedicated waiting http://blog.sina.com.cn/cpytiger

0. Prerequisites:

0.1 for more information about how to expand the engine class, such as adding member functions and member variables, and calling them through scripts, see the TDN Article http://tdn.garagegames.com/wiki/code/how_do_ I _make_a_scriptable_object%3f. For more information, see the 1.7 official manual. However, this tutorial only shows you how to create a class that can be used in C ++, but does not involve how to call script code in C ++. Here is a brief introduction: void gui3dwheel: onselectchanged (u32 selectid) {con: executef (this,
"Onselectchanged", Con: getintarg_r (selectid);} The above code is executed when the onselectchanged function is called, and the input parameter type is int, the parameter is selectid. In this case, if I define the function: function pickerbuttonholder: onselectchanged (% selectid) {// do something…} in the script environment ...} At this time, the function will be called.

0.2 contact and difference between datablock class, object class, datablock instance, and object instance. If you do not understand the difference between the above four definitions, see the 1.7 official manual-> scriptingreference-> introduction-> datablocks, which provides clear examples. Tsstatic and gamebase differences if you do not understand the differences between the above two classes, please refer to the TDN Article http://tdn.garagegames.com/wiki/dts/scripting/addingobjects.
.

0.3 how torque networking works because we have learned the computer network course, it is time to use them in actual project understanding. Torque's network collaboration method can be understood through this article, if you look at the source code, you will find that many of the content we have learned, such as the connectionprotocol class, uses the sliding window protocol. In addition, this article contains some related structures and functions, such as movewritepacketdata and readpacketdata.

1. player class structure: according to the structure of the object class and datablock class, the player class has the corresponding player and playerdata. The data definition in playerdata is delivered to the client by the server when the mission is created, when creating a player, we can use the specified playerdata to create our player. Most of the class members are public or protected. That is to say, these members can be inherited by subclass.

2. gamebase and player-related important functions are inherited from the gamebase class. From the third article, we can learn that the gamebase class has its own datablock, network features, and update features.

2.1 The gamebasedata gamebase class has its own data block gamebasedata. Only the attributes of some base classes are defined, including the stringtable and packed attributes, and some related member functions. It mainly provides templates for subclass inheritance.

2.2 gamebase network features are mainly provided by the following two virtual functions: 1. writepacketdata (): messages sent from a client to a server are generally transmitted through this function. 2. readpacketdata (): Read the functions on the server and return the verification value. Obviously, we can rewrite writepacketdata () to send our own speed or status information to the server, and rewrite readpacketdata () to read server information.

2.3 gamebase update features gamebase has two update Methods. According to the General engine, gamebase is divided into game logic-based update processtick and game time-based update advancetime: processtick (): update every 32 ms, irrelevant to FPS. It is usually used to update location, status, and other status information related to the game logic. Advancetime (): update the actual game time. It is usually used to update game time-related information such as animations. Interpolatetick (): A function used for interpolation between two tick.

3. shapebase provides basic model loading, sound, animation, damage status, energy, physics, and embedded points for you to embed objects and images. This category is already very strong and involves all aspects of the game, with a total of 4 thousand lines of code. Here, I just want to explain some methods that I think are important.

3.1 shapebasedata contains shadow-related attributes, explosion-related attributes, and physical-related attributes. More importantly, shapebasedata contains model data. Resource shape and related boundingbox, but it does not contain the information of the collision box, the information of the collision box is delayed to be included in the shapebase class. Another important point is that at this time, we have found that the source code contains a large number of lelemethod declarations for the script to reference callback calls. I will randomly refer to a method we call at ordinary times:

Consolemethod (shapebase, setwhiteout, void, 3, 3, "(float flashlevel)") {f32 flash = datof (argv [2]); If (Object-> isserverobject ()) object-> setwhiteout (flash);} we can find that when the script uses the setwhiteout function, the return value is void. The minimum value is three parameters and the maximum value is three, the description is "flashlevel" and the method class is shapebase. The script engine's setwhiteout
Finally parsed to the setwhiteout (flash) function in the C ++ engine: void shapebase: setwhiteout (const f32 flash) {mwhiteout = flash; If (mwhiteout <0.0) mwhiteout = 0; else if (mwhiteout> 1.5) mwhiteout = 1.5;} more often, we won't find the C ++ code corresponding to the functions in the script engine, because the script itself can define functions, it is usually first interpreted as a function in the script before further interpreted as C ++ code,
At this time, we need to patiently track and search again and again.

3.2 The three most important data members in shapebase: shapebase * mcontrollingobject; // The control object shapebasedata * mdatablock; // the corresponding data block convex * mconvexlist; // member functions that are important to the collision box list:

3.2.1. in terms of physical collision detection: After a physical collision occurs, it will be added to a list. When the physical collision timeout occurs, it will be notified (0.1 s by default ). Void policycollision (); Virtual void oncollision (shapebase * object, vectorf VEC); void queuecollision (shapebase * object, const vectorf & VEC );

4. Player

4.1 playerdata I believe that the data members in playerdata do not need to go into details here, because there is a very detailed list to describe the situation in Chapter 5th of game development.

4.2 The most important function in player is processtick (). The detailed process of processtick () is as follows: The information in processtick () comes from unpackupdate, and all the messages to be sent are completed through packupdate (). These two important letters are used to synchronize with other players in the network.

5. Change the player Structure

5.1 Add a group for the player. First, we can add group information in two places, which can be added in playdata or in the player class. At this time, it is necessary to complete according to their own needs. In datablock, we can simply add a group to the player at the beginning. You only need to modify the group in three places.

1. First, add a member variable: int team in the player public domain;

2. since the player class does not override initpersistfields (), we need to override this variable to expose the team variable in the player to the script for calling, otherwise, you can write the console function to access the attributes in player. So here we rewrite initpersistfields (): void PLAYER: initpersistfields () {parent: initpersistfields (); addfield ("team", types32, offset (Team, player)
);}

3. After rewriting, We Can recompile the engine. Later in the script, we can directly access the team attributes through % player. Team.

5.2 After adding a magic attack to the player function and mastering the above knowledge, it is still difficult to make a magic attack, because for a magic attack, both the magic effect and the magic return need to be controlled by a program. The magic effect can be achieved by creating a dynamic trigger. However, it is difficult to control the magic effect. Therefore, it is necessary to learn torque plot functions.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.