Mini talk game script (1)

Source: Internet
Author: User

Mini talk game script (1)

(Inscription: Recently I learned a new idea on the Internet (it should be from Liu weipeng's blog): writing is for better learning, this is quite different from the original ideas about how to solve the problem. If you think about product or product, isn't things both on both sides? It's just a different angle. So I thought about it later, it is indeed necessary to write down some of your learning experiences or thoughts, which will not only benefit your own improvement, but may also help others in the future. How happy is it? (It seems that I am gradually discontinuing from Mars. I think I have been ignoring online forums in the past ...))

 

  1. Sporadic known knowledge

Speaking of the game script topic, as far as I know, the same compilation principle is a concept. The difference may be that most of the compilation principles do not involve virtual machines. For the sake of flexibility and security, virtual machines are often a must-consider category

I have also taken this course for the compilation principle. I remember that the teacher had been sighing at the course's boring nature, so that the course process was so boring that the exam is approaching, the teacher seems to be worried about our credit plan (probably because we are afraid that we will drag down his title evaluation), so I sent several simulated questions before the exam, and even helped us further clarify the scope, alas, good intentions, the world can be learned! However, there is no surprise for me, so I have no idea how to take the test or how to take the test. After all, there are too many things like this... however, I have some experience in the Course Design of compilation principles. When I have the opportunity to explain it again, I have to stop my self-help complaints and return to our game script (compilation principles ).

First of all, let's talk about the books I know. I used to shop for these books in the library and spent some time on Google. So I recommend three books here, but it's really clumsy, these three books have never been read, but they do not prevent me from selling them, they are the compilation principles, modern compilation principles, and advanced compiler design and implementation called longshu, hushu, and whale respectively in the compilation field. I feel that longshu pays attention to the front-end of the compiler, while whale books focuses more on the back-end of the compiler, while Tiger books is a practical book. I personally feel that it is necessary to explain it step by step with the help of tiger books, supplemented by the theoretical support of longshu in the middle, the whale watching book can help you go further to the next level. (my opinion ...)

Generally, compilation involves the following steps:

. Lexical Analysis

. Syntax analysis

. Semantic Analysis

. Intermediate code generation

. Intermediate code optimization

. Target code generation

Generally, the intermediate code is called the front-end of the compiler, and later called the back-end of the compiler. Theoretically, except for the generation of the target code, other parts are not necessary, however, splitting part of them has its own profound intention. lexical analysis first extracts various forms of words from the input source file into a unified attribute block, which is called a token, then, the syntax analysis checks whether these tokens comply with the syntax rules, fills the symbol table and creates a syntax analysis tree, and then the semantic analysis further checks whether they comply with the corresponding semantic rules, after all security checks are passed, the intermediate code is generated, and the generated intermediate code is optimized later. Finally, the optimized target code generates executable target code. (For more detailed information, see an article translated by Yan Liang a long time ago)

For some simple languages, Semantic Analysis and intermediate code optimization are often omitted. In fact, we can also directly generate the target code while parsing the syntax, but the consequence of doing so is the loss of flexibility. Imagine if we change the format of the target code because of the cross-platform goal in the future, as long as we keep the generation of intermediate code, we can keep the front-end of the compiler unchanged, otherwise...

In addition, virtual machines are generally used to run script code in the custom format. The goal is to achieve real platform independence, but it is a pity that the cost of running speed is required. (Think about the mature Java language, but it is still difficult to look back at the speed of running C/C ++ ...) generally, you need to complete the loading, execution, and shutdown operations in the virtual machine life cycle. For further details, a script loader is required during loading, A resource manager is required when the runtime stack is disabled, and so on. Well, the topic about the virtual machine is stuck. I am afraid I will have a big smile when talking about it, although the following example does not strictly implement a virtual machine prototype, there are also some shadows in the mid-term.

Here I want to describe a very simple script system, which is not even process-oriented, but can only be called as command-based (for this topic, there were several articles in the past ). The so-called command-based mechanism means that the script system only allows you to call the system's fixed APIs (commands) and does not allow you to perform any other types of operations (such as expression operations ), we can imagine the flexibility and practicality of this system... however, in line with the KISS Principle and the need to learn and implement it, we will only discuss this topic here. Most of the content comes from advanced programming of game scripts, and I will only repeat it myself, but I still feel happy.

1.Basic knowledge of Command Script

Even for some complex games, many functions in the game can be completed through a series of sequential actions, for example, consider the following command-based script code:

Showbitmap "image/item0.bmp"

Loadmusic "music/cheer. Mid"

Fademusicin

Getitem "gold of Apple"

Gamemessage "oh, yeah! You got the gold of apple! "

 

This code is very simple, and the functions to be completed are very clear. here we can see a major advantage of the command-Based Script: simple form, but also exposed a major disadvantage: with a single function, we can also see the general form of such script commands:

Command param0 param1 param2...

 

Generally, the above code format can meet the requirements of command script-based, but at the same time, if you want to add a syntax format such as C/C ++, such as parentheses and semicolons, it's just a parsing problem.

The command-based scripts are highly relevant to specific fields, as shown in the code above may be suitable for games such as RPG games, but for the stream of flight simulation or sports competition, i'm afraid I can't do anything...

At this point, you may sneer at the command-based scripting language or start to favor it. But in summary, things have two sides, command-based scripts are good for defining events that will be executed by the game engine in a fixed order, because they are fast and convenient, but other more complex applications, for example, in the end, the boss's AI may not be able to cope with the problem, so the selection of it is still a range of problems, there is a trade-off.

(There is a good example of a command-Based Script, that is, the "Legend of the holy sword 2" on the golden point a long time ago.)

2.Advanced command script-based Knowledge

 

For a language, no matter how simple it is, we still need to solve its data and Syntax problems first. Let's talk about the data types. Generally, command-based scripts must support integer and string data. The former is used to express various parameter information, while the latter often represents various texts in the game, but in fact, as long as we make some simple extensions, command-based scripts can also support Boolean and floating-point data. For specific practices, see the design implementation examples in the following sections, at the same time, we can also add support for constants. As for the benefits of adding constants, I don't want to repeat the script syntax here. Generally, the command-based scripting language is composed of several commands, and commands are executed sequentially. However, to increase the flexibility of the script, we can add some simple loops and branches, for more information, see the design example.

Finally, let's talk about the loading and execution of script files. When we talk about loading script files, the first problem we encounter is preprocessing. In a command-based script, if we decide to support the include operation, we have to pre-process the script, but there are some problems in it. We will leave it for further discussion. The author is whether to "compile" the code ", you may wonder why there is "Compilation". In fact, according to our general idea, the running of a command-Based Script system may look like this (ignore irrelevant details ):

First open the script file, parse the command name, command parameters, and then execute the corresponding program based on the parsed command and parameters, and then continue the parsing command, until the script ends.

Yes, this is the most common and intuitive method we think of, but there are some problems during this period. The first is the execution speed problem. As we all know, dynamic String Parsing is a very slow process, if the above parsing operation is used to run the script, it will take a considerable amount of CPU time, but in fact, if we compile the compiled script file into a defined binary format before running it, it will greatly speed up the execution of the script file, and at the same time we get at least two additional benefits: first, we can easily find errors in the script, instead of performing the parsing and execution on the front, which may lead to disastrous consequences. Second, we further ensure the security of the script, compared with the previously readable script file, we can provide only the corresponding compilation version after compiling the correct script, without exposing the script content to the daily environment, since pre-compilation has such advantages, do not hesitate to let our scripts support Compilation without hesitation.

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.