Ninth back stub

Source: Internet
Author: User
I don't know if it has a special name. In the engine, I call it stub. The so-called stub refers to an interface for exposing an object and connecting it with the outside world. It can be divided into two types, signal and slot, friends who are familiar with QT will immediately know what this is. In fact, this I also learned from QT, but my implementation method is different from it. an object can issue various events as needed, called signal. It can also accept events from outside world through slot. Each signal and slot has a name and uses When using these objects, you can use these names to connect the specified signal and slot to make the objects work collaboratively. For example: The simplest is the button. When it is clicked, a signal of the click will be issued:

 

 

A delay has a slot named start, which is used to trigger the timing start. When the specified time is reached, a timeup signal will be issued:

 

 

A role controller has a slot named userinput, which is used to receive various mouse/keyboard messages. After processing, it converts it into various control commands of the role and sends them in the form of signal, for example, move or turn. For unprocessed messages Unhandled output.

 

 

Those who have played crysis should be a little familiar, at least from the interface and the flow graph of crysis editor is a bit like, in fact, the flow graph of crysis is indeed the key reference object of this system. Let's talk about the implementation of this thing. In the current implementation, the essence of a signal is a function call, not a message or something. We maintain a function pointer queue for each signal, when a signal is issued, the actual task is to find These function pointers call them one by one, and slot is actually an external interface that can get the function pointer, and a processing function must be implemented accordingly to respond to calls from signal. when we connect signal and slot Find a function pointer in the target object and add it to the signal function pointer queue of the source object. since it is a function call, there will be a parameter. Currently, only one parameter can be passed in one call. I call it "property". All properties are derived from one Basic class, used to abstract the new/delete/copy/compare/type conversion parameters and other basic operations of the parameter. Then, for most common numeric types (INT, float, vector3d, matrix, string, etc.) Each writes a property class, so that the values of these types can be To become the call parameter of signal/slot. Of course, you can also define the required property if needed. I also wrote a "wild" property, which can store multiple properties and can be converted to other types of properties. It is possible to call and pass multiple parameters. the description information of stub (such as name, comment, and parameter type) is written in the class as a static member variable, while the signal function pointer queue is unique to each object, it must be written as a member variable. of course I still use my ugly The method of macro + template to complete this system. The above is a rough introduction to the implementation. I think it may not be very clear, but I am still saying that it is more important to know what to do than to know how to do it. Most experienced usersProgramMembers should be able to write such a set of things, and may write more beautifully. The following are examples: Code Class Cbutton
{
Public :
...
...
Protected :
Gstubbegin (cbutton)

Gsignalvoid (clicked, " Button clicked " ) // This signal has no parameters,

Gstubend ()

...
...
Void Onclick ()
{
...

Stubfirevoid (clicked ); // Send signal of clicked
}

};

ClassCdelay
{
Public:
...
...
Protected:
Gstubbegin (cdelay)

Gslotvoid (start,"Start")
Gsignalvoid (timeup,"Time")

Gstubend ()

// Start processing function
Void Handler_start ()
{
// Start timing
...
}


Void Update ()
{
...
...

If(Istimeup ())//Time
Stubfirevoid (timeup );
}
...
...
};

struct propinput: Public propertybase
{< br> /// mouse, Keyboard Message
...
...
};

ClassCcharctrl
{
Public:
...
...
Protected:
Gstubbegin (ccharctrl)

Gslotdefine (userinput, propinput, " Keyboard/mouse input message " ); // This slot accepts a propinput type parameter.

Gsignaldefine (unhandled, propinput, " Unprocessed keyboard/mouse input messages " ); // This signal sends a propinput type parameter

Gsignalvec3d (move, " Move in a certain direction " ); // A vector3d parameter represents a direction.
Gsignalvoid (stopmove, " Stop a mobile message " );
Gsignalvoid (turnleft, " Turn left " );
Gsignalvoid (turnright, " Turn the message to the right " );
Gsignalvoid (stopturn, " Stop rotating message " );
Gsignalvec3d (turnto, " Rotate to a specified angle " ); // The parameter is a vector3d, representing an ouarta.

Gstubend ()

// userinput processing functions
void handler_userinput (propinput * input)
{< br> bool bhandled = false;

//Send various control commands Based on Input (move, stopmove, turnleft, turnright, stopturn, turnto)
...
...

If(!Bhandled)
Stubfire (propinput, input );
...
}

...
...
};

 

The reasons for using this system are as follows: 1. reuse with respect to inheritance/OverloadingCodeI prefer to use code in componentized mode. The underlying layer implements many basic functional modules and the upper layer is responsible for combining them. This is my favorite method, the stub system provides communication between components. A convenient method. 2. the graphical editing method is cool, allowing non-programmers to participate in the design of game logic. I think I am not the only one who is shocked by the flowgraph OF THE crysis editor. however, it seems that the system of cryengine is not easy to use and lacks a modular reuse mechanism. this I have made some improvements. 3. A common program or script is inconvenient to describe the process with latency. For example, a role goes to point a, puts a skill, jumps, lands, and then goes to point B, it is difficult to describe such a process using a common script, because almost every command requires a certain amount And the script is executed immediately. (However, unrealscript seems to have this function, so I have to admire it ). stub system + component graphical editing can easily and intuitively describe such a process. I hope our engine will be available in the future Edit role skills in a fully graphical manner. I am still worried about this system because it looks too much like a toy and has some performance problems. How can it be tested in practice? I hope it will not be a toy. the following is an example to show how a component is connected by stub. To work: This flow graph describes a short process before the game leader enters the world map in an online game: At the beginning, a wait screen is displayed, and the protagonist information is displayed in the background waiting for the server to send, when all the protagonist information is received, close the waiting screen, Then go to the next process. Meaning of each node object: Proxy: A forwarding interface that forwards a signal to multiple slots. Curtain: waiting for the screen Clock: Clock, timed transmission clock signal Waitplayer: A node implemented by a script. It is used to check whether the protagonist information has been fully received. Delay: latency (because the network and hard disk are busy during initialization, it may be stuck, so wait for a while ). here, Stub is about it. I hope you can understand it. Next, let's talk about resource management.

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.