Unrealscript self-study note (2), unrealscript function and state

Source: Internet
Author: User

Unrealscript functions and States

Author: 文 Date: 2011/9/22

1. In udk development, function and event are declared in the same form. These two keywords are different only when native encoding is used in C ++ source code authorization. Therefore, we recommend that you use functions to declare functions in a unified manner under udk to avoid confusion.

2. In a subclass function, if you want to call a function of the same name as the parent class, use the super keyword. Super. functionname ([parameters]), for example:

function int FunCall(){ `Log('in sub call!'); super.FunCall(); }

3. When declaring a function, you can use key rules such as private, protected, public, static, and final to limit the access permissions of other classes to the function. This is similar to the definition of C ++. When you need to access

When you ask public functions of this class, you must first instantiate this class. For example:

class A extends Actor;public function int Z(int D, int E){  return D + E;}//-----------------------------------------------------

 

Class B extends actor; var A a_obj; // here is the variable declaration of Class A. When it is not instantiated, it is equivalent to a null pointer of C ++ and is invalid. Private function g () {a_obj = spawn (class 'A'); // generate a Class A instance object. A_obj.z (3, 4); // call the Public Function Z () in Class ()}

 

4. The unrealscript uses the "State Machine" programming idea. Each actor must be in a certain state at a certain time. We can customize the actor status. We can define the phase in different States.

Function with the same name. When the same name function of the actor is called externally, udk will define the function according to the current state of the actor, such:

Class A extends actor; Public Function int Z (int d, int e) // function 1 {return D * E ;} state add // Add state {public function int Z (int d, int e) // function 2 {return D + E ;}} state Sub // subtract state {public function int Z (int d, int e) // function 3 {return d-e ;}}//-------------------------------------------------------------------

 

Class B extends actor; var A OBJ; Private function g () {local int V; OBJ = spawn (class 'A'); // generate a Class A instance object. V = obj. Z (3, 4); // function 1 V = 3*4; obj. gotostate ('add'); // enter the add state V = obj. Z (3, 4); // function 2 V = 3 + 4; obj. gotostate ('sub'); // enter the subtract State v = obj. Z (3, 4); // function 2 V = 3-4 ;}

5. Functions and States)

When I was a beginner at udk, I was very confused about the relationship between functions and States.

State. In unrealscript, it looks like a struct, but its function is like a function. State: it has its own code, which can be called in a function, or it can call a function.

What is the difference between state and function?

Since the state function is similar to the function, when is the state function used? Why is State used?

To fully answer these questions, we must first understand a very critical idea: "State is an independent thread within each actor ". How can this sentence be understood? That is to say, after the game is started, the udk has a main thread that will refresh each frame, to call the tick () function of each actor in a scenario, we can perform some update operations in this function, such as physical operations. Each actor can have its own internal independent thread. We call this thread a State thread. A State thread is used to run state code. It works in parallel with the main thread. As shown in.

 

Let's look at a piece of code:

Class countnumactor extends ator; var int sheepnum; // number of sheep var int starnum; // number of stars event tick (float deltatime) {starnum ++; 'Log (starnum @ "Stars");} auto state countsheep {// bored. continue counting sheep begin: sheepnum ++; 'Log (sheepnum @ "Sheep"); GOTO ('begin ');}

 

The countnumactor in this code is a deeply insomnia actor. The main thread is counting stars and the State thread is counting sheep. In the state thread, we use a GOTO ('begin'), which is actually an endless loop of code, but it does not affect the number of stars in the main thread, because two threads are parallel threads.

When we can understand the idea that "the State is an independent thread inside each actor", we can only see the confusion in front of it.

Why can code be run in the state? It is to reduce code coupling and better reflect the idea of targeting the image. It is a guiding ideology of "let alone the snow" and requires "Do your own thing", such as AI, it can automatically search for the enemy in the state thread, and automatically fire after finding the enemy. For example, some actors use TCP network communication to transmit large volumes of data. When the network is congested, the response may be slow. In this case, we can use the state thread to wait slowly without affecting the update speed of the main thread, thus ensuring the smoothness of the overall game picture.

Of course, the above is why State can run its own code like a function, but when you get to know unrealscript more deeply, you will find that, many States are not executing code. In fact, the core function of State is to record which state the actor is in. In this state, we can constrain what the actor can do and what it cannot do in the State. In addition, each State contains the two functions beginstate (), endstate (), the former runs automatically every time we enter this state, and the latter runs automatically when we exit this state. In this way, we can inherit and overwrite these two functions in each State, so as to implement initialization and cleaning during exit.

 

Here, we will mainly clarify the relationship between function and State. There are many more in-depth applications about state and state machines. There is a lot of information on the Internet, I am not going to discuss it in depth here.

 

 

E-MAIL: huawenguang@sina.com

 

========================================================== ========================================================== ========================================================== ==========

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.