Document directory
- Similar to Java, UScript also has its own VM (VirtualMachine, virtual machine). Let's take a look at the following points to understand:
- Unreal Virtual Machine
- UnrealScript implementation
- UnrealScript binary compatibility
- Technical Considerations
Similar to Java, UScript also has its own VM (VirtualMachine, virtual machine). Let's take a look at some points that need to be understood: Convert from UDNUnreal Virtual Machine
The Unreal VM consists of the following components: Server, client, rendering engine, and engine support code.
Unreal controls the gameplay and interaction between all players and objects. In a single player game, the Unreal client and the Unreal server run on the same machine. In an online game, one machine is used for a dedicated server. All players connected to this server are clients.
All game playback occurs in a "level", which is an independent environment containing ry and actors. Although the UnrealServer can run multiple levels at the same time, each level operates independently and is shielded from each other: an object (actors) cannot walk through different levels, in addition, objects in one level cannot communicate with objects in another level.
Each actor in the map can be controlled by players (many players can be played in online games) or by scripts. When an actor is controlled by a script, the script fully defines how the actor moves and interacts with other actors.
For all the running actors, executed scripts, and events in the world, you may ask how it can understand the execution process of UnrealScript. The answer is as follows:
In order to manage the time, Unreal separates the game operation every second into "Ticks ". A Tick is the minimum time unit used by all actors in a level. A tick is generally 1/100 to 1/10 of a second. The tick time is limited only by the CPU power. The faster the machine is, the shorter the tick duration.
In UnrealScript, the execution of some commands only requires zero tick time (that is, their execution does not occupy any game time), and some commands require a lot of ticks. The function that takes up the game time is called "latent functions (latent function )". Examples of some latent functions include:Sleep,FinishAnimAndMoveTo. Latent functions in UnrealScript can only be called from code in a status (also known as "state code )"), instead, it cannot be called from the code of a function (including the function defined in a State.
When an actor executes a latent function, the status of the actor does not continue until the execution of the latent function is complete. However, other actor or VMS may call the internal functions of the actor. The final result is that all UnrealScript functions can be called at any time, even if the latent function is not completed.
In traditional programming terms, UnrealScript works just as if every actor in a level has its own "thread. Internally, Unreal does not use Windows threads because it will be very inefficient (Windows 95 and Windows NT cannot efficiently process thousands of threads that occur simultaneously ). However, UnrealScript simulates the thread. Although this fact is transparent to UnrealScript code, it becomes very obvious when you write C ++ code that interacts with UnrealScript.
All UnrealScripts will be executed independently of each other. If 100 monsters are walking in the level, all the scripts of these 100 monsters are being executed simultaneously and independently in each "Tick.
UnrealScript implementation
For more information about how UnrealScript is implemented at the underlying layer-from the compilation process to the execution of the final byte code-see the UnrealScript implementation page.
UnrealScript binary compatibility
The UnrealScript design can extend the classes in the package file over time without compromising the binary compatibility. The binary data compatibility here refers to "the dependent binary files can be loaded and connected without error "; whether the code you modified works as you designed is a separate problem. Note that the following types of modifications can be made securely:
- The. uc script file in the package can be re-compiled without compromising binary compatibility.
- Add a new class to the package.
- Add a new function to the class.
- Add a new status to the class.
- Add new variables to the class.
- Deletes a private variable from a class.
Other changes are generally insecure, including (but not limited ):
- Add a new member to struct.
- Removes a class from a package.
- Change the type of any variable, function parameter, or return value.
- Change the number of parameters in the function.
Technical Considerations
Garbage Collection.
All objects and actors in Unreal use a garbage collector similar to the tree structure traversal of Java VM for garbage collection. The Unreal Garbage Collector uses the serialization function of the UObject class to recursively determine which other objects are referenced by each active object. Therefore, objects do not need to be explicitly deleted, because when they are not referenced, the garbage collector will eventually recycle them. Although this method has the negative impact of hiding unreferenced objects, its efficiency is far higher than that of referencing the rarely deleted objects. For more information, see the garbage collection page. Bytes
UnrealScript is based on bytecode.
UnrealScript code is compiled into a series of bytecode similar to p-code or Java bytecode. This makes UnrealScript platform independent. This allows Unreal client and server components to be directly transplanted to other platforms, that is, Macintosh or Unix, all versions can easily perform interactive operations by executing the same script.
Unreal as a virtual machine.
The Unreal Engine can be regarded as a virtual machine for 3D games, just as the Java language and its built-in Java class hierarchy define a virtual machine for web development scripts. Unreal virtual machines are inherently portable (due to the separation of all platform-dependent code in different modules) and scalable (due to the hierarchical structure of extensible classes ). However, at present, we have never thought of translating the Unreal VM documentation into a degree that is sufficient for others to create independent but compatible implementations.
UnrealScript compiler execution three times.
Unlike C ++, UnrealScript compilation must be performed three times independently. The first time, the variables, struct, enumeration type, constants, states, and function declarations are analyzed and memorized. A summary of each class is constructed. The second time, compile the script code into byte code. This completely compiles and connects the complex script layers with circular dependencies in these two times without independent connection stages. The third stage is used in the. uc Filedefaultproperties
The value specified by the statement block to analyze and import the default attributes of the class.
Persistent actor status.
In Unreal, it is worth noting that, because users can save the status of the game and all actors at any time, including their script execution status can be saved only when all actors are at the lowest level of the UnrealScript stack. This persistent requirement is the reason why latent functions can only be called from the status code: the status code is executed at the lowest possible level in the stack, so it can be easily serialized. Function Code can exist at any stack level, and the native function (such as) C ++ can be placed under it in the stack, this obviously cannot be saved to the hard disk and restored later.
Unrealfiles (Unreal files) are Unreal native binary files..
Unrealfiles includes an index and serialized storage of objects in a specific Unreal package. Unrealfiles are similar to DLL files because they can contain references to any other objects stored in other Unrealfiles. This method makes it possible to publish Unreal content through a pre-defined package on the Internet, thus saving the download time (by limiting the download of only one specific package at a time ).
Why does UnrealScript not support static variables?.
The good reason C ++ supports static variables is that it is loyal to the root cause of low-level languages. The reason why Java supports static variables does not seem to be well understood, static variables are not supported in UnrealScript because they are vague in serialization, inheritance, and multiple levels: Should static variables be global, which means that the values of all static variables are the same in the Unreal level of all activities? Should they be in each package? Should they be in each level? If so, how do I serialize them-use the class in its. u file or use the level in its. unr file? They are unique in each base class or does the subclass have their own static variable values? In UnrealScript, we avoided this problem by not defining static variables as a language function and left it to programmers for management, programmers can manage static and global variables by creating classes that contain them and exposing them in a real object. If you want to use variables accessed based on each level, you can create a new class containing these variables and make sure they are serialized along with the level. In this way, there will be no ambiguity. For examples of classes for this purpose, see LevelInfo and GameInfo. Bytes